From 309f877106a59af781a901f478df823acef1ed94 Mon Sep 17 00:00:00 2001 From: Joanna Grycz Date: Wed, 23 Oct 2024 10:58:34 +0200 Subject: [PATCH] feat: tpu_queued_resources_list --- .../createQueuedResource.js | 5 +- .../deleteQueuedResource.js | 0 .../forceDeleteQueuedResource.js | 0 .../getQueuedResource.js | 0 tpu/queuedResources/getQueuedResourcesList.js | 52 +++++++++++++++++++ tpu/test/forceDeleteQueuedResource.test.js | 4 +- tpu/test/queuedResource.test.js | 17 ++++-- 7 files changed, 70 insertions(+), 8 deletions(-) rename tpu/{queuedResource => queuedResources}/createQueuedResource.js (95%) rename tpu/{queuedResource => queuedResources}/deleteQueuedResource.js (100%) rename tpu/{queuedResource => queuedResources}/forceDeleteQueuedResource.js (100%) rename tpu/{queuedResource => queuedResources}/getQueuedResource.js (100%) create mode 100644 tpu/queuedResources/getQueuedResourcesList.js diff --git a/tpu/queuedResource/createQueuedResource.js b/tpu/queuedResources/createQueuedResource.js similarity index 95% rename from tpu/queuedResource/createQueuedResource.js rename to tpu/queuedResources/createQueuedResource.js index bb5e991dc96..e8ab1484d65 100644 --- a/tpu/queuedResource/createQueuedResource.js +++ b/tpu/queuedResources/createQueuedResource.js @@ -110,9 +110,8 @@ async function main( // Wait for the create operation to complete. await operation.promise(); - // If you also want to wait for create operation of TPU Node, - // you can use `tpu_vm_get` sample to check current status of the node - // and wait until it is READY. + // You can wait until TPU Node is READY, + // and check its status using getTpuVm() from `tpu_vm_get` sample. console.log(`Queued resource ${queuedResourceName} created.`); } await callCreateQueuedResource(); diff --git a/tpu/queuedResource/deleteQueuedResource.js b/tpu/queuedResources/deleteQueuedResource.js similarity index 100% rename from tpu/queuedResource/deleteQueuedResource.js rename to tpu/queuedResources/deleteQueuedResource.js diff --git a/tpu/queuedResource/forceDeleteQueuedResource.js b/tpu/queuedResources/forceDeleteQueuedResource.js similarity index 100% rename from tpu/queuedResource/forceDeleteQueuedResource.js rename to tpu/queuedResources/forceDeleteQueuedResource.js diff --git a/tpu/queuedResource/getQueuedResource.js b/tpu/queuedResources/getQueuedResource.js similarity index 100% rename from tpu/queuedResource/getQueuedResource.js rename to tpu/queuedResources/getQueuedResource.js diff --git a/tpu/queuedResources/getQueuedResourcesList.js b/tpu/queuedResources/getQueuedResourcesList.js new file mode 100644 index 00000000000..a553aa20c59 --- /dev/null +++ b/tpu/queuedResources/getQueuedResourcesList.js @@ -0,0 +1,52 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +async function main(zone) { + // [START tpu_queued_resources_list] + // Import the TPU library + const {TpuClient} = require('@google-cloud/tpu').v2alpha1; + + // Instantiate a tpuClient + const tpuClient = new TpuClient(); + + /** + * TODO(developer): Update/uncomment these variables before running the sample. + */ + // Project ID or project number of the Google Cloud project, where you want to retrive the list of Queued Resources. + const projectId = await tpuClient.getProjectId(); + + // The zone from which the Queued Resources are retrived. + // zone = 'europe-west4-a'; + + async function callGetQueuedResourcesList() { + const request = { + parent: `projects/${projectId}/locations/${zone}`, + }; + + const [response] = await tpuClient.listQueuedResources(request); + + console.log(JSON.stringify(response)); + } + await callGetQueuedResourcesList(); + // [END tpu_queued_resources_list] +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err); + process.exitCode = 1; +}); diff --git a/tpu/test/forceDeleteQueuedResource.test.js b/tpu/test/forceDeleteQueuedResource.test.js index df1ec53879c..8429f01d4da 100644 --- a/tpu/test/forceDeleteQueuedResource.test.js +++ b/tpu/test/forceDeleteQueuedResource.test.js @@ -35,14 +35,14 @@ describe('TPU queued resource force deletion', async () => { it('should force queued resource deletion', () => { // Create queued resource execSync( - `node ./queuedResource/createQueuedResource.js ${nodeName} ${queuedResourceName} ${zone} ${tpuType} ${tpuSoftwareVersion}`, + `node ./queuedResources/createQueuedResource.js ${nodeName} ${queuedResourceName} ${zone} ${tpuType} ${tpuSoftwareVersion}`, { cwd, } ); const response = execSync( - `node ./queuedResource/forceDeleteQueuedResource.js ${queuedResourceName} ${zone}`, + `node ./queuedResources/forceDeleteQueuedResource.js ${queuedResourceName} ${zone}`, { cwd, } diff --git a/tpu/test/queuedResource.test.js b/tpu/test/queuedResource.test.js index 274049c2738..0467172c4fd 100644 --- a/tpu/test/queuedResource.test.js +++ b/tpu/test/queuedResource.test.js @@ -63,7 +63,7 @@ describe('TPU queued resource', () => { it('should create a new queued resource', async () => { const response = execSync( - `node ./queuedResource/createQueuedResource.js ${nodeName} ${queuedResourceName} ${zone} ${tpuType} ${tpuSoftwareVersion}`, + `node ./queuedResources/createQueuedResource.js ${nodeName} ${queuedResourceName} ${zone} ${tpuType} ${tpuSoftwareVersion}`, { cwd, } @@ -73,21 +73,32 @@ describe('TPU queued resource', () => { it('should return requested queued resource', () => { const response = execSync( - `node ./queuedResource/getQueuedResource.js ${queuedResourceName} ${zone}`, + `node ./queuedResources/getQueuedResource.js ${queuedResourceName} ${zone}`, { cwd, } ); + assert( response.includes(`Queued resource ${queuedResourceName} retrived.`) ); }); + it('should return list of queued resources', () => { + const response = JSON.parse( + execSync(`node ./queuedResources/getQueuedResourcesList.js ${zone}`, { + cwd, + }) + ); + + assert(Array.isArray(response)); + }); + it('should delete queued resource', async () => { // Wait until queued resource is ready to delete. await waitForTPUCreation(nodeName, zone); const response = execSync( - `node ./queuedResource/deleteQueuedResource.js ${queuedResourceName} ${zone}`, + `node ./queuedResources/deleteQueuedResource.js ${queuedResourceName} ${zone}`, { cwd, }