Skip to content

Commit d23b7cc

Browse files
Joanna Grycziennae
authored andcommitted
feat: compute_disk_create_secondary_regional
1 parent 1b1f20d commit d23b7cc

File tree

4 files changed

+234
-5
lines changed

4 files changed

+234
-5
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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_create_secondary_regional]
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 regionDisksClient
31+
const regionDisksClient = new computeLib.RegionDisksClient();
32+
// Instantiate a regionOperationsClient
33+
const regionOperationsClient = new computeLib.RegionOperationsClient();
34+
35+
/**
36+
* TODO(developer): Update/uncomment these variables before running the sample.
37+
*/
38+
// The project for the secondary disk.
39+
const secondaryProjectId = await regionDisksClient.getProjectId();
40+
41+
// The region for the secondary disk.
42+
// secondaryLocation = 'europe-west4';
43+
44+
// The name of the secondary disk.
45+
// secondaryDiskName = 'secondary-disk-name';
46+
47+
// The project that contains the primary disk.
48+
const primaryProjectId = await regionDisksClient.getProjectId();
49+
50+
// The region for the primary disk.
51+
// primaryLocation = 'europe-central2';
52+
53+
// The name of the primary disk that the secondary disk receives data from.
54+
// primaryDiskName = 'primary-disk-name';
55+
56+
// The disk type. Must be one of `pd-ssd` or `pd-balanced`.
57+
const diskType = `regions/${secondaryLocation}/diskTypes/pd-balanced`;
58+
59+
// The size of the secondary disk in gigabytes.
60+
const diskSizeGb = 10;
61+
62+
// Create a secondary disk identical to the primary disk.
63+
async function callCreateComputeRegionalSecondaryDisk() {
64+
// Create a secondary disk
65+
const disk = new compute.Disk({
66+
sizeGb: diskSizeGb,
67+
name: secondaryDiskName,
68+
region: secondaryLocation,
69+
type: diskType,
70+
replicaZones: [
71+
`zones/${secondaryLocation}-a`,
72+
`zones/${secondaryLocation}-b`,
73+
],
74+
asyncPrimaryDisk: new compute.DiskAsyncReplication({
75+
// Make sure that the primary disk supports asynchronous replication.
76+
// Only certain persistent disk types, like `pd-balanced` and `pd-ssd`, are eligible.
77+
disk: `projects/${primaryProjectId}/regions/${primaryLocation}/disks/${primaryDiskName}`,
78+
}),
79+
});
80+
81+
const [response] = await regionDisksClient.insert({
82+
project: secondaryProjectId,
83+
diskResource: disk,
84+
region: secondaryLocation,
85+
});
86+
87+
let operation = response.latestResponse;
88+
89+
// Wait for the create secondary disk operation to complete.
90+
while (operation.status !== 'DONE') {
91+
[operation] = await regionOperationsClient.wait({
92+
operation: operation.name,
93+
project: secondaryProjectId,
94+
region: secondaryLocation,
95+
});
96+
}
97+
98+
console.log(`Secondary disk: ${secondaryDiskName} created.`);
99+
}
100+
101+
await callCreateComputeRegionalSecondaryDisk();
102+
// [END compute_disk_create_secondary_regional]
103+
}
104+
105+
main(...process.argv.slice(2)).catch(err => {
106+
console.error(err);
107+
process.exitCode = 1;
108+
});
File renamed without changes.
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 uuid = require('uuid');
22+
const {before, after, describe, it} = require('mocha');
23+
const cp = require('child_process');
24+
const computeLib = require('@google-cloud/compute');
25+
26+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
27+
const cwd = path.join(__dirname, '..');
28+
29+
const disksClient = new computeLib.RegionDisksClient();
30+
const regionOperationsClient = new computeLib.RegionOperationsClient();
31+
32+
async function createDisk(diskName, region) {
33+
const projectId = await disksClient.getProjectId();
34+
const [response] = await disksClient.insert({
35+
project: projectId,
36+
region,
37+
diskResource: {
38+
sizeGb: 10,
39+
name: diskName,
40+
region,
41+
type: `regions/${region}/diskTypes/pd-balanced`,
42+
replicaZones: [`zones/${region}-a`, `zones/${region}-b`],
43+
},
44+
});
45+
46+
let operation = response.latestResponse;
47+
48+
// Wait for the create disk operation to complete.
49+
while (operation.status !== 'DONE') {
50+
[operation] = await regionOperationsClient.wait({
51+
operation: operation.name,
52+
project: projectId,
53+
region,
54+
});
55+
}
56+
57+
console.log(`Disk: ${diskName} created.`);
58+
}
59+
60+
async function deleteDisk(region, diskName) {
61+
const projectId = await disksClient.getProjectId();
62+
const [response] = await disksClient.delete({
63+
project: projectId,
64+
disk: diskName,
65+
region,
66+
});
67+
let operation = response.latestResponse;
68+
69+
console.log(`Deleting ${diskName}`);
70+
71+
// Wait for the delete operation to complete.
72+
while (operation.status !== 'DONE') {
73+
[operation] = await regionOperationsClient.wait({
74+
operation: operation.name,
75+
project: projectId,
76+
region,
77+
});
78+
}
79+
}
80+
81+
describe('Create compute regional secondary disk', async () => {
82+
const prefix = 'regional-disk';
83+
const secondaryDiskName = `${prefix}-secondary-${uuid.v4()}`;
84+
const primaryDiskName = `${prefix}-primary-${uuid.v4()}`;
85+
const secondaryRegion = 'europe-west4';
86+
const primaryRegion = 'europe-central2';
87+
const disks = [
88+
{
89+
diskName: secondaryDiskName,
90+
region: secondaryRegion,
91+
},
92+
{
93+
diskName: primaryDiskName,
94+
region: primaryRegion,
95+
},
96+
];
97+
98+
before(async () => {
99+
await createDisk(primaryDiskName, primaryRegion);
100+
});
101+
102+
after(async () => {
103+
// Cleanup resources
104+
await Promise.all(
105+
disks.map(disk => deleteDisk(disk.region, disk.diskName))
106+
);
107+
});
108+
109+
it('should create a regional secondary disk', () => {
110+
const response = execSync(
111+
`node ./disks/createRegionalSecondaryDisk.js ${secondaryDiskName} ${secondaryRegion} ${primaryDiskName} ${primaryRegion}`,
112+
{
113+
cwd,
114+
}
115+
);
116+
117+
assert(response.includes(`Secondary disk: ${secondaryDiskName} created.`));
118+
});
119+
});

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

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

60-
describe('Create compute secondary disk', async () => {
61-
const prefix = 'compute-disk';
60+
describe('Create compute zonal secondary disk', async () => {
61+
const prefix = 'zonal-disk';
6262
const secondaryDiskName = `${prefix}-secondary-${uuid.v4()}`;
6363
const primaryDiskName = `${prefix}-primary-${uuid.v4()}`;
64-
const secondaryZone = 'europe-west4-a';
65-
const primaryZone = 'europe-central2-b';
64+
const secondaryRegion = 'europe-west4';
65+
const primaryRegion = 'europe-central2';
66+
const secondaryZone = `${secondaryRegion}-a`;
67+
const primaryZone = `${primaryRegion}-a`;
6668

6769
before(async () => {
6870
await createDisk(primaryDiskName, primaryZone);
@@ -76,7 +78,7 @@ describe('Create compute secondary disk', async () => {
7678

7779
it('should create a zonal secondary disk', () => {
7880
const response = execSync(
79-
`node ./disks/createSecondaryDisk.js ${secondaryDiskName} ${secondaryZone} ${primaryDiskName} ${primaryZone}`,
81+
`node ./disks/createZonalSecondaryDisk.js ${secondaryDiskName} ${secondaryZone} ${primaryDiskName} ${primaryZone}`,
8082
{
8183
cwd,
8284
}

0 commit comments

Comments
 (0)