|
16 | 16 |
|
17 | 17 | package compute; |
18 | 18 |
|
| 19 | +import static compute.disks.consistencygroup.DeleteDiskConsistencyGroup.deleteDiskConsistencyGroup; |
| 20 | + |
19 | 21 | import com.google.cloud.compute.v1.DeleteStoragePoolRequest; |
20 | 22 | import com.google.cloud.compute.v1.Disk; |
21 | 23 | import com.google.cloud.compute.v1.DisksClient; |
|
30 | 32 | import com.google.cloud.compute.v1.RegionInstanceTemplatesClient; |
31 | 33 | import com.google.cloud.compute.v1.Reservation; |
32 | 34 | import com.google.cloud.compute.v1.ReservationsClient; |
| 35 | +import com.google.cloud.compute.v1.ResourcePoliciesClient; |
| 36 | +import com.google.cloud.compute.v1.ResourcePolicy; |
33 | 37 | import com.google.cloud.compute.v1.Snapshot; |
34 | 38 | import com.google.cloud.compute.v1.SnapshotsClient; |
35 | 39 | import com.google.cloud.compute.v1.StoragePool; |
|
52 | 56 | import java.util.concurrent.TimeoutException; |
53 | 57 | import java.util.stream.IntStream; |
54 | 58 |
|
| 59 | + |
55 | 60 | public abstract class Util { |
56 | 61 | // Cleans existing test resources if any. |
57 | 62 | // If the project contains too many instances, use "filter" when listing |
@@ -283,6 +288,22 @@ && isCreatedBeforeThresholdTime(disk.getCreationTimestamp())) { |
283 | 288 | } |
284 | 289 | } |
285 | 290 |
|
| 291 | + // Delete ConsistencyGroup which starts with the given prefixToDelete and |
| 292 | + // has creation timestamp >24 hours. |
| 293 | + public static void cleanUpExistingConsistencyGroup( |
| 294 | + String prefixToDelete, String projectId, String region) |
| 295 | + throws IOException, ExecutionException, InterruptedException { |
| 296 | + try (ResourcePoliciesClient client = ResourcePoliciesClient.create()) { |
| 297 | + for (ResourcePolicy resourcePolicy : client.list(projectId, region).iterateAll()) { |
| 298 | + if (resourcePolicy.getName().contains(prefixToDelete) |
| 299 | + && resourcePolicy.getRegion().equals(region) |
| 300 | + && isCreatedBeforeThresholdTime(resourcePolicy.getCreationTimestamp())) { |
| 301 | + deleteDiskConsistencyGroup(projectId, region, resourcePolicy.getName()); |
| 302 | + } |
| 303 | + } |
| 304 | + } |
| 305 | + } |
| 306 | + |
286 | 307 | public static boolean containPrefixToDeleteAndZone( |
287 | 308 | Object resource, String prefixToDelete, String zone) { |
288 | 309 | boolean containPrefixAndZone = false; |
|
0 commit comments