From 88e68135c65cdf4e60dd9a82f5eeec10dabf27e9 Mon Sep 17 00:00:00 2001 From: Joanna Grycz Date: Thu, 7 Nov 2024 13:41:59 +0100 Subject: [PATCH 1/8] feat: compute_consistency_group_create/delete --- .../createConsistencyGroup.js | 79 +++++++++++++++++++ .../deleteConsistencyGroup.js | 68 ++++++++++++++++ compute/test/consistencyGroup.test.js | 57 +++++++++++++ 3 files changed, 204 insertions(+) create mode 100644 compute/disks/consistencyGroups/createConsistencyGroup.js create mode 100644 compute/disks/consistencyGroups/deleteConsistencyGroup.js create mode 100644 compute/test/consistencyGroup.test.js diff --git a/compute/disks/consistencyGroups/createConsistencyGroup.js b/compute/disks/consistencyGroups/createConsistencyGroup.js new file mode 100644 index 00000000000..21c48fe65aa --- /dev/null +++ b/compute/disks/consistencyGroups/createConsistencyGroup.js @@ -0,0 +1,79 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +async function main(consistencyGroupName, region) { + // [START compute_consistency_group_create] + // Import the Compute library + const computeLib = require('@google-cloud/compute'); + const compute = computeLib.protos.google.cloud.compute.v1; + + // Instantiate a resourcePoliciesClient + const resourcePoliciesClient = new computeLib.ResourcePoliciesClient(); + // Instantiate a regionOperationsClient + const regionOperationsClient = new computeLib.RegionOperationsClient(); + + /** + * TODO(developer): Update/uncomment these variables before running the sample. + */ + // The project that contains the consistency group. + const projectId = await resourcePoliciesClient.getProjectId(); + + // The region for the consistency group. + // If you want to add primary disks to consistency group, use the same region as the primary disks. + // If you want to add secondary disks to the consistency group, use the same region as the secondary disks. + // region = 'europe-central2'; + + // The name for consistency group + // consistencyGroupName = 'consistency-group-name'; + + async function callCreateConsistencyGroup() { + // Create a resourcePolicyResource + const resourcePolicyResource = new compute.ResourcePolicy({ + diskConsistencyGroupPolicy: + new compute.ResourcePolicyDiskConsistencyGroupPolicy({}), + name: consistencyGroupName, + }); + + const [response] = await resourcePoliciesClient.insert({ + project: projectId, + region, + resourcePolicyResource, + }); + + let operation = response.latestResponse; + + // Wait for the create group operation to complete. + while (operation.status !== 'DONE') { + [operation] = await regionOperationsClient.wait({ + operation: operation.name, + project: projectId, + region, + }); + } + + console.log(`Consistency group: ${consistencyGroupName} created.`); + } + + await callCreateConsistencyGroup(); + // [END compute_consistency_group_create] +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err); + process.exitCode = 1; +}); diff --git a/compute/disks/consistencyGroups/deleteConsistencyGroup.js b/compute/disks/consistencyGroups/deleteConsistencyGroup.js new file mode 100644 index 00000000000..d2137d64ba3 --- /dev/null +++ b/compute/disks/consistencyGroups/deleteConsistencyGroup.js @@ -0,0 +1,68 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +async function main(consistencyGroupName, region) { + // [START compute_consistency_group_create] + // Import the Compute library + const computeLib = require('@google-cloud/compute'); + + // Instantiate a resourcePoliciesClient + const resourcePoliciesClient = new computeLib.ResourcePoliciesClient(); + // Instantiate a regionOperationsClient + const regionOperationsClient = new computeLib.RegionOperationsClient(); + /** + * TODO(developer): Update/uncomment these variables before running the sample. + */ + // The project that contains the consistency group. + const projectId = await resourcePoliciesClient.getProjectId(); + + // The region of the consistency group + // region = 'europe-central2'; + + // The name of the consistency group + // consistencyGroupName = 'consistency-group-name'; + + async function callCreateConsistencyGroup() { + const [response] = await resourcePoliciesClient.delete({ + project: projectId, + region, + resourcePolicy: consistencyGroupName, + }); + + let operation = response.latestResponse; + + // Wait for the delete group operation to complete. + while (operation.status !== 'DONE') { + [operation] = await regionOperationsClient.wait({ + operation: operation.name, + project: projectId, + region, + }); + } + + console.log(`Consistency group: ${consistencyGroupName} deleted.`); + } + + await callCreateConsistencyGroup(); + // [END compute_consistency_group_create] +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err); + process.exitCode = 1; +}); diff --git a/compute/test/consistencyGroup.test.js b/compute/test/consistencyGroup.test.js new file mode 100644 index 00000000000..457129d412d --- /dev/null +++ b/compute/test/consistencyGroup.test.js @@ -0,0 +1,57 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const path = require('path'); +const assert = require('node:assert/strict'); +const {describe, it} = require('mocha'); +const uuid = require('uuid'); +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +describe('Consistency group', async () => { + const consistencyGroupName = `consistency-group-name-${uuid.v4()}`; + const region = 'europe-central2'; + + it('should create a new consistency group', () => { + const response = execSync( + `node ./disks/consistencyGroups/createConsistencyGroup.js ${consistencyGroupName} ${region}`, + { + cwd, + } + ); + + assert( + response.includes(`Consistency group: ${consistencyGroupName} created.`) + ); + }); + + it('should delete consistency group', () => { + const response = execSync( + `node ./disks/consistencyGroups/deleteConsistencyGroup.js ${consistencyGroupName} ${region}`, + { + cwd, + } + ); + + assert( + response.includes(`Consistency group: ${consistencyGroupName} deleted.`) + ); + }); +}); From 9f2c418f64efb332186f981dbe4c953c51af864d Mon Sep 17 00:00:00 2001 From: Joanna Grycz Date: Tue, 12 Nov 2024 09:38:00 +0100 Subject: [PATCH 2/8] feat: compute_consistency_group_add_disk --- .../consistencyGroupAddDisk.js | 90 +++++++++++++++++++ .../createConsistencyGroup.js | 5 +- .../deleteConsistencyGroup.js | 8 +- compute/test/consistencyGroup.test.js | 70 +++++++++++++-- 4 files changed, 163 insertions(+), 10 deletions(-) create mode 100644 compute/disks/consistencyGroups/consistencyGroupAddDisk.js diff --git a/compute/disks/consistencyGroups/consistencyGroupAddDisk.js b/compute/disks/consistencyGroups/consistencyGroupAddDisk.js new file mode 100644 index 00000000000..1ba9ce4ca13 --- /dev/null +++ b/compute/disks/consistencyGroups/consistencyGroupAddDisk.js @@ -0,0 +1,90 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +async function main( + consistencyGroupName, + consistencyGroupLocation, + diskName, + diskLocation +) { + // [START compute_consistency_group_add_disk] + // Import the Compute library + const computeLib = require('@google-cloud/compute'); + const compute = computeLib.protos.google.cloud.compute.v1; + + // Instantiate a disksClient + const disksClient = new computeLib.DisksClient(); + // Instantiate a zone + const zoneOperationsClient = new computeLib.ZoneOperationsClient(); + + /** + * TODO(developer): Update/uncomment these variables before running the sample. + */ + // The project that contains the disk. + // const projectId = await disksClient.getProjectId(); + const projectId = 'jgrycz-softserve-project'; + + // The name of the disk. + // diskName = 'disk-name'; + + // The zone of the disk. + // diskLocation = 'europe-central2-a'; + + // The name of the consistency group. + // consistencyGroupName = 'consistency-group-name'; + + // The region of the consistency group. + // consistencyGroupLocation = 'europe-central2'; + + async function callAddDiskToConsistencyGroup() { + const [response] = await disksClient.addResourcePolicies({ + disk: diskName, + project: projectId, + zone: diskLocation, + disksAddResourcePoliciesRequestResource: + new compute.DisksAddResourcePoliciesRequest({ + resourcePolicies: [ + `https://www.googleapis.com/compute/v1/projects/${projectId}/regions/${consistencyGroupLocation}/resourcePolicies/${consistencyGroupName}`, + ], + }), + }); + + let operation = response.latestResponse; + + // Wait for the add disk operation to complete. + while (operation.status !== 'DONE') { + [operation] = await zoneOperationsClient.wait({ + operation: operation.name, + project: projectId, + zone: operation.zone.split('/').pop(), + }); + } + + console.log( + `Disk: ${diskName} added to consistency group: ${consistencyGroupName}.` + ); + } + + await callAddDiskToConsistencyGroup(); + // [END compute_consistency_group_add_disk] +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err); + process.exitCode = 1; +}); diff --git a/compute/disks/consistencyGroups/createConsistencyGroup.js b/compute/disks/consistencyGroups/createConsistencyGroup.js index 21c48fe65aa..67fc602eb8a 100644 --- a/compute/disks/consistencyGroups/createConsistencyGroup.js +++ b/compute/disks/consistencyGroups/createConsistencyGroup.js @@ -26,19 +26,20 @@ async function main(consistencyGroupName, region) { const resourcePoliciesClient = new computeLib.ResourcePoliciesClient(); // Instantiate a regionOperationsClient const regionOperationsClient = new computeLib.RegionOperationsClient(); + const projectId = 'jgrycz-softserve-project'; /** * TODO(developer): Update/uncomment these variables before running the sample. */ // The project that contains the consistency group. - const projectId = await resourcePoliciesClient.getProjectId(); + // const projectId = await resourcePoliciesClient.getProjectId(); // The region for the consistency group. // If you want to add primary disks to consistency group, use the same region as the primary disks. // If you want to add secondary disks to the consistency group, use the same region as the secondary disks. // region = 'europe-central2'; - // The name for consistency group + // The name for consistency group. // consistencyGroupName = 'consistency-group-name'; async function callCreateConsistencyGroup() { diff --git a/compute/disks/consistencyGroups/deleteConsistencyGroup.js b/compute/disks/consistencyGroups/deleteConsistencyGroup.js index d2137d64ba3..503f3dc053c 100644 --- a/compute/disks/consistencyGroups/deleteConsistencyGroup.js +++ b/compute/disks/consistencyGroups/deleteConsistencyGroup.js @@ -25,16 +25,18 @@ async function main(consistencyGroupName, region) { const resourcePoliciesClient = new computeLib.ResourcePoliciesClient(); // Instantiate a regionOperationsClient const regionOperationsClient = new computeLib.RegionOperationsClient(); + /** * TODO(developer): Update/uncomment these variables before running the sample. */ // The project that contains the consistency group. - const projectId = await resourcePoliciesClient.getProjectId(); + // const projectId = await resourcePoliciesClient.getProjectId(); + const projectId = 'jgrycz-softserve-project'; - // The region of the consistency group + // The region of the consistency group. // region = 'europe-central2'; - // The name of the consistency group + // The name of the consistency group. // consistencyGroupName = 'consistency-group-name'; async function callCreateConsistencyGroup() { diff --git a/compute/test/consistencyGroup.test.js b/compute/test/consistencyGroup.test.js index 457129d412d..006085fc465 100644 --- a/compute/test/consistencyGroup.test.js +++ b/compute/test/consistencyGroup.test.js @@ -18,16 +18,61 @@ const path = require('path'); const assert = require('node:assert/strict'); -const {describe, it} = require('mocha'); -const uuid = require('uuid'); +const {before, after, describe, it} = require('mocha'); const cp = require('child_process'); +const uuid = require('uuid'); +const computeLib = require('@google-cloud/compute'); +const {getStaleDisks, deleteDisk} = require('./util'); const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); const cwd = path.join(__dirname, '..'); +const disksClient = new computeLib.DisksClient(); +const zoneOperationsClient = new computeLib.ZoneOperationsClient(); + +async function createDisk(diskName, zone, projectId) { + const [response] = await disksClient.insert({ + project: projectId, + zone, + diskResource: { + sizeGb: 10, + name: diskName, + zone, + type: `zones/${zone}/diskTypes/pd-balanced`, + }, + }); + + let operation = response.latestResponse; + + // Wait for the create disk operation to complete. + while (operation.status !== 'DONE') { + [operation] = await zoneOperationsClient.wait({ + operation: operation.name, + project: projectId, + zone: operation.zone.split('/').pop(), + }); + } + + console.log(`Disk: ${diskName} created.`); +} describe('Consistency group', async () => { const consistencyGroupName = `consistency-group-name-${uuid.v4()}`; + const diskName = `disk-name-${uuid.v4()}`; + const diskLocation = 'europe-central2-a'; const region = 'europe-central2'; + let projectId; + + before(async () => { + projectId = 'jgrycz-softserve-project'; + // projectId = await disksClient.getProjectId(); + await createDisk(diskName, diskLocation, projectId); + }); + + after(async () => { + // Cleanup resources + // const disks = await getStaleDisks(prefix); + // await Promise.all(disks.map(disk => deleteDisk(disk.zone, disk.diskName))); + }); it('should create a new consistency group', () => { const response = execSync( @@ -42,16 +87,31 @@ describe('Consistency group', async () => { ); }); - it('should delete consistency group', () => { + it('should add disk to consistency group', () => { const response = execSync( - `node ./disks/consistencyGroups/deleteConsistencyGroup.js ${consistencyGroupName} ${region}`, + `node ./disks/consistencyGroups/consistencyGroupAddDisk.js ${consistencyGroupName} ${region} ${diskName} ${diskLocation}`, { cwd, } ); assert( - response.includes(`Consistency group: ${consistencyGroupName} deleted.`) + response.includes( + `Disk: ${diskName} added to consistency group: ${consistencyGroupName}.` + ) ); }); + + // it('should delete consistency group', () => { + // const response = execSync( + // `node ./disks/consistencyGroups/deleteConsistencyGroup.js ${consistencyGroupName} ${region}`, + // { + // cwd, + // } + // ); + + // assert( + // response.includes(`Consistency group: ${consistencyGroupName} deleted.`) + // ); + // }); }); From 3111b305c7c29bc09b48cc12da96c33bd864398d Mon Sep 17 00:00:00 2001 From: Joanna Grycz Date: Wed, 13 Nov 2024 16:05:21 +0100 Subject: [PATCH 3/8] feat: compute_consistency_group_remove_disk --- .../consistencyGroupAddDisk.js | 10 +- .../consistencyGroupRemoveDisk.js | 94 +++++++++++++++++++ .../createConsistencyGroup.js | 3 +- .../deleteConsistencyGroup.js | 3 +- compute/test/consistencyGroup.test.js | 49 ++++++---- 5 files changed, 135 insertions(+), 24 deletions(-) create mode 100644 compute/disks/consistencyGroups/consistencyGroupRemoveDisk.js diff --git a/compute/disks/consistencyGroups/consistencyGroupAddDisk.js b/compute/disks/consistencyGroups/consistencyGroupAddDisk.js index 1ba9ce4ca13..51b522bffdb 100644 --- a/compute/disks/consistencyGroups/consistencyGroupAddDisk.js +++ b/compute/disks/consistencyGroups/consistencyGroupAddDisk.js @@ -27,6 +27,8 @@ async function main( const computeLib = require('@google-cloud/compute'); const compute = computeLib.protos.google.cloud.compute.v1; + // If you want to add regional disk, + // you should use: RegionDisksClient and RegionOperationsClient. // Instantiate a disksClient const disksClient = new computeLib.DisksClient(); // Instantiate a zone @@ -36,13 +38,13 @@ async function main( * TODO(developer): Update/uncomment these variables before running the sample. */ // The project that contains the disk. - // const projectId = await disksClient.getProjectId(); - const projectId = 'jgrycz-softserve-project'; + const projectId = await disksClient.getProjectId(); // The name of the disk. // diskName = 'disk-name'; - // The zone of the disk. + // If you use RegionDisksClient- define region, if DisksClient- define zone. + // The zone or region of the disk. // diskLocation = 'europe-central2-a'; // The name of the consistency group. @@ -55,6 +57,7 @@ async function main( const [response] = await disksClient.addResourcePolicies({ disk: diskName, project: projectId, + // If you use RegionDisksClient, pass region as an argument instead of zone. zone: diskLocation, disksAddResourcePoliciesRequestResource: new compute.DisksAddResourcePoliciesRequest({ @@ -71,6 +74,7 @@ async function main( [operation] = await zoneOperationsClient.wait({ operation: operation.name, project: projectId, + // If you use RegionDisksClient, pass region as an argument instead of zone. zone: operation.zone.split('/').pop(), }); } diff --git a/compute/disks/consistencyGroups/consistencyGroupRemoveDisk.js b/compute/disks/consistencyGroups/consistencyGroupRemoveDisk.js new file mode 100644 index 00000000000..6ae4846587d --- /dev/null +++ b/compute/disks/consistencyGroups/consistencyGroupRemoveDisk.js @@ -0,0 +1,94 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +async function main( + consistencyGroupName, + consistencyGroupLocation, + diskName, + diskLocation +) { + // [START compute_consistency_group_remove_disk] + // Import the Compute library + const computeLib = require('@google-cloud/compute'); + const compute = computeLib.protos.google.cloud.compute.v1; + + // If you want to remove regional disk, + // you should use: RegionDisksClient and RegionOperationsClient. + // Instantiate a disksClient + const disksClient = new computeLib.DisksClient(); + // Instantiate a zone + const zoneOperationsClient = new computeLib.ZoneOperationsClient(); + + /** + * TODO(developer): Update/uncomment these variables before running the sample. + */ + // The project that contains the disk. + const projectId = await disksClient.getProjectId(); + + // The name of the disk. + // diskName = 'disk-name'; + + // If you use RegionDisksClient- define region, if DisksClient- define zone. + // The zone or region of the disk. + // diskLocation = 'europe-central2-a'; + + // The name of the consistency group. + // consistencyGroupName = 'consistency-group-name'; + + // The region of the consistency group. + // consistencyGroupLocation = 'europe-central2'; + + async function callDeleteDiskFromConsistencyGroup() { + const [response] = await disksClient.removeResourcePolicies({ + disk: diskName, + project: projectId, + // If you use RegionDisksClient, pass region as an argument instead of zone. + zone: diskLocation, + disksRemoveResourcePoliciesRequestResource: + new compute.DisksRemoveResourcePoliciesRequest({ + resourcePolicies: [ + `https://www.googleapis.com/compute/v1/projects/${projectId}/regions/${consistencyGroupLocation}/resourcePolicies/${consistencyGroupName}`, + ], + }), + }); + + let operation = response.latestResponse; + + // Wait for the delete disk operation to complete. + while (operation.status !== 'DONE') { + [operation] = await zoneOperationsClient.wait({ + operation: operation.name, + project: projectId, + // If you use RegionDisksClient, pass region as an argument instead of zone. + zone: operation.zone.split('/').pop(), + }); + } + + console.log( + `Disk: ${diskName} deleted from consistency group: ${consistencyGroupName}.` + ); + } + + await callDeleteDiskFromConsistencyGroup(); + // [END compute_consistency_group_remove_disk] +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err); + process.exitCode = 1; +}); diff --git a/compute/disks/consistencyGroups/createConsistencyGroup.js b/compute/disks/consistencyGroups/createConsistencyGroup.js index 67fc602eb8a..b5f8d94fde4 100644 --- a/compute/disks/consistencyGroups/createConsistencyGroup.js +++ b/compute/disks/consistencyGroups/createConsistencyGroup.js @@ -26,13 +26,12 @@ async function main(consistencyGroupName, region) { const resourcePoliciesClient = new computeLib.ResourcePoliciesClient(); // Instantiate a regionOperationsClient const regionOperationsClient = new computeLib.RegionOperationsClient(); - const projectId = 'jgrycz-softserve-project'; /** * TODO(developer): Update/uncomment these variables before running the sample. */ // The project that contains the consistency group. - // const projectId = await resourcePoliciesClient.getProjectId(); + const projectId = await resourcePoliciesClient.getProjectId(); // The region for the consistency group. // If you want to add primary disks to consistency group, use the same region as the primary disks. diff --git a/compute/disks/consistencyGroups/deleteConsistencyGroup.js b/compute/disks/consistencyGroups/deleteConsistencyGroup.js index 503f3dc053c..743e3d2b0d6 100644 --- a/compute/disks/consistencyGroups/deleteConsistencyGroup.js +++ b/compute/disks/consistencyGroups/deleteConsistencyGroup.js @@ -30,8 +30,7 @@ async function main(consistencyGroupName, region) { * TODO(developer): Update/uncomment these variables before running the sample. */ // The project that contains the consistency group. - // const projectId = await resourcePoliciesClient.getProjectId(); - const projectId = 'jgrycz-softserve-project'; + const projectId = await resourcePoliciesClient.getProjectId(); // The region of the consistency group. // region = 'europe-central2'; diff --git a/compute/test/consistencyGroup.test.js b/compute/test/consistencyGroup.test.js index 006085fc465..44b7dfe44ae 100644 --- a/compute/test/consistencyGroup.test.js +++ b/compute/test/consistencyGroup.test.js @@ -57,21 +57,21 @@ async function createDisk(diskName, zone, projectId) { } describe('Consistency group', async () => { const consistencyGroupName = `consistency-group-name-${uuid.v4()}`; - const diskName = `disk-name-${uuid.v4()}`; + const prefix = 'disk-name'; + const diskName = `${prefix}-${uuid.v4()}`; const diskLocation = 'europe-central2-a'; const region = 'europe-central2'; let projectId; before(async () => { - projectId = 'jgrycz-softserve-project'; - // projectId = await disksClient.getProjectId(); + projectId = await disksClient.getProjectId(); await createDisk(diskName, diskLocation, projectId); }); after(async () => { // Cleanup resources - // const disks = await getStaleDisks(prefix); - // await Promise.all(disks.map(disk => deleteDisk(disk.zone, disk.diskName))); + const disks = await getStaleDisks(prefix); + await Promise.all(disks.map(disk => deleteDisk(disk.zone, disk.diskName))); }); it('should create a new consistency group', () => { @@ -102,16 +102,31 @@ describe('Consistency group', async () => { ); }); - // it('should delete consistency group', () => { - // const response = execSync( - // `node ./disks/consistencyGroups/deleteConsistencyGroup.js ${consistencyGroupName} ${region}`, - // { - // cwd, - // } - // ); - - // assert( - // response.includes(`Consistency group: ${consistencyGroupName} deleted.`) - // ); - // }); + it('should delete disk from consistency group', () => { + const response = execSync( + `node ./disks/consistencyGroups/consistencyGroupRemoveDisk.js ${consistencyGroupName} ${region} ${diskName} ${diskLocation}`, + { + cwd, + } + ); + + assert( + response.includes( + `Disk: ${diskName} deleted from consistency group: ${consistencyGroupName}.` + ) + ); + }); + + it('should delete consistency group', () => { + const response = execSync( + `node ./disks/consistencyGroups/deleteConsistencyGroup.js ${consistencyGroupName} ${region}`, + { + cwd, + } + ); + + assert( + response.includes(`Consistency group: ${consistencyGroupName} deleted.`) + ); + }); }); From d38127e4a3c15faa065297ae8bb5528e93c8ca13 Mon Sep 17 00:00:00 2001 From: Joanna Grycz Date: Fri, 15 Nov 2024 11:32:04 +0100 Subject: [PATCH 4/8] feat: compute_consistency_group_disks_list --- .../consistencyGroupDisksList.js | 72 +++++++++++++++++++ compute/test/consistencyGroup.test.js | 13 ++++ 2 files changed, 85 insertions(+) create mode 100644 compute/disks/consistencyGroups/consistencyGroupDisksList.js diff --git a/compute/disks/consistencyGroups/consistencyGroupDisksList.js b/compute/disks/consistencyGroups/consistencyGroupDisksList.js new file mode 100644 index 00000000000..38f68816966 --- /dev/null +++ b/compute/disks/consistencyGroups/consistencyGroupDisksList.js @@ -0,0 +1,72 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +async function main( + consistencyGroupName, + consistencyGroupLocation, + disksLocation +) { + // [START compute_consistency_group_disks_list] + // Import the Compute library + const computeLib = require('@google-cloud/compute'); + + // If you want to get regional disks, you should use: RegionDisksClient. + // Instantiate a disksClient + const disksClient = new computeLib.DisksClient(); + + /** + * TODO(developer): Update/uncomment these variables before running the sample. + */ + // The project that contains the disks. + const projectId = await disksClient.getProjectId(); + + // If you use RegionDisksClient- define region, if DisksClient- define zone. + // The zone or region of the disks. + // disksLocation = 'europe-central2-a'; + + // The name of the consistency group. + // consistencyGroupName = 'consistency-group-name'; + + // The region of the consistency group. + // consistencyGroupLocation = 'europe-central2'; + + async function callConsistencyGroupDisksList() { + const filter = `https://www.googleapis.com/compute/v1/projects/${projectId}/regions/${consistencyGroupLocation}/resourcePolicies/${consistencyGroupName}`; + + const [response] = await disksClient.list({ + project: projectId, + // If you use RegionDisksClient, pass region as an argument instead of zone. + zone: disksLocation, + }); + + // Filtering must be done manually for now, since list filtering inside disksClient.list is not supported yet. + const filteredDisks = response.filter(disk => + disk.resourcePolicies.includes(filter) + ); + + console.log(JSON.stringify(filteredDisks)); + } + + await callConsistencyGroupDisksList(); + // [END compute_consistency_group_disks_list] +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err); + process.exitCode = 1; +}); diff --git a/compute/test/consistencyGroup.test.js b/compute/test/consistencyGroup.test.js index 44b7dfe44ae..77f9972b0b7 100644 --- a/compute/test/consistencyGroup.test.js +++ b/compute/test/consistencyGroup.test.js @@ -102,6 +102,19 @@ describe('Consistency group', async () => { ); }); + it('should return disks from consistency group', () => { + const response = JSON.parse( + execSync( + `node ./disks/consistencyGroups/consistencyGroupDisksList.js ${consistencyGroupName} ${region} ${diskLocation}`, + { + cwd, + } + ) + ); + + assert(Array.isArray(response)); + }); + it('should delete disk from consistency group', () => { const response = execSync( `node ./disks/consistencyGroups/consistencyGroupRemoveDisk.js ${consistencyGroupName} ${region} ${diskName} ${diskLocation}`, From f7bf0671ec6ed21f57042733d66a07184db9262d Mon Sep 17 00:00:00 2001 From: Jennifer Davis Date: Fri, 15 Nov 2024 16:07:28 -0800 Subject: [PATCH 5/8] fix(compute): Update compute/disks/consistencyGroups/deleteConsistencyGroup.js Co-authored-by: code-review-assist-experimental[bot] <172519755+code-review-assist-experimental[bot]@users.noreply.github.com> --- compute/disks/consistencyGroups/deleteConsistencyGroup.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compute/disks/consistencyGroups/deleteConsistencyGroup.js b/compute/disks/consistencyGroups/deleteConsistencyGroup.js index 743e3d2b0d6..c87de291245 100644 --- a/compute/disks/consistencyGroups/deleteConsistencyGroup.js +++ b/compute/disks/consistencyGroups/deleteConsistencyGroup.js @@ -17,7 +17,7 @@ 'use strict'; async function main(consistencyGroupName, region) { - // [START compute_consistency_group_create] +// [START compute_consistency_group_delete] // Import the Compute library const computeLib = require('@google-cloud/compute'); From cbda7e4c84805bb3037bff5a042c425f0cea3b33 Mon Sep 17 00:00:00 2001 From: Jennifer Davis Date: Fri, 15 Nov 2024 16:07:51 -0800 Subject: [PATCH 6/8] fix(compute) : Update compute/disks/consistencyGroups/deleteConsistencyGroup.js Co-authored-by: code-review-assist-experimental[bot] <172519755+code-review-assist-experimental[bot]@users.noreply.github.com> --- compute/disks/consistencyGroups/deleteConsistencyGroup.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compute/disks/consistencyGroups/deleteConsistencyGroup.js b/compute/disks/consistencyGroups/deleteConsistencyGroup.js index c87de291245..7af7cb66c31 100644 --- a/compute/disks/consistencyGroups/deleteConsistencyGroup.js +++ b/compute/disks/consistencyGroups/deleteConsistencyGroup.js @@ -38,7 +38,7 @@ async function main(consistencyGroupName, region) { // The name of the consistency group. // consistencyGroupName = 'consistency-group-name'; - async function callCreateConsistencyGroup() { +async function callDeleteConsistencyGroup() { const [response] = await resourcePoliciesClient.delete({ project: projectId, region, From 9e87ec7d992b70a462da8dc5e970fadf3d24f529 Mon Sep 17 00:00:00 2001 From: Jennifer Davis Date: Fri, 15 Nov 2024 16:08:05 -0800 Subject: [PATCH 7/8] fix(compute): Update compute/disks/consistencyGroups/deleteConsistencyGroup.js Co-authored-by: code-review-assist-experimental[bot] <172519755+code-review-assist-experimental[bot]@users.noreply.github.com> --- compute/disks/consistencyGroups/deleteConsistencyGroup.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compute/disks/consistencyGroups/deleteConsistencyGroup.js b/compute/disks/consistencyGroups/deleteConsistencyGroup.js index 7af7cb66c31..17400602bc6 100644 --- a/compute/disks/consistencyGroups/deleteConsistencyGroup.js +++ b/compute/disks/consistencyGroups/deleteConsistencyGroup.js @@ -60,7 +60,7 @@ async function callDeleteConsistencyGroup() { } await callCreateConsistencyGroup(); - // [END compute_consistency_group_create] +// [END compute_consistency_group_delete] } main(...process.argv.slice(2)).catch(err => { From 04a1f0df4a1fd53a1b1a1acc829029d37cb99a92 Mon Sep 17 00:00:00 2001 From: Jennifer Davis Date: Fri, 15 Nov 2024 16:08:18 -0800 Subject: [PATCH 8/8] fix(compute): Update compute/disks/consistencyGroups/deleteConsistencyGroup.js Co-authored-by: code-review-assist-experimental[bot] <172519755+code-review-assist-experimental[bot]@users.noreply.github.com> --- .../disks/consistencyGroups/deleteConsistencyGroup.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/compute/disks/consistencyGroups/deleteConsistencyGroup.js b/compute/disks/consistencyGroups/deleteConsistencyGroup.js index 17400602bc6..7085eab7f7d 100644 --- a/compute/disks/consistencyGroups/deleteConsistencyGroup.js +++ b/compute/disks/consistencyGroups/deleteConsistencyGroup.js @@ -17,7 +17,8 @@ 'use strict'; async function main(consistencyGroupName, region) { -// [START compute_consistency_group_delete] + // [START compute_consistency_group_delete] + // Import the Compute library const computeLib = require('@google-cloud/compute'); @@ -38,7 +39,8 @@ async function main(consistencyGroupName, region) { // The name of the consistency group. // consistencyGroupName = 'consistency-group-name'; -async function callDeleteConsistencyGroup() { + async function callDeleteConsistencyGroup() { + // Delete a resourcePolicyResource const [response] = await resourcePoliciesClient.delete({ project: projectId, region, @@ -59,8 +61,9 @@ async function callDeleteConsistencyGroup() { console.log(`Consistency group: ${consistencyGroupName} deleted.`); } - await callCreateConsistencyGroup(); -// [END compute_consistency_group_delete] + await callDeleteConsistencyGroup(); + + // [END compute_consistency_group_delete] } main(...process.argv.slice(2)).catch(err => {