Skip to content

Commit 66cdce0

Browse files
author
Joanna Grycz
committed
feat: compute_disk_start/stop_replication
1 parent 96a8b91 commit 66cdce0

File tree

3 files changed

+192
-1
lines changed

3 files changed

+192
-1
lines changed

compute/disks/startReplication.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
secondaryDiskName,
21+
secondaryLocation,
22+
primaryDiskName,
23+
primaryLocation
24+
) {
25+
// [START compute_disk_start_replication]
26+
// Import the Compute library
27+
const computeLib = require('@google-cloud/compute');
28+
const compute = computeLib.protos.google.cloud.compute.v1;
29+
30+
// Instantiate a diskClient
31+
const disksClient = new computeLib.DisksClient();
32+
// Instantiate a zoneOperationsClient
33+
const zoneOperationsClient = new computeLib.ZoneOperationsClient();
34+
35+
/**
36+
* TODO(developer): Update/uncomment these variables before running the sample.
37+
*/
38+
// The project of the secondary disk.
39+
const secondaryProjectId = await disksClient.getProjectId();
40+
41+
// The zone of the secondary disk.
42+
// secondaryLocation = 'europe-west4-a';
43+
44+
// The name of the secondary disk.
45+
// secondaryDiskName = 'secondary-disk-name';
46+
47+
// The project of the primary disk.
48+
const primaryProjectId = await disksClient.getProjectId();
49+
50+
// The zone of the primary disk.
51+
// primaryLocation = 'europe-central2-a';
52+
53+
// The name of the primary disk.
54+
// primaryDiskName = 'primary-disk-name';
55+
56+
// Start replication
57+
async function callStartReplication() {
58+
const [response] = await disksClient.startAsyncReplication({
59+
project: secondaryProjectId,
60+
zone: primaryLocation,
61+
disk: primaryDiskName,
62+
disksStartAsyncReplicationRequestResource:
63+
new compute.DisksStartAsyncReplicationRequest({
64+
asyncSecondaryDisk: `projects/${primaryProjectId}/zones/${secondaryLocation}/disks/${secondaryDiskName}`,
65+
}),
66+
});
67+
68+
let operation = response.latestResponse;
69+
70+
// Wait for the operation to complete.
71+
while (operation.status !== 'DONE') {
72+
[operation] = await zoneOperationsClient.wait({
73+
operation: operation.name,
74+
project: secondaryProjectId,
75+
zone: operation.zone.split('/').pop(),
76+
});
77+
}
78+
79+
console.log(
80+
`Data replication from primary disk: ${primaryDiskName} to secondary disk: ${secondaryDiskName} started.`
81+
);
82+
}
83+
84+
await callStartReplication();
85+
// [END compute_disk_start_replication]
86+
}
87+
88+
main(...process.argv.slice(2)).catch(err => {
89+
console.error(err);
90+
process.exitCode = 1;
91+
});

compute/disks/stopReplication.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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(primaryDiskName, primaryLocation) {
20+
// [START compute_disk_stop_replication]
21+
// Import the Compute library
22+
const computeLib = require('@google-cloud/compute');
23+
24+
// Instantiate a diskClient
25+
const disksClient = new computeLib.DisksClient();
26+
// Instantiate a zoneOperationsClient
27+
const zoneOperationsClient = new computeLib.ZoneOperationsClient();
28+
29+
/**
30+
* TODO(developer): Update/uncomment these variables before running the sample.
31+
*/
32+
// The project that contains the primary disk.
33+
const primaryProjectId = await disksClient.getProjectId();
34+
35+
// The zone of the primary disk.
36+
// primaryLocation = 'europe-central2-a';
37+
38+
// The name of the primary disk.
39+
// primaryDiskName = 'primary-disk-name';
40+
41+
// Stop replication
42+
async function callStopReplication() {
43+
const [response] = await disksClient.stopAsyncReplication({
44+
project: primaryProjectId,
45+
zone: primaryLocation,
46+
disk: primaryDiskName,
47+
});
48+
49+
let operation = response.latestResponse;
50+
51+
// Wait for the operation to complete.
52+
while (operation.status !== 'DONE') {
53+
[operation] = await zoneOperationsClient.wait({
54+
operation: operation.name,
55+
project: primaryProjectId,
56+
zone: operation.zone.split('/').pop(),
57+
});
58+
}
59+
60+
console.log(`Replication for primary disk: ${primaryDiskName} stopped.`);
61+
}
62+
63+
await callStopReplication();
64+
// [END compute_disk_stop_replication]
65+
}
66+
67+
main(...process.argv.slice(2)).catch(err => {
68+
console.error(err);
69+
process.exitCode = 1;
70+
});

compute/test/createZonalSecondaryDisk.test.js renamed to compute/test/zonalSecondaryDisk.test.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ async function createDisk(diskName, zone) {
5757
console.log(`Disk: ${diskName} created.`);
5858
}
5959

60-
describe('Create compute zonal secondary disk', async () => {
60+
describe('Compute zonal secondary disk', async () => {
6161
const prefix = 'zonal-disk';
6262
const secondaryDiskName = `${prefix}-secondary-${uuid.v4()}`;
6363
const primaryDiskName = `${prefix}-primary-${uuid.v4()}`;
@@ -86,4 +86,34 @@ describe('Create compute zonal secondary disk', async () => {
8686

8787
assert(response.includes(`Secondary disk: ${secondaryDiskName} created.`));
8888
});
89+
90+
it('should start replication', () => {
91+
const response = execSync(
92+
`node ./disks/startReplication.js ${secondaryDiskName} ${secondaryZone} ${primaryDiskName} ${primaryZone}`,
93+
{
94+
cwd,
95+
}
96+
);
97+
98+
assert(
99+
response.includes(
100+
`Data replication from primary disk: ${primaryDiskName} to secondary disk: ${secondaryDiskName} started.`
101+
)
102+
);
103+
});
104+
105+
it('should stop replication', () => {
106+
const response = execSync(
107+
`node ./disks/stopReplication.js ${primaryDiskName} ${primaryZone}`,
108+
{
109+
cwd,
110+
}
111+
);
112+
113+
assert(
114+
response.includes(
115+
`Replication for primary disk: ${primaryDiskName} stopped.`
116+
)
117+
);
118+
});
89119
});

0 commit comments

Comments
 (0)