@@ -20,46 +20,72 @@ 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
29+ async function cleanupResources ( projectId , zone , diskName , storagePoolName ) {
30+ const disksClient = new DisksClient ( ) ;
31+ 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+
2868describe ( 'Create compute hyperdisk from pool' , async ( ) => {
2969 const diskName = 'disk-name-from-pool' ;
3070 const zone = 'europe-central2-b' ;
3171 const storagePoolName = 'storage-pool-name-hyperdisk' ;
3272 const disksClient = new DisksClient ( ) ;
3373 const storagePoolsClient = new StoragePoolsClient ( ) ;
74+ const zoneOperationsClient = new ZoneOperationsClient ( ) ;
3475 let projectId ;
3576
3677 before ( async ( ) => {
3778 projectId = await disksClient . getProjectId ( ) ;
3879
3980 // Ensure resources are deleted before attempting to recreate them
4081 try {
41- await disksClient . delete ( {
42- project : projectId ,
43- disk : diskName ,
44- zone,
45- } ) ;
82+ await cleanupResources ( projectId , zone , diskName , storagePoolName ) ;
4683 } catch ( err ) {
47- // Should be ok to ignore (resource doesn't exist)
84+ // Should be ok to ignore (resources do not exist)
4885 console . error ( err ) ;
4986 }
5087
51- try {
52- await storagePoolsClient . delete ( {
53- project : projectId ,
54- storagePool : storagePoolName ,
55- zone,
56- } ) ;
57- } catch ( err ) {
58- // Should be ok to ignore (resource doesn't exist)
59- console . error ( err ) ;
60- }
61-
62- await storagePoolsClient . insert ( {
88+ const [ response ] = await storagePoolsClient . insert ( {
6389 project : projectId ,
6490 storagePoolResource : {
6591 name : storagePoolName ,
@@ -72,44 +98,20 @@ describe('Create compute hyperdisk from pool', async () => {
7298 } ,
7399 zone,
74100 } ) ;
75- } ) ;
76-
77- 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- } ;
101+ let operation = response . latestResponse ;
88102
89- try {
90- await deleteDisk ( ) ;
91- } catch {
92- // Try one more time after repeating the delay
93- await deleteDisk ( ) ;
103+ // Wait for the insert pool operation to complete.
104+ while ( operation . status !== 'DONE' ) {
105+ [ operation ] = await zoneOperationsClient . wait ( {
106+ operation : operation . name ,
107+ project : projectId ,
108+ zone : operation . zone . split ( '/' ) . pop ( ) ,
109+ } ) ;
94110 }
111+ } ) ;
95112
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- } ;
106-
107- try {
108- await deletePool ( ) ;
109- } catch {
110- // Try one more time after repeating the delay
111- await deletePool ( ) ;
112- }
113+ after ( async ( ) => {
114+ await cleanupResources ( projectId , zone , diskName , storagePoolName ) ;
113115 } ) ;
114116
115117 it ( 'should create a new hyperdisk from pool' , ( ) => {
0 commit comments