diff --git a/compute/disks/createComputeHyperdiskPool.js b/compute/disks/createComputeHyperdiskPool.js new file mode 100644 index 00000000000..f648692cf89 --- /dev/null +++ b/compute/disks/createComputeHyperdiskPool.js @@ -0,0 +1,100 @@ +/* + * 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() { + // [START compute_hyperdisk_pool_create] + // Import the Compute library + const computeLib = require('@google-cloud/compute'); + const compute = computeLib.protos.google.cloud.compute.v1; + + // Instantiate a storagePoolClient + const storagePoolClient = new computeLib.StoragePoolsClient(); + // Instantiate a zoneOperationsClient + const zoneOperationsClient = new computeLib.ZoneOperationsClient(); + + /** + * TODO(developer): Update these variables before running the sample. + */ + // Project ID or project number of the Google Cloud project you want to use. + const projectId = await storagePoolClient.getProjectId(); + // Name of the zone in which you want to create the storagePool. + const zone = 'us-central1-a'; + // Name of the storagePool you want to create. + const storagePoolName = 'storage-pool-name'; + // The type of disk you want to create. This value uses the following format: + // "projects/{projectId}/zones/{zone}/storagePoolTypes/(hyperdisk-throughput|hyperdisk-balanced)" + const storagePoolType = `projects/${projectId}/zones/${zone}/storagePoolTypes/hyperdisk-balanced`; + // Optional: The capacity provisioning type of the storage pool. + // The allowed values are advanced and standard. If not specified, the value advanced is used. + const capacityProvisioningType = 'advanced'; + // The total capacity to provision for the new storage pool, specified in GiB by default. + const provisionedCapacity = 10240; + // The IOPS to provision for the storage pool. + // You can use this flag only with Hyperdisk Balanced Storage Pools. + const provisionedIops = 10000; + // The throughput in MBps to provision for the storage pool. + const provisionedThroughput = 1024; + + async function callCreateComputeHyperdiskPool() { + // Create a storagePool. + const storagePool = new compute.StoragePool({ + name: storagePoolName, + poolProvisionedCapacityGb: provisionedCapacity, + poolProvisionedIops: provisionedIops, + poolProvisionedThroughput: provisionedThroughput, + storagePoolType, + capacityProvisioningType, + zone, + }); + + const [response] = await storagePoolClient.insert({ + project: projectId, + storagePoolResource: storagePool, + zone, + }); + + 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(), + }); + } + + const createdStoragePool = ( + await storagePoolClient.get({ + project: projectId, + zone, + storagePool: storagePoolName, + }) + )[0]; + + console.log(JSON.stringify(createdStoragePool)); + } + + await callCreateComputeHyperdiskPool(); + // [END compute_hyperdisk_pool_create] +} + +main().catch(err => { + console.error(err); + process.exitCode = 1; +}); diff --git a/compute/test/createComputeHyperdiskPool.test.js b/compute/test/createComputeHyperdiskPool.test.js new file mode 100644 index 00000000000..824483f62fb --- /dev/null +++ b/compute/test/createComputeHyperdiskPool.test.js @@ -0,0 +1,62 @@ +/* + * 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 cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); +const {StoragePoolsClient} = require('@google-cloud/compute').v1; +const storagePoolsClient = new StoragePoolsClient(); + +async function deleteStoragePool(projectId, zone, storagePoolName) { + try { + await storagePoolsClient.delete({ + project: projectId, + storagePool: storagePoolName, + zone, + }); + } catch (err) { + console.error('Deleting storage pool failed: ', err); + } +} + +describe('Create compute hyperdisk pool', async () => { + const storagePoolName = 'storage-pool-name'; + const zone = 'us-central1-a'; + let projectId; + + before(async () => { + projectId = await storagePoolsClient.getProjectId(); + }); + + after(async () => { + await deleteStoragePool(projectId, zone, storagePoolName); + }); + + it('should create a new storage pool', () => { + const response = JSON.parse( + execSync('node ./disks/createComputeHyperdiskPool.js', { + cwd, + }) + ); + + assert.equal(response.name, storagePoolName); + }); +}); diff --git a/package.json b/package.json index cab4ab1f18b..017cfbbc3fe 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "@typescript-eslint/eslint-plugin": "^7.0.0", "@typescript-eslint/parser": "^7.0.0", "c8": "^10.0.0", + "chai": "^4.2.0", "eslint-config-prettier": "^9.0.0", "eslint-plugin-node": "^11.1.0", "eslint-plugin-prettier": "^5.0.0-alpha.1",