@@ -13,6 +13,7 @@ import (
13
13
"path"
14
14
"strconv"
15
15
"strings"
16
+ "sync"
16
17
17
18
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
18
19
"github.com/vmware/govmomi"
@@ -22,6 +23,8 @@ import (
22
23
"github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/virtualdisk"
23
24
)
24
25
26
+ var vsphereVirtualDiskMakeDirectoryMutex sync.Mutex
27
+
25
28
type virtualDisk struct {
26
29
size int
27
30
vmdkPath string
@@ -170,9 +173,14 @@ func resourceVSphereVirtualDiskCreate(d *schema.ResourceData, meta interface{})
170
173
if vDisk .createDirectories {
171
174
directoryPathIndex := strings .LastIndex (vDisk .vmdkPath , "/" )
172
175
if directoryPathIndex > 0 {
176
+ // Only allow one MakeDirectory operation at a time in order to avoid
177
+ // overlapping attempts to create the same directory, which can result
178
+ // in some of the attempts failing.
179
+ vsphereVirtualDiskMakeDirectoryMutex .Lock ()
173
180
vmdkPath := vDisk .vmdkPath [0 :directoryPathIndex ]
174
181
log .Printf ("[DEBUG] Creating parent directories: %v" , ds .Path (vmdkPath ))
175
182
err = fm .MakeDirectory (context .TODO (), ds .Path (vmdkPath ), dc , true )
183
+ vsphereVirtualDiskMakeDirectoryMutex .Unlock ()
176
184
if err != nil && ! isAlreadyExists (err ) {
177
185
log .Printf ("[DEBUG] Failed to create parent directories: %v" , err )
178
186
return err
0 commit comments