Skip to content

Commit a45fc2a

Browse files
Joanna Grycziennae
authored andcommitted
feat: compute_consistency_group_clone
1 parent cf8e417 commit a45fc2a

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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_clone]
21+
// Import the Compute library
22+
const computeLib = require('@google-cloud/compute');
23+
const compute = computeLib.protos.google.cloud.compute.v1;
24+
25+
// If you want to clone regional disks,
26+
// you should use: RegionDisksClient and RegionOperationsClient.
27+
// Instantiate a disksClient
28+
// TODO(developer): Uncomment disksClient and zoneOperationsClient before running the sample.
29+
// disksClient = new computeLib.DisksClient();
30+
// Instantiate a zone
31+
// zoneOperationsClient = new computeLib.ZoneOperationsClient();
32+
33+
/**
34+
* TODO(developer): Update/uncomment these variables before running the sample.
35+
*/
36+
// The project that contains the disks.
37+
const projectId = await disksClient.getProjectId();
38+
39+
// If you use RegionDisksClient- define region, if DisksClient- define zone.
40+
// The zone or region that the disks in the consistency group are located in. The clones are created in this location.
41+
// diskLocation = 'europe-central2-a';
42+
const disksLocation = 'europe-north1-a';
43+
44+
// The name of the consistency group, that contains secondary disks to clone.
45+
// consistencyGroupName = 'consistency-group-name';
46+
const consistencyGroupName = 'consistency-group-1';
47+
48+
// The region of the consistency group.
49+
const consistencyGroupLocation = 'europe-north1';
50+
51+
async function callConsistencyGroupClone() {
52+
const [response] = await disksClient.bulkInsert({
53+
project: projectId,
54+
// If you use RegionDisksClient, pass region as an argument instead of zone.
55+
zone: disksLocation,
56+
bulkInsertDiskResourceResource: new compute.BulkInsertDiskResource({
57+
sourceConsistencyGroupPolicy: [
58+
`https://www.googleapis.com/compute/v1/projects/${projectId}/regions/${consistencyGroupLocation}/resourcePolicies/${consistencyGroupName}`,
59+
],
60+
}),
61+
});
62+
63+
let operation = response.latestResponse;
64+
65+
// Wait for the clone operation to complete.
66+
while (operation.status !== 'DONE') {
67+
[operation] = await zoneOperationsClient.wait({
68+
operation: operation.name,
69+
project: projectId,
70+
// If you use RegionDisksClient, pass region as an argument instead of zone.
71+
zone: operation.zone.split('/').pop(),
72+
});
73+
}
74+
75+
const message = `Disks cloned from consistency group: ${consistencyGroupName}.`;
76+
console.log(message);
77+
return message;
78+
}
79+
80+
return await callConsistencyGroupClone();
81+
// [END compute_consistency_group_clone]
82+
}
83+
84+
module.exports = main;
85+
86+
// TODO(developer): Uncomment below lines before running the sample.
87+
// main(...process.argv.slice(2)).catch(err => {
88+
// console.error(err);
89+
// process.exitCode = 1;
90+
// });
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 consistencyGroupClone = require('../disks/consistencyGroups/consistencyGroupClone.js');
23+
24+
describe('Compute disks consistency group clone', 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+
bulkInsert: 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 create clones from disks in consitency group', async () => {
61+
const response = await consistencyGroupClone(
62+
disksClientMock,
63+
zoneOperationsClientMock
64+
);
65+
console.log(response);
66+
assert.equal(
67+
response,
68+
`Disks cloned from consistency group: ${consistencyGroupName}.`
69+
);
70+
});
71+
});

0 commit comments

Comments
 (0)