Skip to content

Commit 4e15dda

Browse files
authored
Merge branch 'main' into renovate/github-actions
2 parents bff42d4 + 4b58b83 commit 4e15dda

File tree

2 files changed

+164
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)