Skip to content

Commit 88e6813

Browse files
Joanna Grycziennae
authored andcommitted
feat: compute_consistency_group_create/delete
1 parent 0e00257 commit 88e6813

File tree

3 files changed

+204
-0
lines changed

3 files changed

+204
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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(consistencyGroupName, region) {
20+
// [START compute_consistency_group_create]
21+
// Import the Compute library
22+
const computeLib = require('@google-cloud/compute');
23+
const compute = computeLib.protos.google.cloud.compute.v1;
24+
25+
// Instantiate a resourcePoliciesClient
26+
const resourcePoliciesClient = new computeLib.ResourcePoliciesClient();
27+
// Instantiate a regionOperationsClient
28+
const regionOperationsClient = new computeLib.RegionOperationsClient();
29+
30+
/**
31+
* TODO(developer): Update/uncomment these variables before running the sample.
32+
*/
33+
// The project that contains the consistency group.
34+
const projectId = await resourcePoliciesClient.getProjectId();
35+
36+
// The region for the consistency group.
37+
// If you want to add primary disks to consistency group, use the same region as the primary disks.
38+
// If you want to add secondary disks to the consistency group, use the same region as the secondary disks.
39+
// region = 'europe-central2';
40+
41+
// The name for consistency group
42+
// consistencyGroupName = 'consistency-group-name';
43+
44+
async function callCreateConsistencyGroup() {
45+
// Create a resourcePolicyResource
46+
const resourcePolicyResource = new compute.ResourcePolicy({
47+
diskConsistencyGroupPolicy:
48+
new compute.ResourcePolicyDiskConsistencyGroupPolicy({}),
49+
name: consistencyGroupName,
50+
});
51+
52+
const [response] = await resourcePoliciesClient.insert({
53+
project: projectId,
54+
region,
55+
resourcePolicyResource,
56+
});
57+
58+
let operation = response.latestResponse;
59+
60+
// Wait for the create group operation to complete.
61+
while (operation.status !== 'DONE') {
62+
[operation] = await regionOperationsClient.wait({
63+
operation: operation.name,
64+
project: projectId,
65+
region,
66+
});
67+
}
68+
69+
console.log(`Consistency group: ${consistencyGroupName} created.`);
70+
}
71+
72+
await callCreateConsistencyGroup();
73+
// [END compute_consistency_group_create]
74+
}
75+
76+
main(...process.argv.slice(2)).catch(err => {
77+
console.error(err);
78+
process.exitCode = 1;
79+
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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(consistencyGroupName, region) {
20+
// [START compute_consistency_group_create]
21+
// Import the Compute library
22+
const computeLib = require('@google-cloud/compute');
23+
24+
// Instantiate a resourcePoliciesClient
25+
const resourcePoliciesClient = new computeLib.ResourcePoliciesClient();
26+
// Instantiate a regionOperationsClient
27+
const regionOperationsClient = new computeLib.RegionOperationsClient();
28+
/**
29+
* TODO(developer): Update/uncomment these variables before running the sample.
30+
*/
31+
// The project that contains the consistency group.
32+
const projectId = await resourcePoliciesClient.getProjectId();
33+
34+
// The region of the consistency group
35+
// region = 'europe-central2';
36+
37+
// The name of the consistency group
38+
// consistencyGroupName = 'consistency-group-name';
39+
40+
async function callCreateConsistencyGroup() {
41+
const [response] = await resourcePoliciesClient.delete({
42+
project: projectId,
43+
region,
44+
resourcePolicy: consistencyGroupName,
45+
});
46+
47+
let operation = response.latestResponse;
48+
49+
// Wait for the delete group operation to complete.
50+
while (operation.status !== 'DONE') {
51+
[operation] = await regionOperationsClient.wait({
52+
operation: operation.name,
53+
project: projectId,
54+
region,
55+
});
56+
}
57+
58+
console.log(`Consistency group: ${consistencyGroupName} deleted.`);
59+
}
60+
61+
await callCreateConsistencyGroup();
62+
// [END compute_consistency_group_create]
63+
}
64+
65+
main(...process.argv.slice(2)).catch(err => {
66+
console.error(err);
67+
process.exitCode = 1;
68+
});

compute/test/consistencyGroup.test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
const path = require('path');
20+
const assert = require('node:assert/strict');
21+
const {describe, it} = require('mocha');
22+
const uuid = require('uuid');
23+
const cp = require('child_process');
24+
25+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
26+
const cwd = path.join(__dirname, '..');
27+
28+
describe('Consistency group', async () => {
29+
const consistencyGroupName = `consistency-group-name-${uuid.v4()}`;
30+
const region = 'europe-central2';
31+
32+
it('should create a new consistency group', () => {
33+
const response = execSync(
34+
`node ./disks/consistencyGroups/createConsistencyGroup.js ${consistencyGroupName} ${region}`,
35+
{
36+
cwd,
37+
}
38+
);
39+
40+
assert(
41+
response.includes(`Consistency group: ${consistencyGroupName} created.`)
42+
);
43+
});
44+
45+
it('should delete consistency group', () => {
46+
const response = execSync(
47+
`node ./disks/consistencyGroups/deleteConsistencyGroup.js ${consistencyGroupName} ${region}`,
48+
{
49+
cwd,
50+
}
51+
);
52+
53+
assert(
54+
response.includes(`Consistency group: ${consistencyGroupName} deleted.`)
55+
);
56+
});
57+
});

0 commit comments

Comments
 (0)