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