diff --git a/compute/disks/consistencyGroups/consistencyGroupAddDisk.js b/compute/disks/consistencyGroups/consistencyGroupAddDisk.js new file mode 100644 index 00000000000..51b522bffdb --- /dev/null +++ b/compute/disks/consistencyGroups/consistencyGroupAddDisk.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_add_disk] + // Import the Compute library + 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 + 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 callAddDiskToConsistencyGroup() { + 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({ + 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, + // If you use RegionDisksClient, pass region as an argument instead of zone. + 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/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/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 new file mode 100644 index 00000000000..b5f8d94fde4 --- /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..7085eab7f7d --- /dev/null +++ b/compute/disks/consistencyGroups/deleteConsistencyGroup.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, region) { + // [START compute_consistency_group_delete] + + // 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 callDeleteConsistencyGroup() { + // Delete a resourcePolicyResource + 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 callDeleteConsistencyGroup(); + + // [END compute_consistency_group_delete] +} + +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..77f9972b0b7 --- /dev/null +++ b/compute/test/consistencyGroup.test.js @@ -0,0 +1,145 @@ +/* + * 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 {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 prefix = 'disk-name'; + const diskName = `${prefix}-${uuid.v4()}`; + const diskLocation = 'europe-central2-a'; + const region = 'europe-central2'; + let projectId; + + before(async () => { + 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( + `node ./disks/consistencyGroups/createConsistencyGroup.js ${consistencyGroupName} ${region}`, + { + cwd, + } + ); + + assert( + response.includes(`Consistency group: ${consistencyGroupName} created.`) + ); + }); + + it('should add disk to consistency group', () => { + const response = execSync( + `node ./disks/consistencyGroups/consistencyGroupAddDisk.js ${consistencyGroupName} ${region} ${diskName} ${diskLocation}`, + { + cwd, + } + ); + + assert( + response.includes( + `Disk: ${diskName} added to consistency group: ${consistencyGroupName}.` + ) + ); + }); + + 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}`, + { + 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.`) + ); + }); +});