|
| 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 | + consistencyGroupName, |
| 21 | + consistencyGroupLocation, |
| 22 | + diskName, |
| 23 | + diskLocation |
| 24 | +) { |
| 25 | + // [START compute_consistency_group_remove_disk] |
| 26 | + // Import the Compute library |
| 27 | + const computeLib = require('@google-cloud/compute'); |
| 28 | + const compute = computeLib.protos.google.cloud.compute.v1; |
| 29 | + |
| 30 | + // If you want to remove regional disk, |
| 31 | + // you should use: RegionDisksClient and RegionOperationsClient. |
| 32 | + // Instantiate a disksClient |
| 33 | + const disksClient = new computeLib.DisksClient(); |
| 34 | + // Instantiate a zone |
| 35 | + const zoneOperationsClient = new computeLib.ZoneOperationsClient(); |
| 36 | + |
| 37 | + /** |
| 38 | + * TODO(developer): Update/uncomment these variables before running the sample. |
| 39 | + */ |
| 40 | + // The project that contains the disk. |
| 41 | + const projectId = await disksClient.getProjectId(); |
| 42 | + |
| 43 | + // The name of the disk. |
| 44 | + // diskName = 'disk-name'; |
| 45 | + |
| 46 | + // If you use RegionDisksClient- define region, if DisksClient- define zone. |
| 47 | + // The zone or region of the disk. |
| 48 | + // diskLocation = 'europe-central2-a'; |
| 49 | + |
| 50 | + // The name of the consistency group. |
| 51 | + // consistencyGroupName = 'consistency-group-name'; |
| 52 | + |
| 53 | + // The region of the consistency group. |
| 54 | + // consistencyGroupLocation = 'europe-central2'; |
| 55 | + |
| 56 | + async function callDeleteDiskFromConsistencyGroup() { |
| 57 | + const [response] = await disksClient.removeResourcePolicies({ |
| 58 | + disk: diskName, |
| 59 | + project: projectId, |
| 60 | + // If you use RegionDisksClient, pass region as an argument instead of zone. |
| 61 | + zone: diskLocation, |
| 62 | + disksRemoveResourcePoliciesRequestResource: |
| 63 | + new compute.DisksRemoveResourcePoliciesRequest({ |
| 64 | + resourcePolicies: [ |
| 65 | + `https://www.googleapis.com/compute/v1/projects/${projectId}/regions/${consistencyGroupLocation}/resourcePolicies/${consistencyGroupName}`, |
| 66 | + ], |
| 67 | + }), |
| 68 | + }); |
| 69 | + |
| 70 | + let operation = response.latestResponse; |
| 71 | + |
| 72 | + // Wait for the delete disk operation to complete. |
| 73 | + while (operation.status !== 'DONE') { |
| 74 | + [operation] = await zoneOperationsClient.wait({ |
| 75 | + operation: operation.name, |
| 76 | + project: projectId, |
| 77 | + // If you use RegionDisksClient, pass region as an argument instead of zone. |
| 78 | + zone: operation.zone.split('/').pop(), |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + console.log( |
| 83 | + `Disk: ${diskName} deleted from consistency group: ${consistencyGroupName}.` |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + await callDeleteDiskFromConsistencyGroup(); |
| 88 | + // [END compute_consistency_group_remove_disk] |
| 89 | +} |
| 90 | + |
| 91 | +main(...process.argv.slice(2)).catch(err => { |
| 92 | + console.error(err); |
| 93 | + process.exitCode = 1; |
| 94 | +}); |
0 commit comments