Skip to content

Commit 69be858

Browse files
author
Joanna Grycz
committed
feat: compute_consistency_group_remove_disk
1 parent 1ff93be commit 69be858

File tree

5 files changed

+125
-24
lines changed

5 files changed

+125
-24
lines changed

compute/disks/consistencyGroups/consistencyGroupAddDisk.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@ async function main(
3636
* TODO(developer): Update/uncomment these variables before running the sample.
3737
*/
3838
// The project that contains the disk.
39-
// const projectId = await disksClient.getProjectId();
40-
const projectId = 'jgrycz-softserve-project';
39+
const projectId = await disksClient.getProjectId();
4140

4241
// The name of the disk.
4342
// diskName = 'disk-name';
4443

45-
// The zone of the disk.
44+
// The zone or region of the disk.
4645
// diskLocation = 'europe-central2-a';
4746

4847
// The name of the consistency group.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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_remove_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+
41+
// The name of the disk.
42+
// diskName = 'disk-name';
43+
44+
// The zone or region of the disk.
45+
// diskLocation = 'europe-central2-a';
46+
47+
// The name of the consistency group.
48+
// consistencyGroupName = 'consistency-group-name';
49+
50+
// The region of the consistency group.
51+
// consistencyGroupLocation = 'europe-central2';
52+
53+
async function callDeleteDiskFromConsistencyGroup() {
54+
const [response] = await disksClient.removeResourcePolicies({
55+
disk: diskName,
56+
project: projectId,
57+
zone: diskLocation,
58+
disksRemoveResourcePoliciesRequestResource:
59+
new compute.DisksRemoveResourcePoliciesRequest({
60+
resourcePolicies: [
61+
`https://www.googleapis.com/compute/v1/projects/${projectId}/regions/${consistencyGroupLocation}/resourcePolicies/${consistencyGroupName}`,
62+
],
63+
}),
64+
});
65+
66+
let operation = response.latestResponse;
67+
68+
// Wait for the delete disk operation to complete.
69+
while (operation.status !== 'DONE') {
70+
[operation] = await zoneOperationsClient.wait({
71+
operation: operation.name,
72+
project: projectId,
73+
zone: operation.zone.split('/').pop(),
74+
});
75+
}
76+
77+
console.log(
78+
`Disk: ${diskName} deleted from consistency group: ${consistencyGroupName}.`
79+
);
80+
}
81+
82+
await callDeleteDiskFromConsistencyGroup();
83+
// [END compute_consistency_group_remove_disk]
84+
}
85+
86+
main(...process.argv.slice(2)).catch(err => {
87+
console.error(err);
88+
process.exitCode = 1;
89+
});

compute/disks/consistencyGroups/createConsistencyGroup.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@ 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';
3029

3130
/**
3231
* TODO(developer): Update/uncomment these variables before running the sample.
3332
*/
3433
// The project that contains the consistency group.
35-
// const projectId = await resourcePoliciesClient.getProjectId();
34+
const projectId = await resourcePoliciesClient.getProjectId();
3635

3736
// The region for the consistency group.
3837
// If you want to add primary disks to consistency group, use the same region as the primary disks.

compute/disks/consistencyGroups/deleteConsistencyGroup.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ async function main(consistencyGroupName, region) {
3030
* TODO(developer): Update/uncomment these variables before running the sample.
3131
*/
3232
// The project that contains the consistency group.
33-
// const projectId = await resourcePoliciesClient.getProjectId();
34-
const projectId = 'jgrycz-softserve-project';
33+
const projectId = await resourcePoliciesClient.getProjectId();
3534

3635
// The region of the consistency group.
3736
// region = 'europe-central2';

compute/test/consistencyGroup.test.js

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,21 @@ async function createDisk(diskName, zone, projectId) {
5757
}
5858
describe('Consistency group', async () => {
5959
const consistencyGroupName = `consistency-group-name-${uuid.v4()}`;
60-
const diskName = `disk-name-${uuid.v4()}`;
60+
const prefix = 'consistency-group-disk-name';
61+
const diskName = `${prefix}-${uuid.v4()}`;
6162
const diskLocation = 'europe-central2-a';
6263
const region = 'europe-central2';
6364
let projectId;
6465

6566
before(async () => {
66-
projectId = 'jgrycz-softserve-project';
67-
// projectId = await disksClient.getProjectId();
67+
projectId = await disksClient.getProjectId();
6868
await createDisk(diskName, diskLocation, projectId);
6969
});
7070

7171
after(async () => {
7272
// Cleanup resources
73-
// const disks = await getStaleDisks(prefix);
74-
// await Promise.all(disks.map(disk => deleteDisk(disk.zone, disk.diskName)));
73+
const disks = await getStaleDisks(prefix);
74+
await Promise.all(disks.map(disk => deleteDisk(disk.zone, disk.diskName)));
7575
});
7676

7777
it('should create a new consistency group', () => {
@@ -102,16 +102,31 @@ describe('Consistency group', async () => {
102102
);
103103
});
104104

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-
// });
105+
it('should delete disk from consistency group', () => {
106+
const response = execSync(
107+
`node ./disks/consistencyGroups/consistencyGroupRemoveDisk.js ${consistencyGroupName} ${region} ${diskName} ${diskLocation}`,
108+
{
109+
cwd,
110+
}
111+
);
112+
113+
assert(
114+
response.includes(
115+
`Disk: ${diskName} deleted from consistency group: ${consistencyGroupName}.`
116+
)
117+
);
118+
});
119+
120+
it('should delete consistency group', () => {
121+
const response = execSync(
122+
`node ./disks/consistencyGroups/deleteConsistencyGroup.js ${consistencyGroupName} ${region}`,
123+
{
124+
cwd,
125+
}
126+
);
127+
128+
assert(
129+
response.includes(`Consistency group: ${consistencyGroupName} deleted.`)
130+
);
131+
});
117132
});

0 commit comments

Comments
 (0)