Skip to content

Commit 697100d

Browse files
Joanna Grycziennae
authored andcommitted
feat: compute_disk_start/stop_replication
1 parent 889837c commit 697100d

File tree

6 files changed

+207
-9
lines changed

6 files changed

+207
-9
lines changed

compute/disks/createCustomSecondaryDisk.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ async function main(
2727
const computeLib = require('@google-cloud/compute');
2828
const compute = computeLib.protos.google.cloud.compute.v1;
2929

30+
// If you want to create regional disk, you should use: RegionDisksClient and RegionOperationsClient.
3031
// Instantiate a diskClient
3132
const disksClient = new computeLib.DisksClient();
3233
// Instantiate a zoneOperationsClient
@@ -38,17 +39,19 @@ async function main(
3839
// The project for the secondary disk.
3940
const secondaryProjectId = await disksClient.getProjectId();
4041

41-
// The zone for the secondary disk. The primary and secondary disks must be in different regions.
42-
// secondaryLocation = 'europe-west4-a';
42+
// The zone or region for the secondary disk. The primary and secondary disks must be in different regions.
43+
// If you use RegionDisksClient- define region, if DisksClient- define zone.
44+
// secondaryLocation = 'us-central1-a';
4345

4446
// The name of the secondary disk.
4547
// secondaryDiskName = 'secondary-disk-name';
4648

4749
// The project that contains the primary disk.
4850
const primaryProjectId = await disksClient.getProjectId();
4951

50-
// The zone for the primary disk.
51-
// primaryLocation = 'europe-central2-b';
52+
// The zone or region for the primary disk.
53+
// If you use RegionDisksClient- define region, if DisksClient- define zone.
54+
// primaryLocation = 'us-central1-b';
5255

5356
// The name of the primary disk that the secondary disk receives data from.
5457
// primaryDiskName = 'primary-disk-name';
@@ -65,6 +68,7 @@ async function main(
6568
const disk = new compute.Disk({
6669
sizeGb: diskSizeGb,
6770
name: secondaryDiskName,
71+
// If you use RegionDisksClient, pass region as an argument instead of zone
6872
zone: secondaryLocation,
6973
type: diskType,
7074
asyncPrimaryDisk: new compute.DiskAsyncReplication({
@@ -73,6 +77,7 @@ async function main(
7377
disk: `projects/${primaryProjectId}/zones/${primaryLocation}/disks/${primaryDiskName}`,
7478
}),
7579
// Specify additional guest OS features.
80+
// To learn more about OS features, open: `https://cloud.google.com/compute/docs/disks/async-pd/configure?authuser=0#secondary2`.
7681
// You don't need to include the guest OS features of the primary disk.
7782
// The secondary disk automatically inherits the guest OS features of the primary disk.
7883
guestOsFeatures: [
@@ -90,6 +95,7 @@ async function main(
9095

9196
const [response] = await disksClient.insert({
9297
project: secondaryProjectId,
98+
// If you use RegionDisksClient, pass region as an argument instead of zone
9399
zone: secondaryLocation,
94100
diskResource: disk,
95101
});
@@ -101,6 +107,7 @@ async function main(
101107
[operation] = await zoneOperationsClient.wait({
102108
operation: operation.name,
103109
project: secondaryProjectId,
110+
// If you use RegionOperationsClient, pass region as an argument instead of zone
104111
zone: operation.zone.split('/').pop(),
105112
});
106113
}

compute/disks/createRegionalSecondaryDisk.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async function main(
3939
const secondaryProjectId = await regionDisksClient.getProjectId();
4040

4141
// The region for the secondary disk.
42-
// secondaryLocation = 'europe-west4';
42+
// secondaryLocation = 'us-central1';
4343

4444
// The name of the secondary disk.
4545
// secondaryDiskName = 'secondary-disk-name';
@@ -48,7 +48,7 @@ async function main(
4848
const primaryProjectId = await regionDisksClient.getProjectId();
4949

5050
// The region for the primary disk.
51-
// primaryLocation = 'europe-central2';
51+
// primaryLocation = 'us-central2';
5252

5353
// The name of the primary disk that the secondary disk receives data from.
5454
// primaryDiskName = 'primary-disk-name';

compute/disks/createZonalSecondaryDisk.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async function main(
3939
const secondaryProjectId = await disksClient.getProjectId();
4040

4141
// The zone for the secondary disk. The primary and secondary disks must be in different regions.
42-
// secondaryLocation = 'europe-west4-a';
42+
// secondaryLocation = 'us-central1-a';
4343

4444
// The name of the secondary disk.
4545
// secondaryDiskName = 'secondary-disk-name';
@@ -48,7 +48,7 @@ async function main(
4848
const primaryProjectId = await disksClient.getProjectId();
4949

5050
// The zone for the primary disk.
51-
// primaryLocation = 'europe-central2-b';
51+
// primaryLocation = 'us-central1-b';
5252

5353
// The name of the primary disk that the secondary disk receives data from.
5454
// primaryDiskName = 'primary-disk-name';

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 = 'us-central1-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 = 'us-central1-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 = 'us-central1-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)