|
| 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 | + disksLocation |
| 23 | +) { |
| 24 | + // [START compute_consistency_group_disks_list] |
| 25 | + // Import the Compute library |
| 26 | + const computeLib = require('@google-cloud/compute'); |
| 27 | + |
| 28 | + // If you want to get regional disks, you should use: RegionDisksClient. |
| 29 | + // Instantiate a disksClient |
| 30 | + const disksClient = new computeLib.DisksClient(); |
| 31 | + |
| 32 | + /** |
| 33 | + * TODO(developer): Update/uncomment these variables before running the sample. |
| 34 | + */ |
| 35 | + // The project that contains the disks. |
| 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 | + // disksLocation = 'europe-central2-a'; |
| 41 | + |
| 42 | + // The name of the consistency group. |
| 43 | + // consistencyGroupName = 'consistency-group-name'; |
| 44 | + |
| 45 | + // The region of the consistency group. |
| 46 | + // consistencyGroupLocation = 'europe-central2'; |
| 47 | + |
| 48 | + async function callConsistencyGroupDisksList() { |
| 49 | + const filter = `https://www.googleapis.com/compute/v1/projects/${projectId}/regions/${consistencyGroupLocation}/resourcePolicies/${consistencyGroupName}`; |
| 50 | + |
| 51 | + const [response] = await disksClient.list({ |
| 52 | + project: projectId, |
| 53 | + // If you use RegionDisksClient, pass region as an argument instead of zone. |
| 54 | + zone: disksLocation, |
| 55 | + }); |
| 56 | + |
| 57 | + // Filtering must be done manually for now, since list filtering inside disksClient.list is not supported yet. |
| 58 | + const filteredDisks = response.filter(disk => |
| 59 | + disk.resourcePolicies.includes(filter) |
| 60 | + ); |
| 61 | + |
| 62 | + console.log(JSON.stringify(filteredDisks)); |
| 63 | + } |
| 64 | + |
| 65 | + await callConsistencyGroupDisksList(); |
| 66 | + // [END compute_consistency_group_disks_list] |
| 67 | +} |
| 68 | + |
| 69 | +main(...process.argv.slice(2)).catch(err => { |
| 70 | + console.error(err); |
| 71 | + process.exitCode = 1; |
| 72 | +}); |
0 commit comments