Skip to content

Commit d38127e

Browse files
Joanna Grycziennae
Joanna Grycz
authored andcommitted
feat: compute_consistency_group_disks_list
1 parent 3111b30 commit d38127e

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
disksLocation
23+
) {
24+
// [START compute_consistency_group_disks_list]
25+
// Import the Compute library
26+
const computeLib = require('@google-cloud/compute');
27+
28+
// If you want to get regional disks, you should use: RegionDisksClient.
29+
// Instantiate a disksClient
30+
const disksClient = new computeLib.DisksClient();
31+
32+
/**
33+
* TODO(developer): Update/uncomment these variables before running the sample.
34+
*/
35+
// The project that contains the disks.
36+
const projectId = await disksClient.getProjectId();
37+
38+
// If you use RegionDisksClient- define region, if DisksClient- define zone.
39+
// The zone or region of the disks.
40+
// disksLocation = 'europe-central2-a';
41+
42+
// The name of the consistency group.
43+
// consistencyGroupName = 'consistency-group-name';
44+
45+
// The region of the consistency group.
46+
// consistencyGroupLocation = 'europe-central2';
47+
48+
async function callConsistencyGroupDisksList() {
49+
const filter = `https://www.googleapis.com/compute/v1/projects/${projectId}/regions/${consistencyGroupLocation}/resourcePolicies/${consistencyGroupName}`;
50+
51+
const [response] = await disksClient.list({
52+
project: projectId,
53+
// If you use RegionDisksClient, pass region as an argument instead of zone.
54+
zone: disksLocation,
55+
});
56+
57+
// Filtering must be done manually for now, since list filtering inside disksClient.list is not supported yet.
58+
const filteredDisks = response.filter(disk =>
59+
disk.resourcePolicies.includes(filter)
60+
);
61+
62+
console.log(JSON.stringify(filteredDisks));
63+
}
64+
65+
await callConsistencyGroupDisksList();
66+
// [END compute_consistency_group_disks_list]
67+
}
68+
69+
main(...process.argv.slice(2)).catch(err => {
70+
console.error(err);
71+
process.exitCode = 1;
72+
});

compute/test/consistencyGroup.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ describe('Consistency group', async () => {
102102
);
103103
});
104104

105+
it('should return disks from consistency group', () => {
106+
const response = JSON.parse(
107+
execSync(
108+
`node ./disks/consistencyGroups/consistencyGroupDisksList.js ${consistencyGroupName} ${region} ${diskLocation}`,
109+
{
110+
cwd,
111+
}
112+
)
113+
);
114+
115+
assert(Array.isArray(response));
116+
});
117+
105118
it('should delete disk from consistency group', () => {
106119
const response = execSync(
107120
`node ./disks/consistencyGroups/consistencyGroupRemoveDisk.js ${consistencyGroupName} ${region} ${diskName} ${diskLocation}`,

0 commit comments

Comments
 (0)