diff --git a/compute/disks/createComputeHyperdisk.js b/compute/disks/createComputeHyperdisk.js index 76b231eded..e46c04c030 100644 --- a/compute/disks/createComputeHyperdisk.js +++ b/compute/disks/createComputeHyperdisk.js @@ -16,7 +16,7 @@ 'use strict'; -async function main() { +async function main(diskName) { // [START compute_hyperdisk_create] // Import the Compute library const computeLib = require('@google-cloud/compute'); @@ -28,14 +28,14 @@ async function main() { const zoneOperationsClient = new computeLib.ZoneOperationsClient(); /** - * TODO(developer): Update these variables before running the sample. + * TODO(developer): Update/uncomment these variables before running the sample. */ // Project ID or project number of the Google Cloud project you want to use. const projectId = await disksClient.getProjectId(); // The zone where your VM and new disk are located. const zone = 'europe-central2-b'; // The name of the new disk - const diskName = 'disk-name'; + // diskName = 'disk-name'; // The type of disk. This value uses the following format: // "zones/{zone}/diskTypes/(hyperdisk-balanced|hyperdisk-extreme|hyperdisk-ml|hyperdisk-throughput)". // For example: "zones/us-west3-b/diskTypes/hyperdisk-balanced" @@ -78,22 +78,14 @@ async function main() { }); } - const hyperdisk = ( - await disksClient.get({ - project: projectId, - zone, - disk: diskName, - }) - )[0]; - - console.log(JSON.stringify(hyperdisk)); + console.log(`Disk: ${diskName} created.`); } await callCreateComputeHyperdisk(); // [END compute_hyperdisk_create] } -main().catch(err => { +main(...process.argv.slice(2)).catch(err => { console.error(err); process.exitCode = 1; }); diff --git a/compute/disks/createComputeHyperdiskFromPool.js b/compute/disks/createComputeHyperdiskFromPool.js index 3a2c34d09d..01b8aa87aa 100644 --- a/compute/disks/createComputeHyperdiskFromPool.js +++ b/compute/disks/createComputeHyperdiskFromPool.js @@ -16,7 +16,7 @@ 'use strict'; -async function main() { +async function main(diskName, storagePoolName) { // [START compute_hyperdisk_create_from_pool] // Import the Compute library const computeLib = require('@google-cloud/compute'); @@ -28,16 +28,16 @@ async function main() { const zoneOperationsClient = new computeLib.ZoneOperationsClient(); /** - * TODO(developer): Update these variables before running the sample. + * TODO(developer): Update/uncomment these variables before running the sample. */ // Project ID or project number of the Google Cloud project you want to use. const projectId = await disksClient.getProjectId(); // The zone where your VM and new disk are located. const zone = 'us-central1-a'; // The name of the new disk - const diskName = 'disk-from-pool-name'; + // diskName = 'disk-from-pool-name'; // The name of the storage pool - const storagePoolName = 'storage-pool-name'; + // storagePoolName = 'storage-pool-name'; // Link to the storagePool you want to use. Use format: // https://www.googleapis.com/compute/v1/projects/{projectId}/zones/{zone}/storagePools/{storagePoolName} const storagePool = `https://www.googleapis.com/compute/v1/projects/${projectId}/zones/${zone}/storagePools/${storagePoolName}`; @@ -84,22 +84,14 @@ async function main() { }); } - const hyperdisk = ( - await disksClient.get({ - project: projectId, - zone, - disk: diskName, - }) - )[0]; - - console.log(JSON.stringify(hyperdisk)); + console.log(`Disk: ${diskName} created.`); } await callCreateComputeHyperdiskFromPool(); // [END compute_hyperdisk_create_from_pool] } -main().catch(err => { +main(...process.argv.slice(2)).catch(err => { console.error(err); process.exitCode = 1; }); diff --git a/compute/disks/createComputeHyperdiskPool.js b/compute/disks/createComputeHyperdiskPool.js index d8ae457e6b..faa2e4b0db 100644 --- a/compute/disks/createComputeHyperdiskPool.js +++ b/compute/disks/createComputeHyperdiskPool.js @@ -16,7 +16,7 @@ 'use strict'; -async function main() { +async function main(storagePoolName) { // [START compute_hyperdisk_pool_create] // Import the Compute library const computeLib = require('@google-cloud/compute'); @@ -28,14 +28,14 @@ async function main() { const zoneOperationsClient = new computeLib.ZoneOperationsClient(); /** - * TODO(developer): Update these variables before running the sample. + * TODO(developer): Update/uncomment these variables before running the sample. */ // Project ID or project number of the Google Cloud project you want to use. const projectId = await storagePoolClient.getProjectId(); // Name of the zone in which you want to create the storagePool. const zone = 'us-central1-a'; // Name of the storagePool you want to create. - const storagePoolName = 'storage-pool-name'; + // storagePoolName = 'storage-pool-name'; // The type of disk you want to create. This value uses the following format: // "projects/{projectId}/zones/{zone}/storagePoolTypes/(hyperdisk-throughput|hyperdisk-balanced)" const storagePoolType = `projects/${projectId}/zones/${zone}/storagePoolTypes/hyperdisk-balanced`; @@ -79,22 +79,14 @@ async function main() { }); } - const createdStoragePool = ( - await storagePoolClient.get({ - project: projectId, - zone, - storagePool: storagePoolName, - }) - )[0]; - - console.log(JSON.stringify(createdStoragePool)); + console.log(`Storage pool: ${storagePoolName} created.`); } await callCreateComputeHyperdiskPool(); // [END compute_hyperdisk_pool_create] } -main().catch(err => { +main(...process.argv.slice(2)).catch(err => { console.error(err); process.exitCode = 1; }); diff --git a/compute/reservations/createReservationFromProperties.js b/compute/reservations/createReservationFromProperties.js index 42e23755e8..e4f00a1e59 100644 --- a/compute/reservations/createReservationFromProperties.js +++ b/compute/reservations/createReservationFromProperties.js @@ -16,7 +16,7 @@ 'use strict'; -async function main() { +async function main(reservationName) { // [START compute_reservation_create] // Import the Compute library const computeLib = require('@google-cloud/compute'); @@ -28,14 +28,14 @@ async function main() { const zoneOperationsClient = new computeLib.ZoneOperationsClient(); /** - * TODO(developer): Update these variables before running the sample. + * TODO(developer): Update/uncomment these variables before running the sample. */ // The ID of the project where you want to reserve resources. const projectId = await reservationsClient.getProjectId(); // The zone in which to reserve resources. const zone = 'us-central1-a'; // The name of the reservation to create. - const reservationName = 'reservation-01'; + // reservationName = 'reservation-01'; // The number of VMs to reserve. const vmsNumber = 3; // Machine type to use for each VM. @@ -96,22 +96,14 @@ async function main() { }); } - const createdReservation = ( - await reservationsClient.get({ - project: projectId, - zone, - reservation: reservationName, - }) - )[0]; - - console.log(JSON.stringify(createdReservation)); + console.log(`Reservation: ${reservationName} created.`); } await callCreateComputeReservationFromProperties(); // [END compute_reservation_create] } -main().catch(err => { +main(...process.argv.slice(2)).catch(err => { console.error(err); process.exitCode = 1; }); diff --git a/compute/reservations/createReservationInstanceTemplate.js b/compute/reservations/createReservationInstanceTemplate.js index 796bbc6064..ad9236e7f7 100644 --- a/compute/reservations/createReservationInstanceTemplate.js +++ b/compute/reservations/createReservationInstanceTemplate.js @@ -16,7 +16,7 @@ 'use strict'; -async function main(location, instanceTemplateName) { +async function main(reservationName, location, instanceTemplateName) { // [START compute_reservation_create_template] // Import the Compute library const computeLib = require('@google-cloud/compute'); @@ -28,14 +28,14 @@ async function main(location, instanceTemplateName) { const zoneOperationsClient = new computeLib.ZoneOperationsClient(); /** - * TODO(developer): Update these variables before running the sample. + * TODO(developer): Update/uncomment these variables before running the sample. */ // The ID of the project where you want to reserve resources and where the instance template exists. const projectId = await reservationsClient.getProjectId(); // The zone in which to reserve resources. const zone = 'us-central1-a'; // The name of the reservation to create. - const reservationName = 'reservation-01'; + // reservationName = 'reservation-01'; // The number of VMs to reserve. const vmsNumber = 3; @@ -87,15 +87,7 @@ async function main(location, instanceTemplateName) { }); } - const createdReservation = ( - await reservationsClient.get({ - project: projectId, - zone, - reservation: reservationName, - }) - )[0]; - - console.log(JSON.stringify(createdReservation)); + console.log(`Reservation: ${reservationName} created.`); } await callCreateComputeReservationInstanceTemplate(); diff --git a/compute/reservations/deleteReservation.js b/compute/reservations/deleteReservation.js index 0c13b49f11..5d3c0956b9 100644 --- a/compute/reservations/deleteReservation.js +++ b/compute/reservations/deleteReservation.js @@ -14,7 +14,7 @@ * limitations under the License. */ 'use strict'; -async function main() { +async function main(reservationName) { // [START compute_reservation_delete] // Import the Compute library const computeLib = require('@google-cloud/compute'); @@ -23,14 +23,14 @@ async function main() { // Instantiate a zoneOperationsClient const zoneOperationsClient = new computeLib.ZoneOperationsClient(); /** - * TODO(developer): Update these variables before running the sample. + * TODO(developer): Update/uncomment these variables before running the sample. */ // The ID of the project where your reservation is located. const projectId = await reservationsClient.getProjectId(); // The zone where your reservation is located. const zone = 'us-central1-a'; // The name of the reservation to delete. - const reservationName = 'reservation-01'; + // reservationName = 'reservation-01'; async function callDeleteReservation() { // Delete the reservation @@ -50,11 +50,13 @@ async function main() { zone: operation.zone.split('/').pop(), }); } + + console.log(`Reservation: ${reservationName} deleted.`); } await callDeleteReservation(); // [END compute_reservation_delete] } -main().catch(err => { +main(...process.argv.slice(2)).catch(err => { console.error(err); process.exitCode = 1; }); diff --git a/compute/reservations/getReservation.js b/compute/reservations/getReservation.js index 733090ae6d..a57836083e 100644 --- a/compute/reservations/getReservation.js +++ b/compute/reservations/getReservation.js @@ -16,7 +16,7 @@ 'use strict'; -async function main() { +async function main(reservationName) { // [START compute_reservation_get] // Import the Compute library const computeLib = require('@google-cloud/compute'); @@ -25,14 +25,14 @@ async function main() { const reservationsClient = new computeLib.ReservationsClient(); /** - * TODO(developer): Update these variables before running the sample. + * TODO(developer): Update/uncomment these variables before running the sample. */ // The ID of the project where your reservation is located. const projectId = await reservationsClient.getProjectId(); // The zone where your reservation is located. const zone = 'us-central1-a'; // The name of the reservation to return. - const reservationName = 'reservation-01'; + // reservationName = 'reservation-01'; async function callGetReservation() { const requestedReservation = ( @@ -50,7 +50,7 @@ async function main() { // [END compute_reservation_get] } -main().catch(err => { +main(...process.argv.slice(2)).catch(err => { console.error(err); process.exitCode = 1; }); diff --git a/compute/reservations/reservationVmsUpdate.js b/compute/reservations/reservationVmsUpdate.js index 24305367f7..629075f365 100644 --- a/compute/reservations/reservationVmsUpdate.js +++ b/compute/reservations/reservationVmsUpdate.js @@ -16,7 +16,7 @@ 'use strict'; -async function main() { +async function main(reservationName) { // [START compute_reservation_vms_update] // Import the Compute library const computeLib = require('@google-cloud/compute'); @@ -27,14 +27,14 @@ async function main() { const zoneOperationsClient = new computeLib.ZoneOperationsClient(); /** - * TODO(developer): Update these variables before running the sample. + * TODO(developer): Update/uncomment these variables before running the sample. */ // The ID of the project where the reservation is located. const projectId = await reservationsClient.getProjectId(); // The zone where the reservation is located. const zone = 'us-central1-a'; // The name of an existing reservation. - const reservationName = 'reservation-01'; + // reservationName = 'reservation-01'; // The new number of VMs to reserve(increase or decrease the number). Before modifying the number of VMs, // ensure that all required conditions are met. See: https://cloud.google.com/compute/docs/instances/reservations-modify#resizing_a_reservation. const vmsNumber = 1; @@ -61,22 +61,14 @@ async function main() { }); } - const updatedReservation = ( - await reservationsClient.get({ - project: projectId, - zone, - reservation: reservationName, - }) - )[0]; - - console.log(JSON.stringify(updatedReservation)); + console.log(`Reservation: ${reservationName} updated.`); } await callComputeReservationVmsUpdate(); // [END compute_reservation_vms_update] } -main().catch(err => { +main(...process.argv.slice(2)).catch(err => { console.error(err); process.exitCode = 1; }); diff --git a/compute/test/createComputeHyperdisk.test.js b/compute/test/createComputeHyperdisk.test.js index ac4721f764..3c098c98e4 100644 --- a/compute/test/createComputeHyperdisk.test.js +++ b/compute/test/createComputeHyperdisk.test.js @@ -17,7 +17,7 @@ 'use strict'; const path = require('path'); -const {assert} = require('chai'); +const assert = require('node:assert/strict'); const {before, after, describe, it} = require('mocha'); const cp = require('child_process'); const {DisksClient} = require('@google-cloud/compute').v1; @@ -26,23 +26,13 @@ const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); const cwd = path.join(__dirname, '..'); describe('Create compute hyperdisk', async () => { - const diskName = 'disk-name'; + const diskName = `hyperdisk-name-941ad2d${Math.floor(Math.random() * 1000 + 1)}`; const zone = 'europe-central2-b'; const disksClient = new DisksClient(); let projectId; before(async () => { projectId = await disksClient.getProjectId(); - try { - // Ensure resource is deleted attempting to recreate it - await disksClient.delete({ - project: projectId, - disk: diskName, - zone, - }); - } catch { - // ok to ignore (resource doesn't exist) - } }); after(async () => { @@ -54,12 +44,13 @@ describe('Create compute hyperdisk', async () => { }); it('should create a new hyperdisk', () => { - const response = JSON.parse( - execSync('node ./disks/createComputeHyperdisk.js', { + const response = execSync( + `node ./disks/createComputeHyperdisk.js ${diskName}`, + { cwd, - }) + } ); - assert.equal(response.name, diskName); + assert(response.includes(`Disk: ${diskName} created.`)); }); }); diff --git a/compute/test/createComputeHyperdiskFromPool.test.js b/compute/test/createComputeHyperdiskFromPool.test.js index 5813a2158c..063b22c16b 100644 --- a/compute/test/createComputeHyperdiskFromPool.test.js +++ b/compute/test/createComputeHyperdiskFromPool.test.js @@ -17,7 +17,7 @@ 'use strict'; const path = require('path'); -const {assert} = require('chai'); +const assert = require('node:assert/strict'); const {after, before, describe, it} = require('mocha'); const cp = require('child_process'); const {DisksClient, StoragePoolsClient, ZoneOperationsClient} = @@ -66,22 +66,14 @@ async function cleanupResources(projectId, zone, diskName, storagePoolName) { } describe('Create compute hyperdisk from pool', async () => { - const diskName = 'disk-from-pool-name'; + const diskName = `disk-from-pool-name-745d98${Math.floor(Math.random() * 1000 + 1)}f`; const zone = 'us-central1-a'; - const storagePoolName = 'storage-pool-name'; + const storagePoolName = `storage-pool-name-745d9${Math.floor(Math.random() * 1000 + 1)}5f`; const disksClient = new DisksClient(); let projectId; before(async () => { projectId = await disksClient.getProjectId(); - - // Ensure resources are deleted before attempting to recreate them - try { - await cleanupResources(projectId, zone, diskName, storagePoolName); - } catch (err) { - // Should be ok to ignore (resources do not exist) - console.error(err); - } }); after(async () => { @@ -89,26 +81,24 @@ describe('Create compute hyperdisk from pool', async () => { }); it('should create a new storage pool', () => { - const response = JSON.parse( - execSync('node ./disks/createComputeHyperdiskPool.js', { + const response = execSync( + `node ./disks/createComputeHyperdiskPool.js ${storagePoolName}`, + { cwd, - }) + } ); - assert.equal(response.name, storagePoolName); + assert(response.includes(`Storage pool: ${storagePoolName} created.`)); }); it('should create a new hyperdisk from pool', () => { - const response = JSON.parse( - execSync('node ./disks/createComputeHyperdiskFromPool.js', { + const response = execSync( + `node ./disks/createComputeHyperdiskFromPool.js ${diskName} ${storagePoolName}`, + { cwd, - }) + } ); - assert.equal(response.name, diskName); - assert.equal( - response.storagePool, - `https://www.googleapis.com/compute/v1/projects/${projectId}/zones/${zone}/storagePools/${storagePoolName}` - ); + assert(response.includes(`Disk: ${diskName} created.`)); }); }); diff --git a/compute/test/createReservationGlobalInstanceTemplate.test.js b/compute/test/createReservationGlobalInstanceTemplate.test.js index df34b1ad56..aef50166c5 100644 --- a/compute/test/createReservationGlobalInstanceTemplate.test.js +++ b/compute/test/createReservationGlobalInstanceTemplate.test.js @@ -26,8 +26,8 @@ const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); const cwd = path.join(__dirname, '..'); describe('Create compute reservation using global instance template', async () => { - const reservationName = 'reservation-01'; - const instanceTemplateName = 'pernament-global-template-name'; + const reservationName = `global-reservation-68ef06a${Math.floor(Math.random() * 1000 + 1)}`; + const instanceTemplateName = `pernament-global-template-68ef06a${Math.floor(Math.random() * 1000 + 1)}`; const location = 'global'; const reservationsClient = new ReservationsClient(); let projectId; @@ -45,7 +45,7 @@ describe('Create compute reservation using global instance template', async () = after(() => { // Delete reservation - execSync('node ./reservations/deleteReservation.js', { + execSync(`node ./reservations/deleteReservation.js ${reservationName}`, { cwd, }); // Delete template @@ -58,20 +58,13 @@ describe('Create compute reservation using global instance template', async () = }); it('should create a new reservation', () => { - const response = JSON.parse( - execSync( - `node ./reservations/createReservationInstanceTemplate.js ${location} ${instanceTemplateName}`, - { - cwd, - } - ) + const response = execSync( + `node ./reservations/createReservationInstanceTemplate.js ${reservationName} ${location} ${instanceTemplateName}`, + { + cwd, + } ); - assert.equal(response.name, reservationName); - assert.equal(response.specificReservation.count, '3'); - assert.equal( - response.specificReservation.sourceInstanceTemplate, - `https://www.googleapis.com/compute/v1/projects/${projectId}/${location}/instanceTemplates/${instanceTemplateName}` - ); + assert(response.includes(`Reservation: ${reservationName} created.`)); }); }); diff --git a/compute/test/createReservationRegionalInstanceTemplate.test.js b/compute/test/createReservationRegionalInstanceTemplate.test.js index 76522e3e2b..5eccdb4dfb 100644 --- a/compute/test/createReservationRegionalInstanceTemplate.test.js +++ b/compute/test/createReservationRegionalInstanceTemplate.test.js @@ -18,46 +18,32 @@ const path = require('path'); const assert = require('node:assert/strict'); -const {after, before, describe, it} = require('mocha'); +const {after, describe, it} = require('mocha'); const cp = require('child_process'); -const {ReservationsClient} = require('@google-cloud/compute').v1; const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); const cwd = path.join(__dirname, '..'); describe('Create compute reservation using regional instance template', async () => { - const reservationName = 'reservation-01'; + const reservationName = `regional-reservation-04bf4ed${Math.floor(Math.random() * 1000 + 1)}`; const instanceTemplateName = 'pernament-region-template-name'; const location = 'regions/us-central1'; - const reservationsClient = new ReservationsClient(); - let projectId; - - before(async () => { - projectId = await reservationsClient.getProjectId(); - }); after(() => { // Delete reservation - execSync('node ./reservations/deleteReservation.js', { + execSync(`node ./reservations/deleteReservation.js ${reservationName}`, { cwd, }); }); it('should create a new reservation', () => { - const response = JSON.parse( - execSync( - `node ./reservations/createReservationInstanceTemplate.js ${location} ${instanceTemplateName}`, - { - cwd, - } - ) + const response = execSync( + `node ./reservations/createReservationInstanceTemplate.js ${reservationName} ${location} ${instanceTemplateName}`, + { + cwd, + } ); - assert.equal(response.name, reservationName); - assert.equal(response.specificReservation.count, '3'); - assert.equal( - response.specificReservation.sourceInstanceTemplate, - `https://www.googleapis.com/compute/v1/projects/${projectId}/${location}/instanceTemplates/${instanceTemplateName}` - ); + assert(response.includes(`Reservation: ${reservationName} created.`)); }); }); diff --git a/compute/test/reservations.test.js b/compute/test/reservations.test.js index 75715f020f..49e2d02cb7 100644 --- a/compute/test/reservations.test.js +++ b/compute/test/reservations.test.js @@ -18,7 +18,7 @@ const path = require('path'); const assert = require('node:assert/strict'); -const {describe, it} = require('mocha'); +const {before, describe, it} = require('mocha'); const cp = require('child_process'); const {ReservationsClient} = require('@google-cloud/compute').v1; @@ -26,62 +26,34 @@ const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); const cwd = path.join(__dirname, '..'); describe('Compute reservation', async () => { - const reservationName = 'reservation-01'; + const reservationName = `reservation-1a0bb${Math.floor(Math.random() * 1000 + 1)}`; const zone = 'us-central1-a'; const reservationsClient = new ReservationsClient(); let projectId; - let reservation; before(async () => { projectId = await reservationsClient.getProjectId(); }); it('should create a new reservation', () => { - const instanceProperties = { - _machineType: 'machineType', - _minCpuPlatform: 'minCpuPlatform', - guestAccelerators: [ - { - _acceleratorCount: 'acceleratorCount', - _acceleratorType: 'acceleratorType', - acceleratorCount: 1, - acceleratorType: 'nvidia-tesla-t4', - }, - ], - localSsds: [ - { - diskSizeGb: '375', - interface: 'NVME', - _diskSizeGb: 'diskSizeGb', - _interface: 'interface', - }, - ], - machineType: 'n1-standard-4', - minCpuPlatform: 'Intel Skylake', - }; - - reservation = JSON.parse( - execSync('node ./reservations/createReservationFromProperties.js', { + const response = execSync( + `node ./reservations/createReservationFromProperties.js ${reservationName}`, + { cwd, - }) + } ); - assert.equal(reservation.name, reservationName); - assert.equal(reservation.specificReservation.count, '3'); - assert.deepEqual( - reservation.specificReservation.instanceProperties, - instanceProperties - ); + assert(response.includes(`Reservation: ${reservationName} created.`)); }); it('should return reservation', () => { const response = JSON.parse( - execSync('node ./reservations/getReservation.js', { + execSync(`node ./reservations/getReservation.js ${reservationName}`, { cwd, }) ); - assert.deepEqual(response, reservation); + assert(response.name === reservationName); }); it('should return list of reservations', () => { @@ -91,11 +63,11 @@ describe('Compute reservation', async () => { }) ); - assert.deepEqual(response, [reservation]); + assert(Array.isArray(response)); }); it('should delete reservation', async () => { - execSync('node ./reservations/deleteReservation.js', { + execSync(`node ./reservations/deleteReservation.js ${reservationName}`, { cwd, });