|
| 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_vms_update] |
| 21 | + // Import the Compute library |
| 22 | + const computeLib = require('@google-cloud/compute'); |
| 23 | + |
| 24 | + // Instantiate a reservationsClient |
| 25 | + const reservationsClient = new computeLib.ReservationsClient(); |
| 26 | + // Instantiate a zoneOperationsClient |
| 27 | + const zoneOperationsClient = new computeLib.ZoneOperationsClient(); |
| 28 | + |
| 29 | + /** |
| 30 | + * TODO(developer): Update these variables before running the sample. |
| 31 | + */ |
| 32 | + // The ID of the project where the reservation is located. |
| 33 | + const projectId = await reservationsClient.getProjectId(); |
| 34 | + // The zone where the reservation is located. |
| 35 | + const zone = 'us-central1-a'; |
| 36 | + // The name of an existing reservation. |
| 37 | + const reservationName = 'reservation-01'; |
| 38 | + // The new number of VMs to reserve(increase or decrease the number). Before modifying the number of VMs, |
| 39 | + // ensure that all required conditions are met. See: https://cloud.google.com/compute/docs/instances/reservations-modify#resizing_a_reservation. |
| 40 | + const vmsNumber = 1; |
| 41 | + |
| 42 | + async function callComputeReservationVmsUpdate() { |
| 43 | + // Modify the number of reserved VMs in specific reservation |
| 44 | + const [response] = await reservationsClient.resize({ |
| 45 | + project: projectId, |
| 46 | + reservation: reservationName, |
| 47 | + zone, |
| 48 | + reservationsResizeRequestResource: { |
| 49 | + specificSkuCount: vmsNumber, |
| 50 | + }, |
| 51 | + }); |
| 52 | + |
| 53 | + let operation = response.latestResponse; |
| 54 | + |
| 55 | + // Wait for the update reservation operation to complete. |
| 56 | + while (operation.status !== 'DONE') { |
| 57 | + [operation] = await zoneOperationsClient.wait({ |
| 58 | + operation: operation.name, |
| 59 | + project: projectId, |
| 60 | + zone: operation.zone.split('/').pop(), |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + const updatedReservation = ( |
| 65 | + await reservationsClient.get({ |
| 66 | + project: projectId, |
| 67 | + zone, |
| 68 | + reservation: reservationName, |
| 69 | + }) |
| 70 | + )[0]; |
| 71 | + |
| 72 | + console.log(JSON.stringify(updatedReservation)); |
| 73 | + } |
| 74 | + |
| 75 | + await callComputeReservationVmsUpdate(); |
| 76 | + // [END compute_reservation_vms_update] |
| 77 | +} |
| 78 | + |
| 79 | +main().catch(err => { |
| 80 | + console.error(err); |
| 81 | + process.exitCode = 1; |
| 82 | +}); |
0 commit comments