Skip to content

Commit b070a22

Browse files
Joanna Gryczgryczj
authored andcommitted
feat: compute_instance_attach_regional_disk
1 parent 56bb891 commit b070a22

File tree

3 files changed

+116
-6
lines changed

3 files changed

+116
-6
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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]
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 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+
}),
55+
zone,
56+
});
57+
58+
let operation = response.latestResponse;
59+
60+
// Wait for the operation to complete.
61+
while (operation.status !== 'DONE') {
62+
[operation] = await zoneOperationsClient.wait({
63+
operation: operation.name,
64+
project: projectId,
65+
zone: operation.zone.split('/').pop(),
66+
});
67+
}
68+
69+
console.log(`Replicated disk: ${diskName} attached to VM: ${vmName}.`);
70+
}
71+
72+
await callAttachRegionalDisk();
73+
// [END compute_instance_attach_regional_disk]
74+
}
75+
76+
main(...process.argv.slice(2)).catch(err => {
77+
console.error(err);
78+
process.exitCode = 1;
79+
});

compute/disks/createRegionalReplicatedDisk.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,11 @@ async function main(diskName, region, zone1, zone2) {
4242
// zone2 = 'europe-central2-b';
4343

4444
// The name of the new disk.
45-
// diskName = 'primary-disk-name';
45+
// diskName = 'disk-name';
4646

4747
// The type of replicated disk.
4848
// The default value is `pd-standard`. For Hyperdisk, specify the value `hyperdisk-balanced-high-availability`.
4949
const diskType = `regions/${region}/diskTypes/pd-standard`;
50-
// const diskType = 'pd-standard';
5150

5251
// The size of the new disk in gigabytes.
5352
const diskSizeGb = 200;

compute/test/createRegionalReplicatedDisk.test.js renamed to compute/test/replicatedDisk.test.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,16 @@
1919
const path = require('path');
2020
const assert = require('node:assert/strict');
2121
const uuid = require('uuid');
22-
const {after, describe, it} = require('mocha');
22+
const {after, before, describe, it} = require('mocha');
2323
const cp = require('child_process');
2424
const computeLib = require('@google-cloud/compute');
2525

2626
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2727
const cwd = path.join(__dirname, '..');
2828

29-
async function deleteDisk(region, diskName) {
29+
async function deleteDisk(projectId, region, diskName) {
3030
const disksClient = new computeLib.RegionDisksClient();
3131
const regionOperationsClient = new computeLib.RegionOperationsClient();
32-
const projectId = await disksClient.getProjectId();
3332

3433
const [response] = await disksClient.delete({
3534
project: projectId,
@@ -52,13 +51,23 @@ async function deleteDisk(region, diskName) {
5251

5352
describe('Create compute regional replicated disk', async () => {
5453
const diskName = `replicated-disk-${uuid.v4()}`;
54+
const vmName = `vm-with-replicated-disk-${uuid.v4()}`;
5555
const region = 'europe-central2';
5656
const zone1 = 'europe-central2-a';
5757
const zone2 = 'europe-central2-b';
58+
let projectId;
59+
60+
before(async () => {
61+
const instancesClient = new computeLib.InstancesClient();
62+
projectId = await instancesClient.getProjectId();
63+
});
5864

5965
after(async () => {
6066
// Cleanup resources
61-
await deleteDisk(region, diskName);
67+
execSync(`node ./deleteInstance.js ${projectId} ${zone1} ${vmName}`, {
68+
cwd,
69+
});
70+
await deleteDisk(projectId, region, diskName);
6271
});
6372

6473
it('should create a regional replicated disk', () => {
@@ -71,4 +80,27 @@ describe('Create compute regional replicated disk', async () => {
7180

7281
assert(response.includes(`Regional replicated disk: ${diskName} created.`));
7382
});
83+
84+
it('should attach replicated disk to vm', () => {
85+
// Create VM, where replicated disk will be attached.
86+
execSync(
87+
`node ./createInstance.js ${projectId} ${zone1} ${vmName} e2-small`,
88+
{
89+
cwd,
90+
}
91+
);
92+
93+
const response = execSync(
94+
`node ./disks/attachRegionalDisk.js ${diskName} ${region} ${vmName} ${zone1}`,
95+
{
96+
cwd,
97+
}
98+
);
99+
100+
assert(
101+
response.includes(
102+
`Replicated disk: ${diskName} attached to VM: ${vmName}.`
103+
)
104+
);
105+
});
74106
});

0 commit comments

Comments
 (0)