1616
1717package compute ;
1818
19+ import static compute .disks .consistencygroup .DeleteDiskConsistencyGroup .deleteDiskConsistencyGroup ;
20+
1921import com .google .cloud .compute .v1 .DeleteStoragePoolRequest ;
2022import com .google .cloud .compute .v1 .Disk ;
2123import com .google .cloud .compute .v1 .DisksClient ;
3032import com .google .cloud .compute .v1 .RegionInstanceTemplatesClient ;
3133import com .google .cloud .compute .v1 .Reservation ;
3234import com .google .cloud .compute .v1 .ReservationsClient ;
35+ import com .google .cloud .compute .v1 .ResourcePoliciesClient ;
36+ import com .google .cloud .compute .v1 .ResourcePolicy ;
3337import com .google .cloud .compute .v1 .Snapshot ;
3438import com .google .cloud .compute .v1 .SnapshotsClient ;
3539import com .google .cloud .compute .v1 .StoragePool ;
@@ -266,6 +270,21 @@ public static void deleteStoragePool(String project, String zone, String storage
266270 }
267271 }
268272
273+ // Delete storagePools which starts with the given prefixToDelete and
274+ // has creation timestamp >24 hours.
275+ public static void cleanUpExistingConsistencyGroup (
276+ String prefixToDelete , String projectId , String region )
277+ throws IOException , ExecutionException , InterruptedException {
278+ try (ResourcePoliciesClient client = ResourcePoliciesClient .create ()) {
279+ for (ResourcePolicy resourcePolicy : client .list (projectId , region ).iterateAll ()) {
280+ if (containPrefixToDeleteAndZone (resourcePolicy , prefixToDelete , region )
281+ && isCreatedBeforeThresholdTime (resourcePolicy .getCreationTimestamp ())) {
282+ deleteDiskConsistencyGroup (projectId , region , resourcePolicy .getName ());
283+ }
284+ }
285+ }
286+ }
287+
269288 public static boolean containPrefixToDeleteAndZone (
270289 Object resource , String prefixToDelete , String zone ) {
271290 boolean containPrefixAndZone = false ;
@@ -291,6 +310,11 @@ public static boolean containPrefixToDeleteAndZone(
291310 containPrefixAndZone = ((StoragePool ) resource ).getName ().contains (prefixToDelete )
292311 && ((StoragePool ) resource ).getZone ().contains (zone );
293312 }
313+ if (resource instanceof ResourcePolicy ) {
314+ containPrefixAndZone = ((ResourcePolicy ) resource ).getName ().contains (prefixToDelete )
315+ && ((ResourcePolicy ) resource ).getRegion ()
316+ .contains (zone .substring (0 , zone .lastIndexOf ('-' )));
317+ }
294318 } catch (NullPointerException e ) {
295319 System .out .println ("Resource not found, skipping deletion:" );
296320 }
0 commit comments