Skip to content

Commit f994413

Browse files
author
Joanna Grycz
committed
feat: compute_hyperdisk_pool_create
1 parent 6fe7417 commit f994413

File tree

3 files changed

+163
-1
lines changed

3 files changed

+163
-1
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
// [START compute_hyperdisk_pool_create]
21+
// Import the Compute library
22+
const computeLib = require('@google-cloud/compute');
23+
const compute = computeLib.protos.google.cloud.compute.v1;
24+
25+
// Instantiate a storagePoolClient
26+
const storagePoolClient = new computeLib.StoragePoolsClient();
27+
// Instantiate a zoneOperationsClient
28+
const zoneOperationsClient = new computeLib.ZoneOperationsClient();
29+
30+
/**
31+
* TODO(developer): Update these variables before running the sample.
32+
*/
33+
// Project ID or project number of the Google Cloud project you want to use.
34+
const projectId = await storagePoolClient.getProjectId();
35+
// Name of the zone in which you want to create the storagePool.
36+
const zone = 'us-central1-a';
37+
// Name of the storagePool you want to create.
38+
const storagePoolName = 'storage-pool-name';
39+
// The type of disk you want to create. This value uses the following format:
40+
// "projects/{projectId}/zones/{zone}/storagePoolTypes/(hyperdisk-throughput|hyperdisk-balanced)"
41+
const storagePoolType = `projects/${projectId}/zones/${zone}/storagePoolTypes/hyperdisk-balanced`;
42+
// Optional: The capacity provisioning type of the storage pool.
43+
// The allowed values are advanced and standard. If not specified, the value advanced is used.
44+
const capacityProvisioningType = 'advanced';
45+
// The total capacity to provision for the new storage pool, specified in GiB by default.
46+
const provisionedCapacity = 10240;
47+
// The IOPS to provision for the storage pool.
48+
// You can use this flag only with Hyperdisk Balanced Storage Pools.
49+
const provisionedIops = 10000;
50+
// The throughput in MBps to provision for the storage pool.
51+
const provisionedThroughput = 1024;
52+
53+
async function callCreateComputeHyperdiskPool() {
54+
// Create a storagePool.
55+
const storagePool = new compute.StoragePool({
56+
name: storagePoolName,
57+
poolProvisionedCapacityGb: provisionedCapacity,
58+
poolProvisionedIops: provisionedIops,
59+
poolProvisionedThroughput: provisionedThroughput,
60+
storagePoolType,
61+
capacityProvisioningType,
62+
zone,
63+
});
64+
65+
const [response] = await storagePoolClient.insert({
66+
project: projectId,
67+
storagePoolResource: storagePool,
68+
zone,
69+
});
70+
71+
let operation = response.latestResponse;
72+
73+
// Wait for the create disk operation to complete.
74+
while (operation.status !== 'DONE') {
75+
[operation] = await zoneOperationsClient.wait({
76+
operation: operation.name,
77+
project: projectId,
78+
zone: operation.zone.split('/').pop(),
79+
});
80+
}
81+
82+
const createdStoragePool = (
83+
await storagePoolClient.get({
84+
project: projectId,
85+
zone,
86+
storagePool: storagePoolName,
87+
})
88+
)[0];
89+
90+
console.log(JSON.stringify(createdStoragePool));
91+
}
92+
93+
await callCreateComputeHyperdiskPool();
94+
// [END compute_hyperdisk_pool_create]
95+
}
96+
97+
main().catch(err => {
98+
console.error(err);
99+
process.exitCode = 1;
100+
});

compute/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"devDependencies": {
2323
"@google-cloud/storage": "^7.0.0",
2424
"c8": "^10.0.0",
25-
"chai": "^5.0.0",
25+
"chai": "^4.2.0",
2626
"mocha": "^10.0.0",
2727
"proxyquire": "^2.0.1",
2828
"uuid": "^10.0.0"
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
const path = require('path');
20+
const assert = require('node:assert/strict');
21+
const {describe, it} = require('mocha');
22+
const cp = require('child_process');
23+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
24+
const cwd = path.join(__dirname, '..');
25+
const {StoragePoolsClient} = require('@google-cloud/compute').v1;
26+
const storagePoolsClient = new StoragePoolsClient();
27+
28+
async function deleteStoragePool(projectId, zone, storagePoolName) {
29+
try {
30+
await storagePoolsClient.delete({
31+
project: projectId,
32+
storagePool: storagePoolName,
33+
zone,
34+
});
35+
} catch (err) {
36+
console.error('Deleting storage pool failed: ', err);
37+
}
38+
}
39+
40+
describe('Create compute hyperdisk pool', async () => {
41+
const storagePoolName = 'storage-pool-name';
42+
const zone = 'us-central1-a';
43+
let projectId;
44+
45+
before(async () => {
46+
projectId = await storagePoolsClient.getProjectId();
47+
});
48+
49+
after(async () => {
50+
await deleteStoragePool(projectId, zone, storagePoolName);
51+
});
52+
53+
it('should create a new storage pool', () => {
54+
const response = JSON.parse(
55+
execSync('node ./disks/createComputeHyperdiskPool.js', {
56+
cwd,
57+
})
58+
);
59+
60+
assert.equal(response.name, storagePoolName);
61+
});
62+
});

0 commit comments

Comments
 (0)