From c26e5f047cfec4344cd48b77effcd411677d9ff7 Mon Sep 17 00:00:00 2001 From: Kyriakos Oikonomakos Date: Mon, 11 Nov 2019 16:12:22 +0000 Subject: [PATCH] Pick default datastore for extra disks (#897) Pick default datastore for extra disks When cloning virtual machines it is possible to define additional disks to be created. If a datastore is not defined either for the disk or for the virtual machine then the creation of the additional virtual machines will fail. This change introduces a fallback where if a datastore is not set we pick the first datastore found that is associated with the virtual machine. --- .../virtual_machine_disk_subresource.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/vsphere/internal/virtualdevice/virtual_machine_disk_subresource.go b/vsphere/internal/virtualdevice/virtual_machine_disk_subresource.go index 4197a50ad..c27239275 100644 --- a/vsphere/internal/virtualdevice/virtual_machine_disk_subresource.go +++ b/vsphere/internal/virtualdevice/virtual_machine_disk_subresource.go @@ -3,6 +3,7 @@ package virtualdevice import ( "errors" "fmt" + "github.com/terraform-providers/terraform-provider-vsphere/vsphere/internal/helper/virtualmachine" "log" "math" "path" @@ -1695,8 +1696,24 @@ func (r *DiskSubresource) assignBackingInfo(disk *types.VirtualDisk) error { if dsID == "" || dsID == diskDatastoreComputedName { // Default to the default datastore dsID = r.rdd.Get("datastore_id").(string) - } + if dsID == "" { + vmObj, err := virtualmachine.FromUUID(r.client, r.rdd.Id()) + if err != nil { + return err + } + + vmprops, err := virtualmachine.Properties(vmObj) + if err != nil { + return err + } + if len(vmprops.Datastore) == 0 { + return fmt.Errorf("no datastore was set and was unable to find a default to fall back to") + } + dsID = vmprops.Datastore[0].Value + + } + } ds, err := datastore.FromID(r.client, dsID) if err != nil { return err