Skip to content

Commit

Permalink
code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
gryczj committed Aug 22, 2024
1 parent 5e508c1 commit 5866c23
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 17 deletions.
2 changes: 1 addition & 1 deletion compute/disks/createComputeHyperdiskFromPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async function main() {
// The name of the new disk
const diskName = 'disk-name';
// The name of the storage pool
const storagePoolName = 'storage-pool-hyperdisk';
const storagePoolName = 'storage-pool-name';
// Link to the storagePool you want to use. Use format:
// https://www.googleapis.com/compute/v1/projects/{projectId}/zones/{zone}/storagePools/{storagePoolName}
const storagePool = `https://www.googleapis.com/compute/v1/projects/${projectId}/zones/${zone}/storagePools/${storagePoolName}`;
Expand Down
37 changes: 25 additions & 12 deletions compute/test/createComputeHyperdiskFromPool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ const path = require('path');
const {assert} = require('chai');
const {describe, it} = require('mocha');
const cp = require('child_process');
const {StoragePoolsClient} = require('@google-cloud/compute');
const {
StoragePoolsClient,
ZoneOperationsClient,
} = require('@google-cloud/compute');
const {DisksClient} = require('@google-cloud/compute').v1;
const {createStoragePool, deleteStoragePool, deleteDisk} = require('./util');

Check failure on line 28 in compute/test/createComputeHyperdiskFromPool.test.js

View workflow job for this annotation

GitHub Actions / lint

'createStoragePool' is assigned a value but never used

Check failure on line 28 in compute/test/createComputeHyperdiskFromPool.test.js

View workflow job for this annotation

GitHub Actions / lint

'deleteStoragePool' is assigned a value but never used

Expand All @@ -30,29 +33,39 @@ const cwd = path.join(__dirname, '..');
describe('Create compute hyperdisk from pool', async () => {
const diskName = 'disk-name';
const zone = 'europe-central2-b';
const storagePoolName = 'storage-pool-hyperdisk';
const storagePoolName = 'storage-pool-name';
const disksClient = new DisksClient();
const storagePoolsClient = new StoragePoolsClient();

Check failure on line 38 in compute/test/createComputeHyperdiskFromPool.test.js

View workflow job for this annotation

GitHub Actions / lint

'storagePoolsClient' is assigned a value but never used
const zoneOperationsClient = new ZoneOperationsClient();

let projectId;

before(async () => {
projectId = await disksClient.getProjectId();
await createStoragePool(
storagePoolsClient,
projectId,
zone,
storagePoolName
);
// await createStoragePool(
// storagePoolsClient,
// zoneOperationsClient,
// projectId,
// zone,
// storagePoolName
// );
});

after(async () => {
await deleteDisk(disksClient, projectId, zone, diskName);
await deleteStoragePool(
storagePoolsClient,
await deleteDisk(
disksClient,
zoneOperationsClient,
projectId,
zone,
storagePoolName
diskName
);
// await deleteStoragePool(
// storagePoolsClient,
// zoneOperationsClient,
// projectId,
// zone,
// storagePoolName
// );
});

it('should create a new hyperdisk from pool', () => {
Expand Down
46 changes: 42 additions & 4 deletions compute/test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,29 @@ async function deleteInstance(zone, instanceName) {
}
}

async function deleteDisk(disksClient, projectId, zone, diskName) {
async function deleteDisk(
disksClient,
zoneOperationsClient,
projectId,
zone,
diskName
) {
try {
await disksClient.delete({
const [response] = await disksClient.delete({
project: projectId,
disk: diskName,
zone,
});
let operation = response.latestResponse;

// Wait for the delete operation to complete.
while (operation.status !== 'DONE') {
[operation] = await zoneOperationsClient.wait({
operation: operation.name,
project: projectId,
zone: operation.zone.split('/').pop(),
});
}
} catch (err) {
console.error('Deleting job failed: ', err);
throw new Error(err);
Expand All @@ -97,12 +113,13 @@ async function deleteDisk(disksClient, projectId, zone, diskName) {

async function createStoragePool(
storagePoolsClient,
zoneOperationsClient,
projectId,
zone,
storagePoolName
) {
try {
await storagePoolsClient.insert({
const [response] = await storagePoolsClient.insert({
project: projectId,
storagePoolResource: {
name: storagePoolName,
Expand All @@ -115,6 +132,16 @@ async function createStoragePool(
},
zone,
});
let operation = response.latestResponse;

// Wait for the delete operation to complete.
while (operation.status !== 'DONE') {
[operation] = await zoneOperationsClient.wait({
operation: operation.name,
project: projectId,
zone: operation.zone.split('/').pop(),
});
}
} catch (err) {
console.error('Creating storage pool failed: ', err);
throw new Error(err);
Expand All @@ -123,16 +150,27 @@ async function createStoragePool(

async function deleteStoragePool(
storagePoolsClient,
zoneOperationsClient,
projectId,
zone,
storagePoolName
) {
try {
await storagePoolsClient.delete({
const [response] = await storagePoolsClient.delete({
project: projectId,
storagePool: storagePoolName,
zone,
});
let operation = response.latestResponse;

// Wait for the delete operation to complete.
while (operation.status !== 'DONE') {
[operation] = await zoneOperationsClient.wait({
operation: operation.name,
project: projectId,
zone: operation.zone.split('/').pop(),
});
}
} catch (err) {
console.error('Deleting storage pool failed: ', err);
throw new Error(err);
Expand Down

0 comments on commit 5866c23

Please sign in to comment.