1919//[START tpu_queued_resources_delete]
2020
2121import com .google .api .gax .retrying .RetrySettings ;
22+ import com .google .api .gax .rpc .UnknownException ;
2223import com .google .cloud .tpu .v2alpha1 .DeleteQueuedResourceRequest ;
24+ import com .google .cloud .tpu .v2alpha1 .GetQueuedResourceRequest ;
25+ import com .google .cloud .tpu .v2alpha1 .QueuedResource ;
2326import com .google .cloud .tpu .v2alpha1 .TpuClient ;
2427import com .google .cloud .tpu .v2alpha1 .TpuSettings ;
2528import java .io .IOException ;
2629import java .util .concurrent .ExecutionException ;
2730import org .threeten .bp .Duration ;
2831
2932public class DeleteQueuedResource {
30- public static void main (String [] args )
31- throws IOException , ExecutionException , InterruptedException {
33+ public static void main (String [] args ) {
3234 // TODO(developer): Replace these variables before running the sample.
3335 // Project ID or project number of the Google Cloud project.
3436 String projectId = "YOUR_PROJECT_ID" ;
@@ -41,8 +43,7 @@ public static void main(String[] args)
4143 }
4244
4345 // Deletes a Queued Resource asynchronously.
44- public static void deleteQueuedResource (String projectId , String zone , String queuedResourceId )
45- throws ExecutionException , InterruptedException {
46+ public static void deleteQueuedResource (String projectId , String zone , String queuedResourceId ) {
4647 String name = String .format ("projects/%s/locations/%s/queuedResources/%s" ,
4748 projectId , zone , queuedResourceId );
4849 // With these settings the client library handles the Operation's polling mechanism
@@ -63,13 +64,21 @@ public static void deleteQueuedResource(String projectId, String zone, String qu
6364 // Initialize client that will be used to send requests. This client only needs to be created
6465 // once, and can be reused for multiple requests.
6566 try (TpuClient tpuClient = TpuClient .create (clientSettings .build ())) {
67+ // Retrive node name
68+ GetQueuedResourceRequest getRequest =
69+ GetQueuedResourceRequest .newBuilder ().setName (name ).build ();
70+ QueuedResource queuedResource = tpuClient .getQueuedResource (getRequest );
71+ String nodeName = queuedResource .getTpu ().getNodeSpec (0 ).getNode ().getName ();
72+ // Before deleting the queued resource it is required to delete the TPU VM.
73+ DeleteTpuVm .deleteTpuVm (projectId , zone , nodeName );
74+
6675 DeleteQueuedResourceRequest request =
6776 DeleteQueuedResourceRequest .newBuilder ().setName (name ).build ();
6877
6978 tpuClient .deleteQueuedResourceAsync (request ).get ();
7079
71- } catch (Exception e ) {
72- System .out .println ("Error unpacking QueuedResource: " + e .getMessage ());
80+ } catch (UnknownException | InterruptedException | ExecutionException | IOException e ) {
81+ System .out .println (e .getMessage ());
7382 }
7483 System .out .printf ("Deleted Queued Resource: %s\n " , name );
7584 }
0 commit comments