Skip to content

Commit 0cdadae

Browse files
author
Joanna Grycz
committed
feat: compute_disk_regional_replicated
1 parent 0e00257 commit 0cdadae

File tree

3 files changed

+173
-1
lines changed

3 files changed

+173
-1
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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(diskName, region, zone1, zone2) {
20+
// [START compute_disk_regional_replicated]
21+
// Import the Compute library
22+
const computeLib = require('@google-cloud/compute');
23+
const compute = computeLib.protos.google.cloud.compute.v1;
24+
25+
// Instantiate a diskClient
26+
const disksClient = new computeLib.RegionDisksClient();
27+
// Instantiate a regionOperationsClient
28+
const regionOperationsClient = new computeLib.RegionOperationsClient();
29+
30+
/**
31+
* TODO(developer): Update/uncomment these variables before running the sample.
32+
*/
33+
// The project where new disk is created.
34+
const projectId = await disksClient.getProjectId();
35+
36+
// The region for the replicated disk to reside in.
37+
// The disk must be in the same region as the VM that you plan to attach it to.
38+
// region = 'europe-central2';
39+
40+
// The zones within the region where the two disk replicas are located
41+
// zone1 = 'europe-central2-a';
42+
// zone2 = 'europe-central2-b';
43+
44+
// The name of the new disk.
45+
// diskName = 'primary-disk-name';
46+
47+
// The type of replicated disk.
48+
// The default value is `pd-standard`. For Hyperdisk, specify the value `hyperdisk-balanced-high-availability`.
49+
const diskType = `regions/${region}/diskTypes/pd-standard`;
50+
// const diskType = 'pd-standard';
51+
52+
// The size of the new disk in gigabytes.
53+
const diskSizeGb = 200;
54+
55+
// Create a secondary disk identical to the primary disk.
56+
async function callCreateRegionalDiskReplicated() {
57+
// Create a replicated disk
58+
const disk = new compute.Disk({
59+
sizeGb: diskSizeGb,
60+
name: diskName,
61+
region,
62+
type: diskType,
63+
replicaZones: [
64+
`projects/${projectId}/zones/${zone1}`,
65+
`projects/${projectId}/zones/${zone2}`,
66+
],
67+
});
68+
69+
const [response] = await disksClient.insert({
70+
project: projectId,
71+
diskResource: disk,
72+
region,
73+
});
74+
75+
let operation = response.latestResponse;
76+
77+
// Wait for the create secondary disk operation to complete.
78+
while (operation.status !== 'DONE') {
79+
[operation] = await regionOperationsClient.wait({
80+
operation: operation.name,
81+
project: projectId,
82+
region,
83+
});
84+
}
85+
86+
console.log(`Regional replicated disk: ${diskName} created.`);
87+
}
88+
89+
await callCreateRegionalDiskReplicated();
90+
// [END compute_disk_regional_replicated]
91+
}
92+
93+
main(...process.argv.slice(2)).catch(err => {
94+
console.error(err);
95+
process.exitCode = 1;
96+
});
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 {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+
async function deleteDisk(region, diskName) {
30+
const disksClient = new computeLib.RegionDisksClient();
31+
const regionOperationsClient = new computeLib.RegionOperationsClient();
32+
const projectId = await disksClient.getProjectId();
33+
34+
const [response] = await disksClient.delete({
35+
project: projectId,
36+
disk: diskName,
37+
region,
38+
});
39+
let operation = response.latestResponse;
40+
41+
console.log(`Deleting ${diskName}`);
42+
43+
// Wait for the delete operation to complete.
44+
while (operation.status !== 'DONE') {
45+
[operation] = await regionOperationsClient.wait({
46+
operation: operation.name,
47+
project: projectId,
48+
region,
49+
});
50+
}
51+
}
52+
53+
describe('Create compute regional replicated disk', async () => {
54+
const diskName = `replicated-disk-${uuid.v4()}`;
55+
const region = 'europe-central2';
56+
const zone1 = 'europe-central2-a';
57+
const zone2 = 'europe-central2-b';
58+
59+
after(async () => {
60+
// Cleanup resources
61+
await deleteDisk(region, diskName);
62+
});
63+
64+
it('should create a regional replicated disk', () => {
65+
const response = execSync(
66+
`node ./disks/createRegionalReplicatedDisk.js ${diskName} ${region} ${zone1} ${zone2}`,
67+
{
68+
cwd,
69+
}
70+
);
71+
72+
assert(response.includes(`Regional replicated disk: ${diskName} created.`));
73+
});
74+
});

compute/test/util.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,9 @@ async function getStaleDisks(prefix) {
240240
}
241241

242242
async function deleteDisk(zone, diskName) {
243-
const projectId = await disksClient.getProjectId();
243+
const projectId = 'jgrycz-softserve-project';
244+
245+
// const projectId = await disksClient.getProjectId();
244246

245247
const [response] = await disksClient.delete({
246248
project: projectId,

0 commit comments

Comments
 (0)