From 51e9266ee7212b029c03c3c1cdb55720fc511cbb Mon Sep 17 00:00:00 2001 From: Kyriakos Oikonomakos Date: Mon, 4 Nov 2019 21:53:23 +0000 Subject: [PATCH 1/2] 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 | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/vsphere/internal/virtualdevice/virtual_machine_disk_subresource.go b/vsphere/internal/virtualdevice/virtual_machine_disk_subresource.go index 4197a50ad..2c7e503c3 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" @@ -1697,6 +1698,22 @@ func (r *DiskSubresource) assignBackingInfo(disk *types.VirtualDisk) error { 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 From 97de16ed7fd711e1ce83ff9a8cfe89a893261671 Mon Sep 17 00:00:00 2001 From: Kyriakos Oikonomakos Date: Mon, 11 Nov 2019 16:02:12 +0000 Subject: [PATCH 2/2] Move the fallback logic --- .../virtual_machine_disk_subresource.go | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/vsphere/internal/virtualdevice/virtual_machine_disk_subresource.go b/vsphere/internal/virtualdevice/virtual_machine_disk_subresource.go index 2c7e503c3..c27239275 100644 --- a/vsphere/internal/virtualdevice/virtual_machine_disk_subresource.go +++ b/vsphere/internal/virtualdevice/virtual_machine_disk_subresource.go @@ -1696,23 +1696,23 @@ 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 - } + 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 + 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 {