Skip to content

Commit 2a47751

Browse files
authored
feat: compute reservation create (#3807)
* Changed debian-10 -> debian-11 * feat: compute_reservation_create
1 parent b01e844 commit 2a47751

File tree

5 files changed

+203
-3
lines changed

5 files changed

+203
-3
lines changed

compute/disks/createComputeHyperdiskFromPool.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ 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
3838
const diskName = 'disk-from-pool-name';
3939
// The name of the storage pool

compute/disks/createComputeHyperdiskPool.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async function main() {
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.
36-
const zone = 'europe-central2-b';
36+
const zone = 'us-central1-a';
3737
// Name of the storagePool you want to create.
3838
const storagePoolName = 'storage-pool-name';
3939
// The type of disk you want to create. This value uses the following format:
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ async function cleanupResources(projectId, zone, diskName, storagePoolName) {
6767

6868
describe('Create compute hyperdisk from pool', async () => {
6969
const diskName = 'disk-from-pool-name';
70-
const zone = 'europe-central2-b';
70+
const zone = 'us-central1-a';
7171
const storagePoolName = 'storage-pool-name';
7272
const disksClient = new DisksClient();
7373
let projectId;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
const path = require('path');
20+
const assert = require('node:assert/strict');
21+
const {after, before, describe, it} = require('mocha');
22+
const cp = require('child_process');
23+
const {ReservationsClient} = require('@google-cloud/compute').v1;
24+
25+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
26+
const cwd = path.join(__dirname, '..');
27+
28+
describe('Create compute reservation by specyfing properties directly', async () => {
29+
const reservationName = 'reservation-01';
30+
const zone = 'us-central1-a';
31+
const reservationsClient = new ReservationsClient();
32+
let projectId;
33+
34+
before(async () => {
35+
projectId = await reservationsClient.getProjectId();
36+
});
37+
38+
after(async () => {
39+
await reservationsClient.delete({
40+
project: projectId,
41+
reservation: reservationName,
42+
zone,
43+
});
44+
});
45+
46+
it('should create a new reservation', () => {
47+
const instanceProperties = {
48+
_machineType: 'machineType',
49+
_minCpuPlatform: 'minCpuPlatform',
50+
guestAccelerators: [
51+
{
52+
_acceleratorCount: 'acceleratorCount',
53+
_acceleratorType: 'acceleratorType',
54+
acceleratorCount: 1,
55+
acceleratorType: 'nvidia-tesla-t4',
56+
},
57+
],
58+
localSsds: [
59+
{
60+
diskSizeGb: '375',
61+
interface: 'NVME',
62+
_diskSizeGb: 'diskSizeGb',
63+
_interface: 'interface',
64+
},
65+
],
66+
machineType: 'n1-standard-4',
67+
minCpuPlatform: 'Intel Skylake',
68+
};
69+
70+
const response = JSON.parse(
71+
execSync('node ./reservations/createReservationFromProperties.js', {
72+
cwd,
73+
})
74+
);
75+
76+
assert.equal(response.name, reservationName);
77+
assert.equal(response.specificReservation.count, '3');
78+
assert.deepEqual(
79+
response.specificReservation.instanceProperties,
80+
instanceProperties
81+
);
82+
});
83+
});

0 commit comments

Comments
 (0)