|
9 | 9 | "log"
|
10 | 10 | "strconv"
|
11 | 11 | "strings"
|
| 12 | + "time" |
12 | 13 |
|
13 | 14 | "errors"
|
14 | 15 | "path"
|
@@ -173,16 +174,27 @@ func resourceVSphereVirtualDiskCreate(d *schema.ResourceData, meta interface{})
|
173 | 174 | if directoryPathIndex > 0 {
|
174 | 175 | vmdkPath := vDisk.vmdkPath[0:directoryPathIndex]
|
175 | 176 | 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) |
180 | 188 | }
|
181 | 189 |
|
182 | 190 | err = searchForDirectory(client, vDisk.datacenter, vDisk.datastore, vmdkPath)
|
183 | 191 | if err != nil {
|
184 | 192 | 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 | + } |
186 | 198 | }
|
187 | 199 | }
|
188 | 200 | }
|
@@ -424,11 +436,6 @@ func resourceVSphereVirtualDiskDelete(d *schema.ResourceData, meta interface{})
|
424 | 436 | return nil
|
425 | 437 | }
|
426 | 438 |
|
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 |
| - |
432 | 439 | // createHardDisk creates a new Hard Disk.
|
433 | 440 | func createHardDisk(client *govmomi.Client, size int, diskPath string, diskType string, adapterType string, dc string) error {
|
434 | 441 | var vDiskType string
|
|
0 commit comments