1616
1717package compute ;
1818
19- import com .google .api .gax .rpc .NotFoundException ;
20- import com .google .cloud .compute .v1 .AggregatedListInstancesRequest ;
2119import com .google .cloud .compute .v1 .Disk ;
2220import com .google .cloud .compute .v1 .DisksClient ;
2321import com .google .cloud .compute .v1 .Instance ;
24- import com .google .cloud .compute .v1 .Instance .Status ;
2522import com .google .cloud .compute .v1 .InstanceTemplate ;
2623import com .google .cloud .compute .v1 .InstanceTemplatesClient ;
2724import com .google .cloud .compute .v1 .InstanceTemplatesClient .ListPagedResponse ;
2825import com .google .cloud .compute .v1 .InstancesClient ;
29- import com .google .cloud .compute .v1 .InstancesClient .AggregatedListPagedResponse ;
30- import com .google .cloud .compute .v1 .InstancesScopedList ;
31- import com .google .cloud .compute .v1 .ListInstanceTemplatesRequest ;
26+ import com .google .cloud .compute .v1 .ListRegionInstanceTemplatesRequest ;
3227import com .google .cloud .compute .v1 .RegionDisksClient ;
28+ import com .google .cloud .compute .v1 .RegionInstanceTemplatesClient ;
3329import com .google .cloud .compute .v1 .Reservation ;
3430import com .google .cloud .compute .v1 .ReservationsClient ;
31+ import com .google .cloud .compute .v1 .Snapshot ;
32+ import com .google .cloud .compute .v1 .StoragePool ;
33+ import compute .deleteprotection .SetDeleteProtection ;
3534import compute .reservation .DeleteReservation ;
3635import java .io .IOException ;
3736import java .nio .charset .StandardCharsets ;
4039import java .time .OffsetDateTime ;
4140import java .time .temporal .ChronoUnit ;
4241import java .util .Base64 ;
43- import java .util .Map ;
4442import java .util .Random ;
4543import java .util .concurrent .ExecutionException ;
4644import java .util .concurrent .TimeoutException ;
@@ -52,7 +50,7 @@ public abstract class Util {
5250 // resources
5351 // and delete the listed resources based on the timestamp.
5452
55- private static final int DELETION_THRESHOLD_TIME_HOURS = 24 ;
53+ private static final int DELETION_THRESHOLD_TIME_HOURS = 30 ;
5654 // comma separate list of zone names
5755 private static final String TEST_ZONES_NAME = "JAVA_DOCS_COMPUTE_TEST_ZONES" ;
5856 private static final String DEFAULT_ZONES = "us-central1-a,us-west1-a,asia-south1-a" ;
@@ -61,70 +59,67 @@ public abstract class Util {
6159 // has creation timestamp >24 hours.
6260 public static void cleanUpExistingInstanceTemplates (String prefixToDelete , String projectId )
6361 throws IOException , ExecutionException , InterruptedException , TimeoutException {
64- for (InstanceTemplate template : listFilteredInstanceTemplates (projectId , prefixToDelete )
65- .iterateAll ()) {
66- if (!template .hasCreationTimestamp ()) {
67- continue ;
62+ try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient .create ()) {
63+ ListPagedResponse templates = instanceTemplatesClient .list (projectId );
64+ for (InstanceTemplate instanceTemplate : templates .iterateAll ()) {
65+ if (containPrefixToDelete (instanceTemplate , prefixToDelete )
66+ && isCreatedBeforeThresholdTime (instanceTemplate .getCreationTimestamp ())
67+ && instanceTemplate .isInitialized ()) {
68+ DeleteInstanceTemplate .deleteInstanceTemplate (projectId , instanceTemplate .getName ());
69+ }
6870 }
69- if (template .getName ().contains (prefixToDelete )
70- && isCreatedBeforeThresholdTime (template .getCreationTimestamp ())
71- && template .isInitialized ()) {
72- DeleteInstanceTemplate .deleteInstanceTemplate (projectId , template .getName ());
71+ }
72+ }
73+
74+ // Delete regional instance templates which starts with the given prefixToDelete and
75+ // has creation timestamp >24 hours.
76+ public static void cleanUpExistingRegionalInstanceTemplates (
77+ String prefixToDelete , String projectId , String zone )
78+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
79+ try (RegionInstanceTemplatesClient instanceTemplatesClient =
80+ RegionInstanceTemplatesClient .create ()) {
81+ String region = zone .substring (0 , zone .lastIndexOf ('-' ));
82+ ListRegionInstanceTemplatesRequest request =
83+ ListRegionInstanceTemplatesRequest .newBuilder ()
84+ .setProject (projectId )
85+ .setRegion (region )
86+ .build ();
87+
88+ for (InstanceTemplate instanceTemplate :
89+ instanceTemplatesClient .list (request ).iterateAll ()) {
90+ if (containPrefixToDeleteAndZone (instanceTemplate , prefixToDelete , zone )
91+ && isCreatedBeforeThresholdTime (instanceTemplate .getCreationTimestamp ())
92+ && instanceTemplate .isInitialized ()) {
93+ DeleteRegionalInstanceTemplate .deleteRegionalInstanceTemplate (
94+ projectId , region , instanceTemplate .getName ());
95+ }
7396 }
7497 }
7598 }
7699
77100 // Delete instances which starts with the given prefixToDelete and
78101 // has creation timestamp >24 hours.
79102 public static void cleanUpExistingInstances (String prefixToDelete , String projectId ,
80- String instanceZone )
103+ String instanceZone )
81104 throws IOException , ExecutionException , InterruptedException , TimeoutException {
82- for (Map .Entry <String , InstancesScopedList > instanceGroup : listFilteredInstances (
83- projectId , prefixToDelete ).iterateAll ()) {
84- for (Instance instance : instanceGroup .getValue ().getInstancesList ()) {
85- if (!instance .hasCreationTimestamp ()) {
86- continue ;
105+ try (InstancesClient instancesClient = InstancesClient .create ()) {
106+ for (Instance instance : instancesClient .list (projectId , instanceZone ).iterateAll ()) {
107+ if (instance .getDeletionProtection ()
108+ && isCreatedBeforeThresholdTime (instance .getCreationTimestamp ())) {
109+ SetDeleteProtection .setDeleteProtection (
110+ projectId , instanceZone , instance .getName (), false );
87111 }
88- if (instance .getName ().contains (prefixToDelete )
89- && isCreatedBeforeThresholdTime (instance .getCreationTimestamp ())
90- && instance .getStatus ().equalsIgnoreCase (Status .RUNNING .toString ())) {
112+ if (containPrefixToDeleteAndZone (instance , prefixToDelete , instanceZone )
113+ && isCreatedBeforeThresholdTime (instance .getCreationTimestamp ())) {
91114 DeleteInstance .deleteInstance (projectId , instanceZone , instance .getName ());
92115 }
93116 }
94117 }
95118 }
96119
97- public static AggregatedListPagedResponse listFilteredInstances (
98- String project , String instanceNamePrefix ) throws IOException {
99- try (InstancesClient instancesClient = InstancesClient .create ()) {
100-
101- AggregatedListInstancesRequest aggregatedListInstancesRequest = AggregatedListInstancesRequest
102- .newBuilder ()
103- .setProject (project )
104- .setFilter (String .format ("name:%s" , instanceNamePrefix ))
105- .build ();
106-
107- return instancesClient
108- .aggregatedList (aggregatedListInstancesRequest );
109- }
110- }
111-
112- public static ListPagedResponse listFilteredInstanceTemplates (
113- String projectId , String instanceTemplatePrefix ) throws IOException {
114- try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient .create ()) {
115- ListInstanceTemplatesRequest listInstanceTemplatesRequest =
116- ListInstanceTemplatesRequest .newBuilder ()
117- .setProject (projectId )
118- .setFilter (String .format ("name:%s" , instanceTemplatePrefix ))
119- .build ();
120-
121- return instanceTemplatesClient .list (listInstanceTemplatesRequest );
122- }
123- }
124-
125120 public static boolean isCreatedBeforeThresholdTime (String timestamp ) {
126121 return OffsetDateTime .parse (timestamp ).toInstant ()
127- .isBefore (Instant .now ().minus (DELETION_THRESHOLD_TIME_HOURS , ChronoUnit .HOURS ));
122+ .isBefore (Instant .now ().minus (DELETION_THRESHOLD_TIME_HOURS , ChronoUnit .MINUTES ));
128123 }
129124
130125 public static String getBase64EncodedKey () {
@@ -185,25 +180,65 @@ public static String getEnvVar(String envVarName, String defaultValue) {
185180 return val ;
186181 }
187182
188- // Delete reservations which starts with the given prefixToDelete and
183+ // Delete reservation which starts with the given prefixToDelete and
189184 // has creation timestamp >24 hours.
190185 public static void cleanUpExistingReservations (
191186 String prefixToDelete , String projectId , String zone )
192187 throws IOException , ExecutionException , InterruptedException , TimeoutException {
193188 try (ReservationsClient reservationsClient = ReservationsClient .create ()) {
194189 for (Reservation reservation : reservationsClient .list (projectId , zone ).iterateAll ()) {
195- if (!reservationsClient .list (projectId , zone ).iterateAll ().iterator ().hasNext ()) {
196- break ;
197- }
198- if (reservation .getName ().contains (prefixToDelete )
190+ if (containPrefixToDeleteAndZone (reservation , prefixToDelete , zone )
199191 && isCreatedBeforeThresholdTime (reservation .getCreationTimestamp ())) {
200- try {
201- DeleteReservation .deleteReservation (projectId , zone , reservation .getName ());
202- } catch (NotFoundException e ) {
203- System .err .println ("Reservation not found, skipping deletion:" + reservation .getName ());
204- }
192+ DeleteReservation .deleteReservation (projectId , zone , reservation .getName ());
205193 }
206194 }
207195 }
208196 }
197+
198+ public static boolean containPrefixToDeleteAndZone (
199+ Object resource , String prefixToDelete , String zone ) {
200+ boolean containPrefixAndZone = false ;
201+ try {
202+ if (resource instanceof Instance ) {
203+ containPrefixAndZone = ((Instance ) resource ).getName ().contains (prefixToDelete )
204+ && ((Instance ) resource ).getZone ().contains (zone );
205+ }
206+ if (resource instanceof InstanceTemplate ) {
207+ containPrefixAndZone = ((InstanceTemplate ) resource ).getName ().contains (prefixToDelete )
208+ && ((InstanceTemplate ) resource ).getRegion ()
209+ .contains (zone .substring (0 , zone .lastIndexOf ('-' )));
210+ }
211+ if (resource instanceof Reservation ) {
212+ containPrefixAndZone = ((Reservation ) resource ).getName ().contains (prefixToDelete )
213+ && ((Reservation ) resource ).getZone ().contains (zone );
214+ }
215+ if (resource instanceof Disk ) {
216+ containPrefixAndZone = ((Disk ) resource ).getName ().contains (prefixToDelete )
217+ && ((Disk ) resource ).getZone ().contains (zone );
218+ }
219+ if (resource instanceof StoragePool ) {
220+ containPrefixAndZone = ((StoragePool ) resource ).getName ().contains (prefixToDelete )
221+ && ((StoragePool ) resource ).getZone ().contains (zone );
222+ }
223+ } catch (NullPointerException e ) {
224+ System .err .println ("Resource not found, skipping deletion:" );
225+ }
226+ return containPrefixAndZone ;
227+ }
228+
229+ public static boolean containPrefixToDelete (
230+ Object resource , String prefixToDelete ) {
231+ boolean containPrefixToDelete = false ;
232+ try {
233+ if (resource instanceof InstanceTemplate ) {
234+ containPrefixToDelete = ((InstanceTemplate ) resource ).getName ().contains (prefixToDelete );
235+ }
236+ if (resource instanceof Snapshot ) {
237+ containPrefixToDelete = ((Snapshot ) resource ).getName ().contains (prefixToDelete );
238+ }
239+ } catch (NullPointerException e ) {
240+ System .err .println ("Resource not found, skipping deletion:" );
241+ }
242+ return containPrefixToDelete ;
243+ }
209244}
0 commit comments