|
| 1 | +/* |
| 2 | + * Copyright 2021 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.cloud.compute.v1.integration; |
| 17 | + |
| 18 | +import com.google.cloud.compute.v1.Address; |
| 19 | +import com.google.cloud.compute.v1.AddressesClient; |
| 20 | +import com.google.cloud.compute.v1.DeleteInstanceRequest; |
| 21 | +import com.google.cloud.compute.v1.Instance; |
| 22 | +import com.google.cloud.compute.v1.InstancesClient; |
| 23 | +import com.google.cloud.compute.v1.InstancesClient.ListPagedResponse; |
| 24 | +import java.time.Instant; |
| 25 | +import java.time.OffsetDateTime; |
| 26 | +import java.time.ZonedDateTime; |
| 27 | +import java.time.temporal.ChronoUnit; |
| 28 | + |
| 29 | +public class Util { |
| 30 | + |
| 31 | + // Cleans existing test resources if any. |
| 32 | + private static final int DELETION_THRESHOLD_TIME_HOURS = 24; |
| 33 | + |
| 34 | + /** Bring down any instances that are older than 24 hours */ |
| 35 | + public static void cleanUpComputeInstances( |
| 36 | + InstancesClient instancesClient, String project, String zone, String prefix) { |
| 37 | + ListPagedResponse listPagedResponse = instancesClient.list(project, zone); |
| 38 | + for (Instance instance : listPagedResponse.iterateAll()) { |
| 39 | + if (isCreatedBeforeThresholdTime( |
| 40 | + ZonedDateTime.parse(instance.getCreationTimestamp()).toInstant()) |
| 41 | + && instance.getName().startsWith(prefix)) { |
| 42 | + instancesClient.deleteAsync( |
| 43 | + DeleteInstanceRequest.newBuilder() |
| 44 | + .setInstance(instance.getName()) |
| 45 | + .setProject(project) |
| 46 | + .setZone(zone) |
| 47 | + .build()); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** Bring down any addresses that are older than 24 hours */ |
| 53 | + public static void cleanUpComputeAddresses( |
| 54 | + AddressesClient addressesClient, String project, String region, String prefix) { |
| 55 | + AddressesClient.ListPagedResponse listPagedResponse = addressesClient.list(project, region); |
| 56 | + for (Address address : listPagedResponse.iterateAll()) { |
| 57 | + if (isCreatedBeforeThresholdTime(address.getCreationTimestamp()) |
| 58 | + && address.getName().startsWith(prefix)) { |
| 59 | + addressesClient.deleteAsync(project, region, address.getName()); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private static boolean isCreatedBeforeThresholdTime(Instant instant) { |
| 65 | + return instant.isBefore(Instant.now().minus(DELETION_THRESHOLD_TIME_HOURS, ChronoUnit.HOURS)); |
| 66 | + } |
| 67 | + |
| 68 | + private static boolean isCreatedBeforeThresholdTime(String timestamp) { |
| 69 | + return OffsetDateTime.parse(timestamp) |
| 70 | + .toInstant() |
| 71 | + .isBefore(Instant.now().minus(DELETION_THRESHOLD_TIME_HOURS, ChronoUnit.HOURS)); |
| 72 | + } |
| 73 | +} |
0 commit comments