Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: Refactor for compute samples and their tests #3868

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions compute/disks/createComputeHyperdisk.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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"
Expand Down Expand Up @@ -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;
});
20 changes: 6 additions & 14 deletions compute/disks/createComputeHyperdiskFromPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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}`;
Expand Down Expand Up @@ -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;
});
18 changes: 5 additions & 13 deletions compute/disks/createComputeHyperdiskPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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`;
Expand Down Expand Up @@ -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;
});
18 changes: 5 additions & 13 deletions compute/reservations/createReservationFromProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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.
Expand Down Expand Up @@ -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;
});
16 changes: 4 additions & 12 deletions compute/reservations/createReservationInstanceTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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;

Expand Down Expand Up @@ -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();
Expand Down
10 changes: 6 additions & 4 deletions compute/reservations/deleteReservation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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
Expand All @@ -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;
});
8 changes: 4 additions & 4 deletions compute/reservations/getReservation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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 = (
Expand All @@ -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;
});
18 changes: 5 additions & 13 deletions compute/reservations/reservationVmsUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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;
Expand All @@ -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;
});
Loading