|
| 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(diskName, region, vmName, zone) { |
| 20 | + // [START compute_instance_attach_regional_disk_force] |
| 21 | + // Import the Compute library |
| 22 | + const computeLib = require('@google-cloud/compute'); |
| 23 | + const compute = computeLib.protos.google.cloud.compute.v1; |
| 24 | + |
| 25 | + // Instantiate an instancesClient |
| 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 | + // Your project ID. |
| 34 | + const projectId = await instancesClient.getProjectId(); |
| 35 | + |
| 36 | + // The zone of your VM. |
| 37 | + // zone = 'europe-central2-a'; |
| 38 | + |
| 39 | + // The name of the VM to which you're adding the new replicated disk. |
| 40 | + // vmName = 'vm-name'; |
| 41 | + |
| 42 | + // The name of the replicated disk |
| 43 | + // diskName = 'disk-name'; |
| 44 | + |
| 45 | + // The region where the replicated disk is located. |
| 46 | + // region = 'europe-central2'; |
| 47 | + |
| 48 | + async function callForceAttachRegionalDisk() { |
| 49 | + const [response] = await instancesClient.attachDisk({ |
| 50 | + instance: vmName, |
| 51 | + project: projectId, |
| 52 | + attachedDiskResource: new compute.AttachedDisk({ |
| 53 | + source: `projects/${projectId}/regions/${region}/disks/${diskName}`, |
| 54 | + forceAttach: true, |
| 55 | + }), |
| 56 | + zone, |
| 57 | + }); |
| 58 | + |
| 59 | + let operation = response.latestResponse; |
| 60 | + |
| 61 | + // Wait for the operation to complete. |
| 62 | + while (operation.status !== 'DONE') { |
| 63 | + [operation] = await zoneOperationsClient.wait({ |
| 64 | + operation: operation.name, |
| 65 | + project: projectId, |
| 66 | + zone: operation.zone.split('/').pop(), |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + console.log( |
| 71 | + `Replicated disk: ${diskName} was forced to be attached to VM: ${vmName}.` |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + await callForceAttachRegionalDisk(); |
| 76 | + // [END compute_instance_attach_regional_disk_force] |
| 77 | +} |
| 78 | + |
| 79 | +main(...process.argv.slice(2)).catch(err => { |
| 80 | + console.error(err); |
| 81 | + process.exitCode = 1; |
| 82 | +}); |
0 commit comments