MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

compute/disks/attachRegionalDisk.js:19–76  ·  view source on GitHub ↗
(diskName, region, vmName, zone)

Source from the content-addressed store, hash-verified

17'use strict';
18
19async function main(diskName, region, vmName, zone) {
20 // [START compute_instance_attach_regional_disk]
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 = 'us-central1-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 = 'us-central1';
47
48 async function callAttachRegionalDisk() {
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 // If you want to force the disk to be attached, uncomment next line.
55 // forceAttach: true,
56 }),
57 zone,
58 });
59
60 let operation = response.latestResponse;
61
62 // Wait for the operation to complete.
63 while (operation.status !== 'DONE') {
64 [operation] = await zoneOperationsClient.wait({
65 operation: operation.name,
66 project: projectId,
67 zone: operation.zone.split('/').pop(),
68 });
69 }
70
71 console.log(`Replicated disk: ${diskName} attached to VM: ${vmName}.`);
72 }
73
74 await callAttachRegionalDisk();
75 // [END compute_instance_attach_regional_disk]
76}

Callers 1

Calls 2

callAttachRegionalDiskFunction · 0.85
getProjectIdMethod · 0.80

Tested by

no test coverage detected