Skip to content

Commit 889837c

Browse files
Joanna Grycziennae
authored andcommitted
feat: compute_disk_create_secondary_custom
1 parent d23b7cc commit 889837c

File tree

2 files changed

+221
-0
lines changed

2 files changed

+221
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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_custom]
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+
// Specify additional guest OS features.
76+
// You don't need to include the guest OS features of the primary disk.
77+
// The secondary disk automatically inherits the guest OS features of the primary disk.
78+
guestOsFeatures: [
79+
new compute.GuestOsFeature({
80+
type: 'NEW_FEATURE_ID_1',
81+
}),
82+
],
83+
// Assign additional labels to the secondary disk.
84+
// You don't need to include the labels of the primary disk.
85+
// The secondary disk automatically inherits the labels from the primary disk
86+
labels: {
87+
key: 'value',
88+
},
89+
});
90+
91+
const [response] = await disksClient.insert({
92+
project: secondaryProjectId,
93+
zone: secondaryLocation,
94+
diskResource: disk,
95+
});
96+
97+
let operation = response.latestResponse;
98+
99+
// Wait for the create secondary disk operation to complete.
100+
while (operation.status !== 'DONE') {
101+
[operation] = await zoneOperationsClient.wait({
102+
operation: operation.name,
103+
project: secondaryProjectId,
104+
zone: operation.zone.split('/').pop(),
105+
});
106+
}
107+
108+
console.log(`Custom secondary disk: ${secondaryDiskName} created.`);
109+
}
110+
111+
await callCreateComputeSecondaryDisk();
112+
// [END compute_disk_create_secondary_custom]
113+
}
114+
115+
main(...process.argv.slice(2)).catch(err => {
116+
console.error(err);
117+
process.exitCode = 1;
118+
});
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
const disksClient = new computeLib.DisksClient();
31+
const zoneOperationsClient = new computeLib.ZoneOperationsClient();
32+
33+
async function createDisk(diskName, zone, projectId) {
34+
const [response] = await disksClient.insert({
35+
project: projectId,
36+
zone,
37+
diskResource: {
38+
sizeGb: 10,
39+
name: diskName,
40+
zone,
41+
type: `zones/${zone}/diskTypes/pd-balanced`,
42+
},
43+
});
44+
45+
let operation = response.latestResponse;
46+
47+
// Wait for the create disk operation to complete.
48+
while (operation.status !== 'DONE') {
49+
[operation] = await zoneOperationsClient.wait({
50+
operation: operation.name,
51+
project: projectId,
52+
zone: operation.zone.split('/').pop(),
53+
});
54+
}
55+
56+
console.log(`Disk: ${diskName} created.`);
57+
}
58+
59+
describe('Create compute custom secondary disk', async () => {
60+
const prefix = 'custom-disk';
61+
const secondaryDiskName = `${prefix}-secondary-${uuid.v4()}`;
62+
const primaryDiskName = `${prefix}-primary-${uuid.v4()}`;
63+
const secondaryRegion = 'europe-west4';
64+
const primaryRegion = 'europe-central2';
65+
const secondaryZone = `${secondaryRegion}-a`;
66+
const primaryZone = `${primaryRegion}-a`;
67+
let projectId;
68+
69+
before(async () => {
70+
projectId = await disksClient.getProjectId();
71+
await createDisk(primaryDiskName, primaryZone, projectId);
72+
});
73+
74+
after(async () => {
75+
// Cleanup resources
76+
const disks = await getStaleDisks(prefix);
77+
await Promise.all(disks.map(disk => deleteDisk(disk.zone, disk.diskName)));
78+
});
79+
80+
it('should create a custom secondary disk', async () => {
81+
const expectedProperties = {
82+
guestOsFeatures: [{type: 'FEATURE_TYPE_UNSPECIFIED', _type: 'type'}],
83+
labels: {
84+
key: 'value',
85+
},
86+
};
87+
execSync(
88+
`node ./disks/createCustomSecondaryDisk.js ${secondaryDiskName} ${secondaryZone} ${primaryDiskName} ${primaryZone}`,
89+
{
90+
cwd,
91+
}
92+
);
93+
94+
const [disk] = await disksClient.get({
95+
project: projectId,
96+
zone: secondaryZone,
97+
disk: secondaryDiskName,
98+
});
99+
100+
assert.deepEqual(disk.guestOsFeatures, expectedProperties.guestOsFeatures);
101+
assert.deepEqual(disk.labels, expectedProperties.labels);
102+
});
103+
});

0 commit comments

Comments
 (0)