Skip to content

Commit 9f2c418

Browse files
Joanna Grycziennae
Joanna Grycz
authored andcommitted
feat: compute_consistency_group_add_disk
1 parent 88e6813 commit 9f2c418

File tree

4 files changed

+163
-10
lines changed

4 files changed

+163
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
consistencyGroupName,
21+
consistencyGroupLocation,
22+
diskName,
23+
diskLocation
24+
) {
25+
// [START compute_consistency_group_add_disk]
26+
// Import the Compute library
27+
const computeLib = require('@google-cloud/compute');
28+
const compute = computeLib.protos.google.cloud.compute.v1;
29+
30+
// Instantiate a disksClient
31+
const disksClient = new computeLib.DisksClient();
32+
// Instantiate a zone
33+
const zoneOperationsClient = new computeLib.ZoneOperationsClient();
34+
35+
/**
36+
* TODO(developer): Update/uncomment these variables before running the sample.
37+
*/
38+
// The project that contains the disk.
39+
// const projectId = await disksClient.getProjectId();
40+
const projectId = 'jgrycz-softserve-project';
41+
42+
// The name of the disk.
43+
// diskName = 'disk-name';
44+
45+
// The zone of the disk.
46+
// diskLocation = 'europe-central2-a';
47+
48+
// The name of the consistency group.
49+
// consistencyGroupName = 'consistency-group-name';
50+
51+
// The region of the consistency group.
52+
// consistencyGroupLocation = 'europe-central2';
53+
54+
async function callAddDiskToConsistencyGroup() {
55+
const [response] = await disksClient.addResourcePolicies({
56+
disk: diskName,
57+
project: projectId,
58+
zone: diskLocation,
59+
disksAddResourcePoliciesRequestResource:
60+
new compute.DisksAddResourcePoliciesRequest({
61+
resourcePolicies: [
62+
`https://www.googleapis.com/compute/v1/projects/${projectId}/regions/${consistencyGroupLocation}/resourcePolicies/${consistencyGroupName}`,
63+
],
64+
}),
65+
});
66+
67+
let operation = response.latestResponse;
68+
69+
// Wait for the add disk operation to complete.
70+
while (operation.status !== 'DONE') {
71+
[operation] = await zoneOperationsClient.wait({
72+
operation: operation.name,
73+
project: projectId,
74+
zone: operation.zone.split('/').pop(),
75+
});
76+
}
77+
78+
console.log(
79+
`Disk: ${diskName} added to consistency group: ${consistencyGroupName}.`
80+
);
81+
}
82+
83+
await callAddDiskToConsistencyGroup();
84+
// [END compute_consistency_group_add_disk]
85+
}
86+
87+
main(...process.argv.slice(2)).catch(err => {
88+
console.error(err);
89+
process.exitCode = 1;
90+
});

compute/disks/consistencyGroups/createConsistencyGroup.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,20 @@ async function main(consistencyGroupName, region) {
2626
const resourcePoliciesClient = new computeLib.ResourcePoliciesClient();
2727
// Instantiate a regionOperationsClient
2828
const regionOperationsClient = new computeLib.RegionOperationsClient();
29+
const projectId = 'jgrycz-softserve-project';
2930

3031
/**
3132
* TODO(developer): Update/uncomment these variables before running the sample.
3233
*/
3334
// The project that contains the consistency group.
34-
const projectId = await resourcePoliciesClient.getProjectId();
35+
// const projectId = await resourcePoliciesClient.getProjectId();
3536

3637
// The region for the consistency group.
3738
// If you want to add primary disks to consistency group, use the same region as the primary disks.
3839
// If you want to add secondary disks to the consistency group, use the same region as the secondary disks.
3940
// region = 'europe-central2';
4041

41-
// The name for consistency group
42+
// The name for consistency group.
4243
// consistencyGroupName = 'consistency-group-name';
4344

4445
async function callCreateConsistencyGroup() {

compute/disks/consistencyGroups/deleteConsistencyGroup.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,18 @@ async function main(consistencyGroupName, region) {
2525
const resourcePoliciesClient = new computeLib.ResourcePoliciesClient();
2626
// Instantiate a regionOperationsClient
2727
const regionOperationsClient = new computeLib.RegionOperationsClient();
28+
2829
/**
2930
* TODO(developer): Update/uncomment these variables before running the sample.
3031
*/
3132
// The project that contains the consistency group.
32-
const projectId = await resourcePoliciesClient.getProjectId();
33+
// const projectId = await resourcePoliciesClient.getProjectId();
34+
const projectId = 'jgrycz-softserve-project';
3335

34-
// The region of the consistency group
36+
// The region of the consistency group.
3537
// region = 'europe-central2';
3638

37-
// The name of the consistency group
39+
// The name of the consistency group.
3840
// consistencyGroupName = 'consistency-group-name';
3941

4042
async function callCreateConsistencyGroup() {

compute/test/consistencyGroup.test.js

+65-5
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,61 @@
1818

1919
const path = require('path');
2020
const assert = require('node:assert/strict');
21-
const {describe, it} = require('mocha');
22-
const uuid = require('uuid');
21+
const {before, after, describe, it} = require('mocha');
2322
const cp = require('child_process');
23+
const uuid = require('uuid');
24+
const computeLib = require('@google-cloud/compute');
25+
const {getStaleDisks, deleteDisk} = require('./util');
2426

2527
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2628
const cwd = path.join(__dirname, '..');
2729

30+
const disksClient = new computeLib.DisksClient();
31+
const zoneOperationsClient = new computeLib.ZoneOperationsClient();
32+
33+
async function createDisk(diskName, zone, projectId) {
34+
const [response] = await disksClient.insert({
35+
project: projectId,
36+
zone,
37+
diskResource: {
38+
sizeGb: 10,
39+
name: diskName,
40+
zone,
41+
type: `zones/${zone}/diskTypes/pd-balanced`,
42+
},
43+
});
44+
45+
let operation = response.latestResponse;
46+
47+
// Wait for the create disk operation to complete.
48+
while (operation.status !== 'DONE') {
49+
[operation] = await zoneOperationsClient.wait({
50+
operation: operation.name,
51+
project: projectId,
52+
zone: operation.zone.split('/').pop(),
53+
});
54+
}
55+
56+
console.log(`Disk: ${diskName} created.`);
57+
}
2858
describe('Consistency group', async () => {
2959
const consistencyGroupName = `consistency-group-name-${uuid.v4()}`;
60+
const diskName = `disk-name-${uuid.v4()}`;
61+
const diskLocation = 'europe-central2-a';
3062
const region = 'europe-central2';
63+
let projectId;
64+
65+
before(async () => {
66+
projectId = 'jgrycz-softserve-project';
67+
// projectId = await disksClient.getProjectId();
68+
await createDisk(diskName, diskLocation, projectId);
69+
});
70+
71+
after(async () => {
72+
// Cleanup resources
73+
// const disks = await getStaleDisks(prefix);
74+
// await Promise.all(disks.map(disk => deleteDisk(disk.zone, disk.diskName)));
75+
});
3176

3277
it('should create a new consistency group', () => {
3378
const response = execSync(
@@ -42,16 +87,31 @@ describe('Consistency group', async () => {
4287
);
4388
});
4489

45-
it('should delete consistency group', () => {
90+
it('should add disk to consistency group', () => {
4691
const response = execSync(
47-
`node ./disks/consistencyGroups/deleteConsistencyGroup.js ${consistencyGroupName} ${region}`,
92+
`node ./disks/consistencyGroups/consistencyGroupAddDisk.js ${consistencyGroupName} ${region} ${diskName} ${diskLocation}`,
4893
{
4994
cwd,
5095
}
5196
);
5297

5398
assert(
54-
response.includes(`Consistency group: ${consistencyGroupName} deleted.`)
99+
response.includes(
100+
`Disk: ${diskName} added to consistency group: ${consistencyGroupName}.`
101+
)
55102
);
56103
});
104+
105+
// it('should delete consistency group', () => {
106+
// const response = execSync(
107+
// `node ./disks/consistencyGroups/deleteConsistencyGroup.js ${consistencyGroupName} ${region}`,
108+
// {
109+
// cwd,
110+
// }
111+
// );
112+
113+
// assert(
114+
// response.includes(`Consistency group: ${consistencyGroupName} deleted.`)
115+
// );
116+
// });
57117
});

0 commit comments

Comments
 (0)