@@ -13,6 +13,7 @@ import (
13
13
"path"
14
14
"strconv"
15
15
"strings"
16
+ "time"
16
17
17
18
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
18
19
"github.com/vmware/govmomi"
@@ -172,16 +173,27 @@ func resourceVSphereVirtualDiskCreate(d *schema.ResourceData, meta interface{})
172
173
if directoryPathIndex > 0 {
173
174
vmdkPath := vDisk .vmdkPath [0 :directoryPathIndex ]
174
175
log .Printf ("[DEBUG] Creating parent directories: %v" , ds .Path (vmdkPath ))
175
- err = fm .MakeDirectory (context .TODO (), ds .Path (vmdkPath ), dc , true )
176
- if err != nil && ! isAlreadyExists (err ) {
177
- log .Printf ("[DEBUG] Failed to create parent directories: %v" , err )
178
- return err
176
+ makeDirectoryErr := fm .MakeDirectory (context .TODO (), ds .Path (vmdkPath ), dc , true )
177
+ if makeDirectoryErr != nil {
178
+ // MakeDirectory can fail with a number of different errors if the directory is
179
+ // already in the process of being created. To avoid the need to handle each of
180
+ // these errors individually, the error is not returned here and the call to
181
+ // searchForDirectory below is used to determine if the directory is present.
182
+ // If the directory is in the process of being created, that will need to complete
183
+ // in order for the search to find the new directory, so sleep for a couple of
184
+ // seconds to allow that.
185
+ log .Printf ("[DEBUG] Error hit when creating parent directories: %v" , makeDirectoryErr )
186
+ time .Sleep (2 * time .Second )
179
187
}
180
188
181
189
err = searchForDirectory (client , vDisk .datacenter , vDisk .datastore , vmdkPath )
182
190
if err != nil {
183
191
log .Printf ("[DEBUG] Failed to find newly created parent directories: %v" , err )
184
- return err
192
+ if makeDirectoryErr != nil {
193
+ return makeDirectoryErr
194
+ } else {
195
+ return err
196
+ }
185
197
}
186
198
}
187
199
}
@@ -423,11 +435,6 @@ func resourceVSphereVirtualDiskDelete(d *schema.ResourceData, meta interface{})
423
435
return nil
424
436
}
425
437
426
- func isAlreadyExists (err error ) bool {
427
- return strings .HasPrefix (err .Error (), "Cannot complete the operation because the file or folder" ) &&
428
- strings .HasSuffix (err .Error (), "already exists" )
429
- }
430
-
431
438
// createHardDisk creates a new Hard Disk.
432
439
func createHardDisk (client * govmomi.Client , size int , diskPath string , diskType string , adapterType string , dc string ) error {
433
440
var vDiskType string
0 commit comments