3434import com .google .cloud .compute .v1 .SnapshotsClient ;
3535import com .google .cloud .compute .v1 .StoragePool ;
3636import com .google .cloud .compute .v1 .StoragePoolsClient ;
37+ import com .google .cloud .tpu .v2 .Node ;
38+ import com .google .cloud .tpu .v2 .TpuClient ;
39+ import com .google .protobuf .Timestamp ;
3740import compute .deleteprotection .SetDeleteProtection ;
3841import compute .disks .DeleteDisk ;
3942import compute .disks .DeleteSnapshot ;
4043import compute .reservation .DeleteReservation ;
44+ import compute .tpu .DeleteTpuVm ;
4145import java .io .IOException ;
4246import java .nio .charset .StandardCharsets ;
4347import java .security .SecureRandom ;
4448import java .time .Instant ;
4549import java .time .OffsetDateTime ;
50+ import java .time .ZoneOffset ;
51+ import java .time .format .DateTimeFormatter ;
4652import java .time .temporal .ChronoUnit ;
4753import java .util .Base64 ;
4854import java .util .Random ;
@@ -57,7 +63,7 @@ public abstract class Util {
5763 // resources
5864 // and delete the listed resources based on the timestamp.
5965
60- private static final int DELETION_THRESHOLD_TIME_MINUTES = 45 ;
66+ private static final int DELETION_THRESHOLD_TIME_MINUTES = 3 ;
6167 // comma separate list of zone names
6268 private static final String TEST_ZONES_NAME = "JAVA_DOCS_COMPUTE_TEST_ZONES" ;
6369 private static final String DEFAULT_ZONES = "us-central1-a,us-west1-a,asia-south1-a" ;
@@ -231,14 +237,31 @@ && isCreatedBeforeThresholdTime(snapshot.getCreationTimestamp())) {
231237 }
232238 }
233239
240+ // Delete tpu which starts with the given prefixToDelete and
241+ // has creation timestamp >24 hours.
242+ public static void cleanUpExistingTpu (String prefixToDelete , String projectId , String zone )
243+ throws IOException , ExecutionException , InterruptedException {
244+ try (TpuClient tpuClient = TpuClient .create ()) {
245+ String parent = String .format ("projects/%s/locations/%s" , projectId , zone );
246+ for (Node node : tpuClient .listNodes (parent ).iterateAll ()) {
247+ String creationTime = formatTimestamp (node .getCreateTime ());
248+ String name = node .getName ().substring (node .getName ().lastIndexOf ("/" ) + 1 );
249+ if (containPrefixToDeleteAndZone (node , prefixToDelete , zone )
250+ && isCreatedBeforeThresholdTime (creationTime )) {
251+ DeleteTpuVm .deleteTpuVm (projectId , zone , name );
252+ }
253+ }
254+ }
255+ }
256+
234257 // Delete storagePools which starts with the given prefixToDelete and
235258 // has creation timestamp >24 hours.
236259 public static void cleanUpExistingStoragePool (
237260 String prefixToDelete , String projectId , String zone )
238261 throws IOException , ExecutionException , InterruptedException , TimeoutException {
239262 try (StoragePoolsClient storagePoolsClient = StoragePoolsClient .create ()) {
240263 for (StoragePool storagePool : storagePoolsClient .list (projectId , zone ).iterateAll ()) {
241- if (containPrefixToDeleteAndZone (projectId , prefixToDelete , zone )
264+ if (containPrefixToDeleteAndZone (storagePool , prefixToDelete , zone )
242265 && isCreatedBeforeThresholdTime (storagePool .getCreationTimestamp ())) {
243266 deleteStoragePool (projectId , zone , storagePool .getName ());
244267 }
@@ -291,6 +314,10 @@ public static boolean containPrefixToDeleteAndZone(
291314 containPrefixAndZone = ((StoragePool ) resource ).getName ().contains (prefixToDelete )
292315 && ((StoragePool ) resource ).getZone ().contains (zone );
293316 }
317+ if (resource instanceof Node ) {
318+ containPrefixAndZone = ((Node ) resource ).getName ().contains (prefixToDelete )
319+ && ((Node ) resource ).getName ().split ("/" )[3 ].contains (zone );
320+ }
294321 } catch (NullPointerException e ) {
295322 System .out .println ("Resource not found, skipping deletion:" );
296323 }
@@ -312,4 +339,12 @@ public static boolean containPrefixToDelete(
312339 }
313340 return containPrefixToDelete ;
314341 }
342+
343+ private static String formatTimestamp (Timestamp timestamp ) {
344+ DateTimeFormatter formatter = DateTimeFormatter .ofPattern ("yyyy-MM-dd'T'HH:mm:ss.SSSXXX" );
345+ OffsetDateTime offsetDateTime = OffsetDateTime .ofInstant (
346+ Instant .ofEpochSecond (timestamp .getSeconds (), timestamp .getNanos ()),
347+ ZoneOffset .UTC );
348+ return formatter .format (offsetDateTime );
349+ }
315350}
0 commit comments