Skip to content

Commit 3111b30

Browse files
Joanna Grycziennae
Joanna Grycz
authored andcommitted
feat: compute_consistency_group_remove_disk
1 parent 9f2c418 commit 3111b30

File tree

5 files changed

+135
-24
lines changed

5 files changed

+135
-24
lines changed

compute/disks/consistencyGroups/consistencyGroupAddDisk.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ async function main(
2727
const computeLib = require('@google-cloud/compute');
2828
const compute = computeLib.protos.google.cloud.compute.v1;
2929

30+
// If you want to add regional disk,
31+
// you should use: RegionDisksClient and RegionOperationsClient.
3032
// Instantiate a disksClient
3133
const disksClient = new computeLib.DisksClient();
3234
// Instantiate a zone
@@ -36,13 +38,13 @@ async function main(
3638
* TODO(developer): Update/uncomment these variables before running the sample.
3739
*/
3840
// The project that contains the disk.
39-
// const projectId = await disksClient.getProjectId();
40-
const projectId = 'jgrycz-softserve-project';
41+
const projectId = await disksClient.getProjectId();
4142

4243
// The name of the disk.
4344
// diskName = 'disk-name';
4445

45-
// The zone of the disk.
46+
// If you use RegionDisksClient- define region, if DisksClient- define zone.
47+
// The zone or region of the disk.
4648
// diskLocation = 'europe-central2-a';
4749

4850
// The name of the consistency group.
@@ -55,6 +57,7 @@ async function main(
5557
const [response] = await disksClient.addResourcePolicies({
5658
disk: diskName,
5759
project: projectId,
60+
// If you use RegionDisksClient, pass region as an argument instead of zone.
5861
zone: diskLocation,
5962
disksAddResourcePoliciesRequestResource:
6063
new compute.DisksAddResourcePoliciesRequest({
@@ -71,6 +74,7 @@ async function main(
7174
[operation] = await zoneOperationsClient.wait({
7275
operation: operation.name,
7376
project: projectId,
77+
// If you use RegionDisksClient, pass region as an argument instead of zone.
7478
zone: operation.zone.split('/').pop(),
7579
});
7680
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
// If you want to remove regional disk,
31+
// you should use: RegionDisksClient and RegionOperationsClient.
32+
// Instantiate a disksClient
33+
const disksClient = new computeLib.DisksClient();
34+
// Instantiate a zone
35+
const zoneOperationsClient = new computeLib.ZoneOperationsClient();
36+
37+
/**
38+
* TODO(developer): Update/uncomment these variables before running the sample.
39+
*/
40+
// The project that contains the disk.
41+
const projectId = await disksClient.getProjectId();
42+
43+
// The name of the disk.
44+
// diskName = 'disk-name';
45+
46+
// If you use RegionDisksClient- define region, if DisksClient- define zone.
47+
// The zone or region of the disk.
48+
// diskLocation = 'europe-central2-a';
49+
50+
// The name of the consistency group.
51+
// consistencyGroupName = 'consistency-group-name';
52+
53+
// The region of the consistency group.
54+
// consistencyGroupLocation = 'europe-central2';
55+
56+
async function callDeleteDiskFromConsistencyGroup() {
57+
const [response] = await disksClient.removeResourcePolicies({
58+
disk: diskName,
59+
project: projectId,
60+
// If you use RegionDisksClient, pass region as an argument instead of zone.
61+
zone: diskLocation,
62+
disksRemoveResourcePoliciesRequestResource:
63+
new compute.DisksRemoveResourcePoliciesRequest({
64+
resourcePolicies: [
65+
`https://www.googleapis.com/compute/v1/projects/${projectId}/regions/${consistencyGroupLocation}/resourcePolicies/${consistencyGroupName}`,
66+
],
67+
}),
68+
});
69+
70+
let operation = response.latestResponse;
71+
72+
// Wait for the delete disk operation to complete.
73+
while (operation.status !== 'DONE') {
74+
[operation] = await zoneOperationsClient.wait({
75+
operation: operation.name,
76+
project: projectId,
77+
// If you use RegionDisksClient, pass region as an argument instead of zone.
78+
zone: operation.zone.split('/').pop(),
79+
});
80+
}
81+
82+
console.log(
83+
`Disk: ${diskName} deleted from consistency group: ${consistencyGroupName}.`
84+
);
85+
}
86+
87+
await callDeleteDiskFromConsistencyGroup();
88+
// [END compute_consistency_group_remove_disk]
89+
}
90+
91+
main(...process.argv.slice(2)).catch(err => {
92+
console.error(err);
93+
process.exitCode = 1;
94+
});

compute/disks/consistencyGroups/createConsistencyGroup.js

+1-2
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

+1-2
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

+32-17
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 = '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)