Skip to content

Commit 76e98e9

Browse files
skevirtenthirtyam
authored andcommitted
fix: Handle fm.MakeDirectory failing when creating parent directories
for a virtual disk
1 parent dea5add commit 76e98e9

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
@@ -13,6 +13,7 @@ import (
1313
"path"
1414
"strconv"
1515
"strings"
16+
"time"
1617

1718
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1819
"github.com/vmware/govmomi"
@@ -172,16 +173,27 @@ func resourceVSphereVirtualDiskCreate(d *schema.ResourceData, meta interface{})
172173
if directoryPathIndex > 0 {
173174
vmdkPath := vDisk.vmdkPath[0:directoryPathIndex]
174175
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)
179187
}
180188

181189
err = searchForDirectory(client, vDisk.datacenter, vDisk.datastore, vmdkPath)
182190
if err != nil {
183191
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+
}
185197
}
186198
}
187199
}
@@ -423,11 +435,6 @@ func resourceVSphereVirtualDiskDelete(d *schema.ResourceData, meta interface{})
423435
return nil
424436
}
425437

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-
431438
// createHardDisk creates a new Hard Disk.
432439
func createHardDisk(client *govmomi.Client, size int, diskPath string, diskType string, adapterType string, dc string) error {
433440
var vDiskType string

0 commit comments

Comments
 (0)