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