Skip to content

Commit 2de0774

Browse files
author
Joanna Grycz
committed
feat: compute_disk_create_secondary
1 parent 5bd0c38 commit 2de0774

File tree

2 files changed

+191
-0
lines changed

2 files changed

+191
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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]
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 for the secondary disk.
39+
const secondaryProjectId = await disksClient.getProjectId();
40+
41+
// The zone for the secondary disk. The primary and secondary disks must be in different regions.
42+
// secondaryLocation = 'europe-west4-a';
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 disksClient.getProjectId();
49+
50+
// The zone for the primary disk.
51+
// primaryLocation = 'europe-central2-b';
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 = `zones/${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 callCreateComputeSecondaryDisk() {
64+
// Create a secondary disk
65+
const disk = new compute.Disk({
66+
sizeGb: diskSizeGb,
67+
name: secondaryDiskName,
68+
zone: secondaryLocation,
69+
type: diskType,
70+
asyncPrimaryDisk: new compute.DiskAsyncReplication({
71+
// Make sure that the primary disk supports asynchronous replication.
72+
// Only certain persistent disk types, like `pd-balanced` and `pd-ssd`, are eligible.
73+
disk: `projects/${primaryProjectId}/zones/${primaryLocation}/disks/${primaryDiskName}`,
74+
}),
75+
});
76+
77+
const [response] = await disksClient.insert({
78+
project: secondaryProjectId,
79+
zone: secondaryLocation,
80+
diskResource: disk,
81+
});
82+
83+
let operation = response.latestResponse;
84+
85+
// Wait for the create secondary disk operation to complete.
86+
while (operation.status !== 'DONE') {
87+
[operation] = await zoneOperationsClient.wait({
88+
operation: operation.name,
89+
project: secondaryProjectId,
90+
zone: operation.zone.split('/').pop(),
91+
});
92+
}
93+
94+
console.log(`Secondary disk: ${secondaryDiskName} created.`);
95+
}
96+
97+
await callCreateComputeSecondaryDisk();
98+
// [END compute_disk_create_secondary]
99+
}
100+
101+
main(...process.argv.slice(2)).catch(err => {
102+
console.error(err);
103+
process.exitCode = 1;
104+
});
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
const {getStaleDisks, deleteDisk} = require('./util');
26+
27+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
28+
const cwd = path.join(__dirname, '..');
29+
30+
async function createDisk(diskName, zone) {
31+
const disksClient = new computeLib.DisksClient();
32+
const zoneOperationsClient = new computeLib.ZoneOperationsClient();
33+
const projectId = await disksClient.getProjectId();
34+
35+
const [response] = await disksClient.insert({
36+
project: projectId,
37+
zone,
38+
diskResource: {
39+
sizeGb: 10,
40+
name: diskName,
41+
zone,
42+
type: `zones/${zone}/diskTypes/pd-balanced`,
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 zoneOperationsClient.wait({
51+
operation: operation.name,
52+
project: projectId,
53+
zone: operation.zone.split('/').pop(),
54+
});
55+
}
56+
57+
console.log(`Disk: ${diskName} created.`);
58+
}
59+
60+
describe('Create compute secondary disk', async () => {
61+
const prefix = 'compute-disk';
62+
const secondaryDiskName = `${prefix}-secondary-${uuid.v4()}`;
63+
const primaryDiskName = `${prefix}-primary-${uuid.v4()}`;
64+
const secondaryZone = 'europe-west4-a';
65+
const primaryZone = 'europe-central2-b';
66+
67+
before(async () => {
68+
await createDisk(primaryDiskName, primaryZone);
69+
});
70+
71+
after(async () => {
72+
// Cleanup resources
73+
const disks = await getStaleDisks(prefix);
74+
await Promise.all(disks.map(disk => deleteDisk(disk.zone, disk.diskName)));
75+
});
76+
77+
it('should create a zonal secondary disk', () => {
78+
const response = execSync(
79+
`node ./disks/createSecondaryDisk.js ${secondaryDiskName} ${secondaryZone} ${primaryDiskName} ${primaryZone}`,
80+
{
81+
cwd,
82+
}
83+
);
84+
85+
assert(response.includes(`Secondary disk: ${secondaryDiskName} created.`));
86+
});
87+
});

0 commit comments

Comments
 (0)