Skip to content

Commit

Permalink
feat: compute_hyperdisk_pool_create (GoogleCloudPlatform#3797)
Browse files Browse the repository at this point in the history
* feat: compute_hyperdisk_pool_create

* Changed debian-10 -> debian-11

---------

Co-authored-by: Tony Pujals <subfuzion@users.noreply.github.com>
  • Loading branch information
gryczj and subfuzion authored Aug 22, 2024
1 parent 1cf0c61 commit 4b58b83
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 0 deletions.
100 changes: 100 additions & 0 deletions compute/disks/createComputeHyperdiskPool.js
Original file line number Diff line number Diff line change
@@ -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 storage pool 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;
});
64 changes: 64 additions & 0 deletions compute/test/createComputeHyperdiskPool.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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 {StoragePoolsClient} = require('@google-cloud/compute').v1;

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
const cwd = path.join(__dirname, '..');
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);
throw new Error(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);
});
});

0 comments on commit 4b58b83

Please sign in to comment.