Skip to content

Commit 0ee1cbd

Browse files
Joanna Gryczgryczj
authored andcommitted
feat: compute_instance_attach_regional_disk_force
1 parent 6eb20eb commit 0ee1cbd

File tree

2 files changed

+114
-6
lines changed

2 files changed

+114
-6
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
});

compute/test/replicatedDisk.test.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ async function deleteDisk(projectId, region, diskName) {
5151

5252
describe('Create compute regional replicated disk', async () => {
5353
const diskName = `replicated-disk-${uuid.v4()}`;
54-
const vmName = `vm-with-replicated-disk-${uuid.v4()}`;
54+
const vmName1 = `vm1-with-replicated-disk-${uuid.v4()}`;
55+
const vmName2 = `vm2-with-replicated-disk-${uuid.v4()}`;
5556
const region = 'europe-central2';
5657
const zone1 = 'europe-central2-a';
5758
const zone2 = 'europe-central2-b';
@@ -64,8 +65,10 @@ describe('Create compute regional replicated disk', async () => {
6465

6566
after(async () => {
6667
// Cleanup resources
67-
execSync(`node ./deleteInstance.js ${projectId} ${zone1} ${vmName}`, {
68-
cwd,
68+
[vmName1, vmName2].forEach(vm => {
69+
execSync(`node ./deleteInstance.js ${projectId} ${zone1} ${vm}`, {
70+
cwd,
71+
});
6972
});
7073
await deleteDisk(projectId, region, diskName);
7174
});
@@ -84,22 +87,45 @@ describe('Create compute regional replicated disk', async () => {
8487
it('should attach replicated disk to vm', () => {
8588
// Create VM, where replicated disk will be attached.
8689
execSync(
87-
`node ./createInstance.js ${projectId} ${zone1} ${vmName} e2-small`,
90+
`node ./createInstance.js ${projectId} ${zone1} ${vmName1} e2-small`,
91+
{
92+
cwd,
93+
}
94+
);
95+
96+
const response = execSync(
97+
`node ./disks/attachRegionalDisk.js ${diskName} ${region} ${vmName1} ${zone1}`,
98+
{
99+
cwd,
100+
}
101+
);
102+
103+
assert(
104+
response.includes(
105+
`Replicated disk: ${diskName} attached to VM: ${vmName1}.`
106+
)
107+
);
108+
});
109+
110+
it('should force replicated disk to be attached to vm', () => {
111+
// Create VM, where replicated disk will be attached.
112+
execSync(
113+
`node ./createInstance.js ${projectId} ${zone1} ${vmName2} e2-small`,
88114
{
89115
cwd,
90116
}
91117
);
92118

93119
const response = execSync(
94-
`node ./disks/attachRegionalDisk.js ${diskName} ${region} ${vmName} ${zone1}`,
120+
`node ./disks/attachRegionalDiskForce.js ${diskName} ${region} ${vmName2} ${zone1}`,
95121
{
96122
cwd,
97123
}
98124
);
99125

100126
assert(
101127
response.includes(
102-
`Replicated disk: ${diskName} attached to VM: ${vmName}.`
128+
`Replicated disk: ${diskName} was forced to be attached to VM: ${vmName2}.`
103129
)
104130
);
105131
});

0 commit comments

Comments
 (0)