|
| 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 | +}); |
0 commit comments