Skip to content

Commit 6e61e3d

Browse files
authored
improvement: Refactor for compute samples and their tests (#3868)
* improvement: Refactor for compute samples and their tests * removed chai module
1 parent c1643a1 commit 6e61e3d

13 files changed

+89
-203
lines changed

compute/disks/createComputeHyperdisk.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
'use strict';
1818

19-
async function main() {
19+
async function main(diskName) {
2020
// [START compute_hyperdisk_create]
2121
// Import the Compute library
2222
const computeLib = require('@google-cloud/compute');
@@ -28,14 +28,14 @@ async function main() {
2828
const zoneOperationsClient = new computeLib.ZoneOperationsClient();
2929

3030
/**
31-
* TODO(developer): Update these variables before running the sample.
31+
* TODO(developer): Update/uncomment these variables before running the sample.
3232
*/
3333
// Project ID or project number of the Google Cloud project you want to use.
3434
const projectId = await disksClient.getProjectId();
3535
// The zone where your VM and new disk are located.
3636
const zone = 'europe-central2-b';
3737
// The name of the new disk
38-
const diskName = 'disk-name';
38+
// diskName = 'disk-name';
3939
// The type of disk. This value uses the following format:
4040
// "zones/{zone}/diskTypes/(hyperdisk-balanced|hyperdisk-extreme|hyperdisk-ml|hyperdisk-throughput)".
4141
// For example: "zones/us-west3-b/diskTypes/hyperdisk-balanced"
@@ -78,22 +78,14 @@ async function main() {
7878
});
7979
}
8080

81-
const hyperdisk = (
82-
await disksClient.get({
83-
project: projectId,
84-
zone,
85-
disk: diskName,
86-
})
87-
)[0];
88-
89-
console.log(JSON.stringify(hyperdisk));
81+
console.log(`Disk: ${diskName} created.`);
9082
}
9183

9284
await callCreateComputeHyperdisk();
9385
// [END compute_hyperdisk_create]
9486
}
9587

96-
main().catch(err => {
88+
main(...process.argv.slice(2)).catch(err => {
9789
console.error(err);
9890
process.exitCode = 1;
9991
});

compute/disks/createComputeHyperdiskFromPool.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
'use strict';
1818

19-
async function main() {
19+
async function main(diskName, storagePoolName) {
2020
// [START compute_hyperdisk_create_from_pool]
2121
// Import the Compute library
2222
const computeLib = require('@google-cloud/compute');
@@ -28,16 +28,16 @@ async function main() {
2828
const zoneOperationsClient = new computeLib.ZoneOperationsClient();
2929

3030
/**
31-
* TODO(developer): Update these variables before running the sample.
31+
* TODO(developer): Update/uncomment these variables before running the sample.
3232
*/
3333
// Project ID or project number of the Google Cloud project you want to use.
3434
const projectId = await disksClient.getProjectId();
3535
// The zone where your VM and new disk are located.
3636
const zone = 'us-central1-a';
3737
// The name of the new disk
38-
const diskName = 'disk-from-pool-name';
38+
// diskName = 'disk-from-pool-name';
3939
// The name of the storage pool
40-
const storagePoolName = 'storage-pool-name';
40+
// storagePoolName = 'storage-pool-name';
4141
// Link to the storagePool you want to use. Use format:
4242
// https://www.googleapis.com/compute/v1/projects/{projectId}/zones/{zone}/storagePools/{storagePoolName}
4343
const storagePool = `https://www.googleapis.com/compute/v1/projects/${projectId}/zones/${zone}/storagePools/${storagePoolName}`;
@@ -84,22 +84,14 @@ async function main() {
8484
});
8585
}
8686

87-
const hyperdisk = (
88-
await disksClient.get({
89-
project: projectId,
90-
zone,
91-
disk: diskName,
92-
})
93-
)[0];
94-
95-
console.log(JSON.stringify(hyperdisk));
87+
console.log(`Disk: ${diskName} created.`);
9688
}
9789

9890
await callCreateComputeHyperdiskFromPool();
9991
// [END compute_hyperdisk_create_from_pool]
10092
}
10193

102-
main().catch(err => {
94+
main(...process.argv.slice(2)).catch(err => {
10395
console.error(err);
10496
process.exitCode = 1;
10597
});

compute/disks/createComputeHyperdiskPool.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
'use strict';
1818

19-
async function main() {
19+
async function main(storagePoolName) {
2020
// [START compute_hyperdisk_pool_create]
2121
// Import the Compute library
2222
const computeLib = require('@google-cloud/compute');
@@ -28,14 +28,14 @@ async function main() {
2828
const zoneOperationsClient = new computeLib.ZoneOperationsClient();
2929

3030
/**
31-
* TODO(developer): Update these variables before running the sample.
31+
* TODO(developer): Update/uncomment these variables before running the sample.
3232
*/
3333
// Project ID or project number of the Google Cloud project you want to use.
3434
const projectId = await storagePoolClient.getProjectId();
3535
// Name of the zone in which you want to create the storagePool.
3636
const zone = 'us-central1-a';
3737
// Name of the storagePool you want to create.
38-
const storagePoolName = 'storage-pool-name';
38+
// storagePoolName = 'storage-pool-name';
3939
// The type of disk you want to create. This value uses the following format:
4040
// "projects/{projectId}/zones/{zone}/storagePoolTypes/(hyperdisk-throughput|hyperdisk-balanced)"
4141
const storagePoolType = `projects/${projectId}/zones/${zone}/storagePoolTypes/hyperdisk-balanced`;
@@ -79,22 +79,14 @@ async function main() {
7979
});
8080
}
8181

82-
const createdStoragePool = (
83-
await storagePoolClient.get({
84-
project: projectId,
85-
zone,
86-
storagePool: storagePoolName,
87-
})
88-
)[0];
89-
90-
console.log(JSON.stringify(createdStoragePool));
82+
console.log(`Storage pool: ${storagePoolName} created.`);
9183
}
9284

9385
await callCreateComputeHyperdiskPool();
9486
// [END compute_hyperdisk_pool_create]
9587
}
9688

97-
main().catch(err => {
89+
main(...process.argv.slice(2)).catch(err => {
9890
console.error(err);
9991
process.exitCode = 1;
10092
});

compute/reservations/createReservationFromProperties.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
'use strict';
1818

19-
async function main() {
19+
async function main(reservationName) {
2020
// [START compute_reservation_create]
2121
// Import the Compute library
2222
const computeLib = require('@google-cloud/compute');
@@ -28,14 +28,14 @@ async function main() {
2828
const zoneOperationsClient = new computeLib.ZoneOperationsClient();
2929

3030
/**
31-
* TODO(developer): Update these variables before running the sample.
31+
* TODO(developer): Update/uncomment these variables before running the sample.
3232
*/
3333
// The ID of the project where you want to reserve resources.
3434
const projectId = await reservationsClient.getProjectId();
3535
// The zone in which to reserve resources.
3636
const zone = 'us-central1-a';
3737
// The name of the reservation to create.
38-
const reservationName = 'reservation-01';
38+
// reservationName = 'reservation-01';
3939
// The number of VMs to reserve.
4040
const vmsNumber = 3;
4141
// Machine type to use for each VM.
@@ -96,22 +96,14 @@ async function main() {
9696
});
9797
}
9898

99-
const createdReservation = (
100-
await reservationsClient.get({
101-
project: projectId,
102-
zone,
103-
reservation: reservationName,
104-
})
105-
)[0];
106-
107-
console.log(JSON.stringify(createdReservation));
99+
console.log(`Reservation: ${reservationName} created.`);
108100
}
109101

110102
await callCreateComputeReservationFromProperties();
111103
// [END compute_reservation_create]
112104
}
113105

114-
main().catch(err => {
106+
main(...process.argv.slice(2)).catch(err => {
115107
console.error(err);
116108
process.exitCode = 1;
117109
});

compute/reservations/createReservationInstanceTemplate.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
'use strict';
1818

19-
async function main(location, instanceTemplateName) {
19+
async function main(reservationName, location, instanceTemplateName) {
2020
// [START compute_reservation_create_template]
2121
// Import the Compute library
2222
const computeLib = require('@google-cloud/compute');
@@ -28,14 +28,14 @@ async function main(location, instanceTemplateName) {
2828
const zoneOperationsClient = new computeLib.ZoneOperationsClient();
2929

3030
/**
31-
* TODO(developer): Update these variables before running the sample.
31+
* TODO(developer): Update/uncomment these variables before running the sample.
3232
*/
3333
// The ID of the project where you want to reserve resources and where the instance template exists.
3434
const projectId = await reservationsClient.getProjectId();
3535
// The zone in which to reserve resources.
3636
const zone = 'us-central1-a';
3737
// The name of the reservation to create.
38-
const reservationName = 'reservation-01';
38+
// reservationName = 'reservation-01';
3939
// The number of VMs to reserve.
4040
const vmsNumber = 3;
4141

@@ -87,15 +87,7 @@ async function main(location, instanceTemplateName) {
8787
});
8888
}
8989

90-
const createdReservation = (
91-
await reservationsClient.get({
92-
project: projectId,
93-
zone,
94-
reservation: reservationName,
95-
})
96-
)[0];
97-
98-
console.log(JSON.stringify(createdReservation));
90+
console.log(`Reservation: ${reservationName} created.`);
9991
}
10092

10193
await callCreateComputeReservationInstanceTemplate();

compute/reservations/deleteReservation.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
'use strict';
17-
async function main() {
17+
async function main(reservationName) {
1818
// [START compute_reservation_delete]
1919
// Import the Compute library
2020
const computeLib = require('@google-cloud/compute');
@@ -23,14 +23,14 @@ async function main() {
2323
// Instantiate a zoneOperationsClient
2424
const zoneOperationsClient = new computeLib.ZoneOperationsClient();
2525
/**
26-
* TODO(developer): Update these variables before running the sample.
26+
* TODO(developer): Update/uncomment these variables before running the sample.
2727
*/
2828
// The ID of the project where your reservation is located.
2929
const projectId = await reservationsClient.getProjectId();
3030
// The zone where your reservation is located.
3131
const zone = 'us-central1-a';
3232
// The name of the reservation to delete.
33-
const reservationName = 'reservation-01';
33+
// reservationName = 'reservation-01';
3434

3535
async function callDeleteReservation() {
3636
// Delete the reservation
@@ -50,11 +50,13 @@ async function main() {
5050
zone: operation.zone.split('/').pop(),
5151
});
5252
}
53+
54+
console.log(`Reservation: ${reservationName} deleted.`);
5355
}
5456
await callDeleteReservation();
5557
// [END compute_reservation_delete]
5658
}
57-
main().catch(err => {
59+
main(...process.argv.slice(2)).catch(err => {
5860
console.error(err);
5961
process.exitCode = 1;
6062
});

compute/reservations/getReservation.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
'use strict';
1818

19-
async function main() {
19+
async function main(reservationName) {
2020
// [START compute_reservation_get]
2121
// Import the Compute library
2222
const computeLib = require('@google-cloud/compute');
@@ -25,14 +25,14 @@ async function main() {
2525
const reservationsClient = new computeLib.ReservationsClient();
2626

2727
/**
28-
* TODO(developer): Update these variables before running the sample.
28+
* TODO(developer): Update/uncomment these variables before running the sample.
2929
*/
3030
// The ID of the project where your reservation is located.
3131
const projectId = await reservationsClient.getProjectId();
3232
// The zone where your reservation is located.
3333
const zone = 'us-central1-a';
3434
// The name of the reservation to return.
35-
const reservationName = 'reservation-01';
35+
// reservationName = 'reservation-01';
3636

3737
async function callGetReservation() {
3838
const requestedReservation = (
@@ -50,7 +50,7 @@ async function main() {
5050
// [END compute_reservation_get]
5151
}
5252

53-
main().catch(err => {
53+
main(...process.argv.slice(2)).catch(err => {
5454
console.error(err);
5555
process.exitCode = 1;
5656
});

compute/reservations/reservationVmsUpdate.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
'use strict';
1818

19-
async function main() {
19+
async function main(reservationName) {
2020
// [START compute_reservation_vms_update]
2121
// Import the Compute library
2222
const computeLib = require('@google-cloud/compute');
@@ -27,14 +27,14 @@ async function main() {
2727
const zoneOperationsClient = new computeLib.ZoneOperationsClient();
2828

2929
/**
30-
* TODO(developer): Update these variables before running the sample.
30+
* TODO(developer): Update/uncomment these variables before running the sample.
3131
*/
3232
// The ID of the project where the reservation is located.
3333
const projectId = await reservationsClient.getProjectId();
3434
// The zone where the reservation is located.
3535
const zone = 'us-central1-a';
3636
// The name of an existing reservation.
37-
const reservationName = 'reservation-01';
37+
// reservationName = 'reservation-01';
3838
// The new number of VMs to reserve(increase or decrease the number). Before modifying the number of VMs,
3939
// ensure that all required conditions are met. See: https://cloud.google.com/compute/docs/instances/reservations-modify#resizing_a_reservation.
4040
const vmsNumber = 1;
@@ -61,22 +61,14 @@ async function main() {
6161
});
6262
}
6363

64-
const updatedReservation = (
65-
await reservationsClient.get({
66-
project: projectId,
67-
zone,
68-
reservation: reservationName,
69-
})
70-
)[0];
71-
72-
console.log(JSON.stringify(updatedReservation));
64+
console.log(`Reservation: ${reservationName} updated.`);
7365
}
7466

7567
await callComputeReservationVmsUpdate();
7668
// [END compute_reservation_vms_update]
7769
}
7870

79-
main().catch(err => {
71+
main(...process.argv.slice(2)).catch(err => {
8072
console.error(err);
8173
process.exitCode = 1;
8274
});

0 commit comments

Comments
 (0)