Skip to content

Commit 3a03e78

Browse files
committed
fix: Handle fm.MakeDirectory failing when creating parent directories
for a virtual disk
1 parent 427af22 commit 3a03e78

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

vsphere/resource_vsphere_virtual_disk.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"log"
1010
"strconv"
1111
"strings"
12+
"time"
1213

1314
"errors"
1415
"path"
@@ -173,16 +174,27 @@ func resourceVSphereVirtualDiskCreate(d *schema.ResourceData, meta interface{})
173174
if directoryPathIndex > 0 {
174175
vmdkPath := vDisk.vmdkPath[0:directoryPathIndex]
175176
log.Printf("[DEBUG] Creating parent directories: %v", ds.Path(vmdkPath))
176-
err = fm.MakeDirectory(context.TODO(), ds.Path(vmdkPath), dc, true)
177-
if err != nil && !isAlreadyExists(err) {
178-
log.Printf("[DEBUG] Failed to create parent directories: %v", err)
179-
return err
177+
makeDirectoryErr := fm.MakeDirectory(context.TODO(), ds.Path(vmdkPath), dc, true)
178+
if makeDirectoryErr != nil {
179+
// MakeDirectory can fail with a number of different errors if the directory is
180+
// already in the process of being created. To avoid the need to handle each of
181+
// these errors individually, the error is not returned here and the call to
182+
// searchForDirectory below is used to determine if the directory is present.
183+
// If the directory is in the process of being created, that will need to complete
184+
// in order for the search to find the new directory, so sleep for a couple of
185+
// seconds to allow that.
186+
log.Printf("[DEBUG] Error hit when creating parent directories: %v", makeDirectoryErr)
187+
time.Sleep(2 * time.Second)
180188
}
181189

182190
err = searchForDirectory(client, vDisk.datacenter, vDisk.datastore, vmdkPath)
183191
if err != nil {
184192
log.Printf("[DEBUG] Failed to find newly created parent directories: %v", err)
185-
return err
193+
if makeDirectoryErr != nil {
194+
return makeDirectoryErr
195+
} else {
196+
return err
197+
}
186198
}
187199
}
188200
}
@@ -424,11 +436,6 @@ func resourceVSphereVirtualDiskDelete(d *schema.ResourceData, meta interface{})
424436
return nil
425437
}
426438

427-
func isAlreadyExists(err error) bool {
428-
return strings.HasPrefix(err.Error(), "Cannot complete the operation because the file or folder") &&
429-
strings.HasSuffix(err.Error(), "already exists")
430-
}
431-
432439
// createHardDisk creates a new Hard Disk.
433440
func createHardDisk(client *govmomi.Client, size int, diskPath string, diskType string, adapterType string, dc string) error {
434441
var vDiskType string

0 commit comments

Comments
 (0)