Skip to content

Commit 87b18af

Browse files
Joanna Grycziennae
Joanna Grycz
authored andcommitted
feat: compute_consistency_group_stop_replication
1 parent a45fc2a commit 87b18af

File tree

4 files changed

+161
-2
lines changed

4 files changed

+161
-2
lines changed

compute/disks/consistencyGroups/consistencyGroupClone.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ async function main(disksClient, zoneOperationsClient) {
2424

2525
// If you want to clone regional disks,
2626
// you should use: RegionDisksClient and RegionOperationsClient.
27-
// Instantiate a disksClient
2827
// TODO(developer): Uncomment disksClient and zoneOperationsClient before running the sample.
28+
// Instantiate a disksClient
2929
// disksClient = new computeLib.DisksClient();
3030
// Instantiate a zone
3131
// zoneOperationsClient = new computeLib.ZoneOperationsClient();
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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(disksClient, zoneOperationsClient) {
20+
// [START compute_consistency_group_stop_replication]
21+
// Import the Compute library
22+
const computeLib = require('@google-cloud/compute');
23+
const compute = computeLib.protos.google.cloud.compute.v1;
24+
25+
// If disks are regional- use RegionDisksClient and RegionOperationsClient.
26+
// TODO(developer): Uncomment disksClient and zoneOperationsClient before running the sample.
27+
// Instantiate a disksClient
28+
// disksClient = new computeLib.DisksClient();
29+
// Instantiate a zoneOperationsClient
30+
// zoneOperationsClient = new computeLib.ZoneOperationsClient();
31+
32+
/**
33+
* TODO(developer): Update/uncomment these variables before running the sample.
34+
*/
35+
// The project that contains the consistency group.
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+
const disksLocation = 'europe-central2-a';
41+
42+
// The name of the consistency group.
43+
const consistencyGroupName = 'consistency-group-1';
44+
45+
// The region of the consistency group.
46+
const consistencyGroupLocation = 'europe-central2';
47+
48+
async function callStopReplication() {
49+
const [response] = await disksClient.stopGroupAsyncReplication({
50+
project: projectId,
51+
// If you use RegionDisksClient, pass region as an argument instead of zone.
52+
zone: disksLocation,
53+
disksStopGroupAsyncReplicationResourceResource:
54+
new compute.DisksStopGroupAsyncReplicationResource({
55+
resourcePolicy: [
56+
`https://www.googleapis.com/compute/v1/projects/${projectId}/regions/${consistencyGroupLocation}/resourcePolicies/${consistencyGroupName}`,
57+
],
58+
}),
59+
});
60+
61+
let operation = response.latestResponse;
62+
63+
// Wait for the operation to complete.
64+
while (operation.status !== 'DONE') {
65+
[operation] = await zoneOperationsClient.wait({
66+
operation: operation.name,
67+
project: projectId,
68+
// If you use RegionDisksClient, pass region as an argument instead of zone.
69+
zone: operation.zone.split('/').pop(),
70+
});
71+
}
72+
73+
const message = `Replication stopped for consistency group: ${consistencyGroupName}.`;
74+
console.log(message);
75+
return message;
76+
}
77+
78+
return await callStopReplication();
79+
// [END compute_consistency_group_stop_replication]
80+
}
81+
82+
module.exports = main;
83+
84+
// TODO(developer): Uncomment below lines before running the sample.
85+
// main(...process.argv.slice(2)).catch(err => {
86+
// console.error(err);
87+
// process.exitCode = 1;
88+
// });

compute/test/consistencyGroupClone.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('Compute disks consistency group clone', async () => {
6262
disksClientMock,
6363
zoneOperationsClientMock
6464
);
65-
console.log(response);
65+
6666
assert.equal(
6767
response,
6868
`Disks cloned from consistency group: ${consistencyGroupName}.`

compute/test/stopReplication.test.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 {beforeEach, afterEach, describe, it} = require('mocha');
20+
const assert = require('node:assert/strict');
21+
const sinon = require('sinon');
22+
const stopReplication = require('../disks/consistencyGroups/stopReplication.js');
23+
24+
describe('Consistency group', async () => {
25+
const consistencyGroupName = 'consistency-group-1';
26+
let disksClientMock;
27+
let zoneOperationsClientMock;
28+
29+
beforeEach(() => {
30+
disksClientMock = {
31+
getProjectId: sinon.stub().resolves('project_id'),
32+
stopGroupAsyncReplication: sinon.stub().resolves([
33+
{
34+
name: consistencyGroupName,
35+
latestResponse: {
36+
status: 'DONE',
37+
name: 'operation-1234567890',
38+
zone: {
39+
value: 'us-central1-a',
40+
},
41+
},
42+
},
43+
]),
44+
};
45+
zoneOperationsClientMock = {
46+
wait: sinon.stub().resolves([
47+
{
48+
latestResponse: {
49+
status: 'DONE',
50+
},
51+
},
52+
]),
53+
};
54+
});
55+
56+
afterEach(() => {
57+
sinon.restore();
58+
});
59+
60+
it('should stop replication for disks', async () => {
61+
const response = await stopReplication(
62+
disksClientMock,
63+
zoneOperationsClientMock
64+
);
65+
66+
assert.equal(
67+
response,
68+
`Replication stopped for consistency group: ${consistencyGroupName}.`
69+
);
70+
});
71+
});

0 commit comments

Comments
 (0)