@@ -20,96 +20,82 @@ const path = require('path');
2020const { assert} = require ( 'chai' ) ;
2121const { after, before, describe, it} = require ( 'mocha' ) ;
2222const cp = require ( 'child_process' ) ;
23- const { DisksClient, StoragePoolsClient} = require ( '@google-cloud/compute' ) . v1 ;
23+ const { DisksClient, StoragePoolsClient, ZoneOperationsClient} =
24+ require ( '@google-cloud/compute' ) . v1 ;
2425
2526const execSync = cmd => cp . execSync ( cmd , { encoding : 'utf-8' } ) ;
2627const cwd = path . join ( __dirname , '..' ) ;
2728
28- describe ( 'Create compute hyperdisk from pool' , async ( ) => {
29- const diskName = 'disk-name-from-pool' ;
30- const zone = 'europe-central2-b' ;
31- const storagePoolName = 'storage-pool-name-hyperdisk' ;
29+ async function cleanupResources ( projectId , zone , diskName , storagePoolName ) {
3230 const disksClient = new DisksClient ( ) ;
3331 const storagePoolsClient = new StoragePoolsClient ( ) ;
32+ const zoneOperationsClient = new ZoneOperationsClient ( ) ;
33+ // Delete disk attached to storagePool
34+ const [ diskResponse ] = await disksClient . delete ( {
35+ project : projectId ,
36+ disk : diskName ,
37+ zone,
38+ } ) ;
39+
40+ let diskOperation = diskResponse . latestResponse ;
41+
42+ // Wait for the delete disk operation to complete.
43+ while ( diskOperation . status !== 'DONE' ) {
44+ [ diskOperation ] = await zoneOperationsClient . wait ( {
45+ operation : diskOperation . name ,
46+ project : projectId ,
47+ zone : diskOperation . zone . split ( '/' ) . pop ( ) ,
48+ } ) ;
49+ }
50+
51+ const [ poolResponse ] = await storagePoolsClient . delete ( {
52+ project : projectId ,
53+ storagePool : storagePoolName ,
54+ zone,
55+ } ) ;
56+ let poolOperation = poolResponse . latestResponse ;
57+
58+ // Wait for the delete pool operation to complete.
59+ while ( poolOperation . status !== 'DONE' ) {
60+ [ poolOperation ] = await zoneOperationsClient . wait ( {
61+ operation : poolOperation . name ,
62+ project : projectId ,
63+ zone : poolOperation . zone . split ( '/' ) . pop ( ) ,
64+ } ) ;
65+ }
66+ }
67+
68+ describe ( 'Create compute hyperdisk from pool' , async ( ) => {
69+ const diskName = 'disk-from-pool-name' ;
70+ const zone = 'us-central1-a' ;
71+ const storagePoolName = 'storage-pool-name' ;
72+ const disksClient = new DisksClient ( ) ;
3473 let projectId ;
3574
3675 before ( async ( ) => {
3776 projectId = await disksClient . getProjectId ( ) ;
3877
3978 // Ensure resources are deleted before attempting to recreate them
4079 try {
41- await disksClient . delete ( {
42- project : projectId ,
43- disk : diskName ,
44- zone,
45- } ) ;
46- } catch ( err ) {
47- // Should be ok to ignore (resource doesn't exist)
48- console . error ( err ) ;
49- }
50-
51- try {
52- await storagePoolsClient . delete ( {
53- project : projectId ,
54- storagePool : storagePoolName ,
55- zone,
56- } ) ;
80+ await cleanupResources ( projectId , zone , diskName , storagePoolName ) ;
5781 } catch ( err ) {
58- // Should be ok to ignore (resource doesn't exist)
82+ // Should be ok to ignore (resources do not exist)
5983 console . error ( err ) ;
6084 }
61-
62- await storagePoolsClient . insert ( {
63- project : projectId ,
64- storagePoolResource : {
65- name : storagePoolName ,
66- poolProvisionedCapacityGb : 10240 ,
67- poolProvisionedIops : 10000 ,
68- poolProvisionedThroughput : 1024 ,
69- storagePoolType : `projects/${ projectId } /zones/${ zone } /storagePoolTypes/hyperdisk-balanced` ,
70- capacityProvisioningType : 'advanced' ,
71- zone,
72- } ,
73- zone,
74- } ) ;
7585 } ) ;
7686
7787 after ( async ( ) => {
78- // Trying to delete the disk too quickly seems to fail
79- const deleteDisk = async ( ) => {
80- setTimeout ( async ( ) => {
81- await disksClient . delete ( {
82- project : projectId ,
83- disk : diskName ,
84- zone,
85- } ) ;
86- } , 120 * 1000 ) ; // wait two minutes
87- } ;
88-
89- try {
90- await deleteDisk ( ) ;
91- } catch {
92- // Try one more time after repeating the delay
93- await deleteDisk ( ) ;
94- }
88+ await cleanupResources ( projectId , zone , diskName , storagePoolName ) ;
89+ } ) ;
9590
96- // Need enough time after removing the disk before removing the pool
97- const deletePool = async ( ) => {
98- setTimeout ( async ( ) => {
99- await storagePoolsClient . delete ( {
100- project : projectId ,
101- storagePool : storagePoolName ,
102- zone,
103- } ) ;
104- } , 120 * 1000 ) ; // wait two minutes
105- } ;
91+ it ( 'should create a new storage pool' , ( ) => {
92+ const response = JSON . parse (
93+ execSync ( 'node ./disks/createComputeHyperdiskPool.js' , {
94+ cwd,
95+ } )
96+ ) ;
10697
107- try {
108- await deletePool ( ) ;
109- } catch {
110- // Try one more time after repeating the delay
111- await deletePool ( ) ;
112- }
98+ assert . equal ( response . name , storagePoolName ) ;
11399 } ) ;
114100
115101 it ( 'should create a new hyperdisk from pool' , ( ) => {
0 commit comments