@@ -20,96 +20,82 @@ const path = require('path');
20
20
const { assert} = require ( 'chai' ) ;
21
21
const { after, before, describe, it} = require ( 'mocha' ) ;
22
22
const cp = require ( 'child_process' ) ;
23
- const { DisksClient, StoragePoolsClient} = require ( '@google-cloud/compute' ) . v1 ;
23
+ const { DisksClient, StoragePoolsClient, ZoneOperationsClient} =
24
+ require ( '@google-cloud/compute' ) . v1 ;
24
25
25
26
const execSync = cmd => cp . execSync ( cmd , { encoding : 'utf-8' } ) ;
26
27
const cwd = path . join ( __dirname , '..' ) ;
27
28
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
+
28
68
describe ( 'Create compute hyperdisk from pool' , async ( ) => {
29
- const diskName = 'disk-name- from-pool' ;
69
+ const diskName = 'disk-from-pool-name ' ;
30
70
const zone = 'europe-central2-b' ;
31
- const storagePoolName = 'storage-pool-name-hyperdisk ' ;
71
+ const storagePoolName = 'storage-pool-name' ;
32
72
const disksClient = new DisksClient ( ) ;
33
- const storagePoolsClient = new StoragePoolsClient ( ) ;
34
73
let projectId ;
35
74
36
75
before ( async ( ) => {
37
76
projectId = await disksClient . getProjectId ( ) ;
38
77
39
78
// Ensure resources are deleted before attempting to recreate them
40
79
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 ) ;
57
81
} catch ( err ) {
58
- // Should be ok to ignore (resource doesn't exist)
82
+ // Should be ok to ignore (resources do not exist)
59
83
console . error ( err ) ;
60
84
}
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
- } ) ;
75
85
} ) ;
76
86
77
87
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
+ } ) ;
95
90
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
+ ) ;
106
97
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 ) ;
113
99
} ) ;
114
100
115
101
it ( 'should create a new hyperdisk from pool' , ( ) => {
0 commit comments