Skip to content

Commit 0ca1215

Browse files
authored
Merge branch 'main' into aml
2 parents d0873bf + 2a47751 commit 0ca1215

File tree

5 files changed

+258
-137
lines changed

5 files changed

+258
-137
lines changed

compute/disks/createComputeHyperdiskFromPool.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ async function main() {
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.
36-
const zone = 'europe-central2-b';
36+
const zone = 'us-central1-a';
3737
// The name of the new disk
38-
const diskName = 'disk-name-from-pool';
38+
const diskName = 'disk-from-pool-name';
3939
// The name of the storage pool
40-
const storagePoolName = 'storage-pool-name-hyperdisk';
40+
const 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}`;
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
async function main() {
20+
// [START compute_reservation_create]
21+
// Import the Compute library
22+
const computeLib = require('@google-cloud/compute');
23+
const compute = computeLib.protos.google.cloud.compute.v1;
24+
25+
// Instantiate a reservationsClient
26+
const reservationsClient = new computeLib.ReservationsClient();
27+
// Instantiate a zoneOperationsClient
28+
const zoneOperationsClient = new computeLib.ZoneOperationsClient();
29+
30+
/**
31+
* TODO(developer): Update these variables before running the sample.
32+
*/
33+
// The ID of the project where you want to reserve resources and where the instance template exists.
34+
const projectId = await reservationsClient.getProjectId();
35+
// The zone in which to reserve resources.
36+
const zone = 'us-central1-a';
37+
// The name of the reservation to create.
38+
const reservationName = 'reservation-01';
39+
// The number of VMs to reserve.
40+
const vmsNumber = 3;
41+
// Machine type to use for each VM.
42+
const machineType = 'n1-standard-4';
43+
44+
async function callCreateComputeReservationFromProperties() {
45+
// Create specific reservation for 3 VMs that each use an N1 predefined machine type with 4 vCPUs.
46+
const specificReservation = new compute.AllocationSpecificSKUReservation({
47+
count: vmsNumber,
48+
instanceProperties: {
49+
machineType,
50+
// To have the reserved VMs use a specific minimum CPU platform instead of the zone's default CPU platform.
51+
minCpuPlatform: 'Intel Skylake',
52+
// If you want to attach GPUs to your reserved N1 VMs, update and uncomment guestAccelerators if needed.
53+
guestAccelerators: [
54+
{
55+
// The number of GPUs to add per reserved VM.
56+
acceleratorCount: 1,
57+
// Supported GPU model for N1 VMs. Ensure that your chosen GPU model is available in the zone,
58+
// where you want to reserve resources.
59+
acceleratorType: 'nvidia-tesla-t4',
60+
},
61+
],
62+
// If you want to add local SSD disks to each reserved VM, update and uncomment localSsds if needed.
63+
// You can specify up to 24 Local SSD disks. Each Local SSD disk is 375 GB.
64+
localSsds: [
65+
{
66+
diskSizeGb: 375,
67+
// The type of interface you want each Local SSD disk to use. Specify one of the following values: NVME or SCSI.
68+
// Make sure that the machine type you specify for the reserved VMs supports the chosen disk interfaces.
69+
interface: 'NVME',
70+
},
71+
],
72+
},
73+
});
74+
75+
// Create a reservation.
76+
const reservation = new compute.Reservation({
77+
name: reservationName,
78+
zone,
79+
specificReservation,
80+
});
81+
82+
const [response] = await reservationsClient.insert({
83+
project: projectId,
84+
reservationResource: reservation,
85+
zone,
86+
});
87+
88+
let operation = response.latestResponse;
89+
90+
// Wait for the create reservation operation to complete.
91+
while (operation.status !== 'DONE') {
92+
[operation] = await zoneOperationsClient.wait({
93+
operation: operation.name,
94+
project: projectId,
95+
zone: operation.zone.split('/').pop(),
96+
});
97+
}
98+
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));
108+
}
109+
110+
await callCreateComputeReservationFromProperties();
111+
// [END compute_reservation_create]
112+
}
113+
114+
main().catch(err => {
115+
console.error(err);
116+
process.exitCode = 1;
117+
});

compute/test/createComputeHyperdiskFromPool.test.js

Lines changed: 55 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -20,96 +20,82 @@ const path = require('path');
2020
const {assert} = require('chai');
2121
const {after, before, describe, it} = require('mocha');
2222
const cp = require('child_process');
23-
const {DisksClient, StoragePoolsClient} = require('@google-cloud/compute').v1;
23+
const {DisksClient, StoragePoolsClient, ZoneOperationsClient} =
24+
require('@google-cloud/compute').v1;
2425

2526
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2627
const cwd = path.join(__dirname, '..');
2728

28-
describe('Create compute hyperdisk from pool', async () => {
29-
const diskName = 'disk-name-from-pool';
30-
const zone = 'europe-central2-b';
31-
const storagePoolName = 'storage-pool-name-hyperdisk';
29+
async function cleanupResources(projectId, zone, diskName, storagePoolName) {
3230
const disksClient = new DisksClient();
3331
const storagePoolsClient = new StoragePoolsClient();
32+
const zoneOperationsClient = new ZoneOperationsClient();
33+
// Delete disk attached to storagePool
34+
const [diskResponse] = await disksClient.delete({
35+
project: projectId,
36+
disk: diskName,
37+
zone,
38+
});
39+
40+
let diskOperation = diskResponse.latestResponse;
41+
42+
// Wait for the delete disk operation to complete.
43+
while (diskOperation.status !== 'DONE') {
44+
[diskOperation] = await zoneOperationsClient.wait({
45+
operation: diskOperation.name,
46+
project: projectId,
47+
zone: diskOperation.zone.split('/').pop(),
48+
});
49+
}
50+
51+
const [poolResponse] = await storagePoolsClient.delete({
52+
project: projectId,
53+
storagePool: storagePoolName,
54+
zone,
55+
});
56+
let poolOperation = poolResponse.latestResponse;
57+
58+
// Wait for the delete pool operation to complete.
59+
while (poolOperation.status !== 'DONE') {
60+
[poolOperation] = await zoneOperationsClient.wait({
61+
operation: poolOperation.name,
62+
project: projectId,
63+
zone: poolOperation.zone.split('/').pop(),
64+
});
65+
}
66+
}
67+
68+
describe('Create compute hyperdisk from pool', async () => {
69+
const diskName = 'disk-from-pool-name';
70+
const zone = 'us-central1-a';
71+
const storagePoolName = 'storage-pool-name';
72+
const disksClient = new DisksClient();
3473
let projectId;
3574

3675
before(async () => {
3776
projectId = await disksClient.getProjectId();
3877

3978
// Ensure resources are deleted before attempting to recreate them
4079
try {
41-
await disksClient.delete({
42-
project: projectId,
43-
disk: diskName,
44-
zone,
45-
});
46-
} catch (err) {
47-
// Should be ok to ignore (resource doesn't exist)
48-
console.error(err);
49-
}
50-
51-
try {
52-
await storagePoolsClient.delete({
53-
project: projectId,
54-
storagePool: storagePoolName,
55-
zone,
56-
});
80+
await cleanupResources(projectId, zone, diskName, storagePoolName);
5781
} catch (err) {
58-
// Should be ok to ignore (resource doesn't exist)
82+
// Should be ok to ignore (resources do not exist)
5983
console.error(err);
6084
}
61-
62-
await storagePoolsClient.insert({
63-
project: projectId,
64-
storagePoolResource: {
65-
name: storagePoolName,
66-
poolProvisionedCapacityGb: 10240,
67-
poolProvisionedIops: 10000,
68-
poolProvisionedThroughput: 1024,
69-
storagePoolType: `projects/${projectId}/zones/${zone}/storagePoolTypes/hyperdisk-balanced`,
70-
capacityProvisioningType: 'advanced',
71-
zone,
72-
},
73-
zone,
74-
});
7585
});
7686

7787
after(async () => {
78-
// Trying to delete the disk too quickly seems to fail
79-
const deleteDisk = async () => {
80-
setTimeout(async () => {
81-
await disksClient.delete({
82-
project: projectId,
83-
disk: diskName,
84-
zone,
85-
});
86-
}, 120 * 1000); // wait two minutes
87-
};
88-
89-
try {
90-
await deleteDisk();
91-
} catch {
92-
// Try one more time after repeating the delay
93-
await deleteDisk();
94-
}
88+
await cleanupResources(projectId, zone, diskName, storagePoolName);
89+
});
9590

96-
// Need enough time after removing the disk before removing the pool
97-
const deletePool = async () => {
98-
setTimeout(async () => {
99-
await storagePoolsClient.delete({
100-
project: projectId,
101-
storagePool: storagePoolName,
102-
zone,
103-
});
104-
}, 120 * 1000); // wait two minutes
105-
};
91+
it('should create a new storage pool', () => {
92+
const response = JSON.parse(
93+
execSync('node ./disks/createComputeHyperdiskPool.js', {
94+
cwd,
95+
})
96+
);
10697

107-
try {
108-
await deletePool();
109-
} catch {
110-
// Try one more time after repeating the delay
111-
await deletePool();
112-
}
98+
assert.equal(response.name, storagePoolName);
11399
});
114100

115101
it('should create a new hyperdisk from pool', () => {

compute/test/createComputeHyperdiskPool.test.js

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)