|
| 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 | +}); |
0 commit comments