Skip to content

Commit a9b6e6b

Browse files
author
Joanna Grycz
committed
feat: compute_consume_specific_shared_reservation
1 parent 5bd0c38 commit a9b6e6b

29 files changed

+2700
-2546
lines changed

compute/reservations/createInstanceToConsumeAnyReservation.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async function main(instanceName) {
2222
const computeLib = require('@google-cloud/compute');
2323
const compute = computeLib.protos.google.cloud.compute.v1;
2424

25-
// Instantiate a reservationsClient
25+
// Instantiate an instancesClient
2626
const instancesClient = new computeLib.InstancesClient();
2727
// Instantiate a zoneOperationsClient
2828
const zoneOperationsClient = new computeLib.ZoneOperationsClient();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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(instancesClient, zoneOperationsClient) {
20+
// [START compute_consume_specific_shared_reservation]
21+
// Import the Compute library
22+
const computeLib = require('@google-cloud/compute');
23+
const compute = computeLib.protos.google.cloud.compute.v1;
24+
25+
/**
26+
* TODO(developer): Uncomment reservationsClient and zoneOperationsClient before running the sample.
27+
*/
28+
// Instantiate an instancesClient
29+
// instancesClient = new computeLib.InstancesClient();
30+
// Instantiate a zoneOperationsClient
31+
// zoneOperationsClient = new computeLib.ZoneOperationsClient();
32+
33+
/**
34+
* TODO(developer): Update these variables before running the sample.
35+
*/
36+
// The ID of the project where instance will be consumed and created.
37+
const baseProjectId = 'base-project-id';
38+
// The ID of the project where reservation is created.
39+
const projectId = 'project-d';
40+
// The name of the instance to create.
41+
const instanceName = 'instance-01';
42+
// The name of the reservation to consume.
43+
// Ensure that the specificReservationRequired field in reservation properties is set to true.
44+
const reservationName = 'reservation-1';
45+
// Machine type to use for VM.
46+
const machineType = 'n1-standard-1';
47+
// The zone in which to create instance.
48+
const zone = 'us-central1-a';
49+
50+
// Create instance to consume shared reservation
51+
async function callCreateInstanceToConsumeSharedReservation() {
52+
// Describe the size and source image of the boot disk to attach to the instance.
53+
// Ensure that the VM's properties match the reservation's VM properties,
54+
// including the zone, machine type (machine family, vCPUs, and memory),
55+
// minimum CPU platform, GPU amount and type, and local SSD interface and size
56+
const disk = new compute.Disk({
57+
boot: true,
58+
autoDelete: true,
59+
type: 'PERSISTENT',
60+
initializeParams: {
61+
diskSizeGb: '10',
62+
sourceImage: 'projects/debian-cloud/global/images/family/debian-12',
63+
},
64+
});
65+
66+
// Define networkInterface
67+
const networkInterface = new compute.NetworkInterface({
68+
name: 'global/networks/default',
69+
});
70+
71+
// Define reservationAffinity
72+
const reservationAffinity = new compute.ReservationAffinity({
73+
consumeReservationType: 'SPECIFIC_RESERVATION',
74+
key: 'compute.googleapis.com/reservation-name',
75+
values: [`projects/${projectId}/reservations/${reservationName}`],
76+
});
77+
78+
// Create an instance
79+
const instance = new compute.Instance({
80+
name: instanceName,
81+
machineType: `zones/${zone}/machineTypes/${machineType}`,
82+
disks: [disk],
83+
networkInterfaces: [networkInterface],
84+
reservationAffinity,
85+
});
86+
87+
const [response] = await instancesClient.insert({
88+
project: baseProjectId,
89+
instanceResource: instance,
90+
zone,
91+
});
92+
93+
let operation = response.latestResponse;
94+
95+
// Wait for the create instance operation to complete.
96+
while (operation.status !== 'DONE') {
97+
[operation] = await zoneOperationsClient.wait({
98+
operation: operation.name,
99+
project: baseProjectId,
100+
zone: operation.zone.split('/').pop(),
101+
});
102+
}
103+
104+
console.log(`Instance ${instanceName} created from shared reservation.`);
105+
return response;
106+
}
107+
108+
return await callCreateInstanceToConsumeSharedReservation();
109+
// [END compute_consume_specific_shared_reservation]
110+
}
111+
112+
module.exports = main;
113+
114+
// TODO(developer): Uncomment below lines before running the sample.
115+
// main(...process.argv.slice(2)).catch(err => {
116+
// console.error(err);
117+
// process.exitCode = 1;
118+
// });

compute/reservations/createInstanceToConsumeSingleProjectReservation.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async function main(instanceName, reservationName) {
2222
const computeLib = require('@google-cloud/compute');
2323
const compute = computeLib.protos.google.cloud.compute.v1;
2424

25-
// Instantiate a reservationsClient
25+
// Instantiate an instancesClient
2626
const instancesClient = new computeLib.InstancesClient();
2727
// Instantiate a zoneOperationsClient
2828
const zoneOperationsClient = new computeLib.ZoneOperationsClient();

compute/reservations/createInstanceToNotConsumeReservation.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async function main(instanceName) {
2222
const computeLib = require('@google-cloud/compute');
2323
const compute = computeLib.protos.google.cloud.compute.v1;
2424

25-
// Instantiate a reservationsClient
25+
// Instantiate an instancesClient
2626
const instancesClient = new computeLib.InstancesClient();
2727
// Instantiate a zoneOperationsClient
2828
const zoneOperationsClient = new computeLib.ZoneOperationsClient();

0 commit comments

Comments
 (0)