From 948c633ce46337fdfd9c240b905e99c901302013 Mon Sep 17 00:00:00 2001 From: Bill Rich Date: Thu, 9 Aug 2018 10:16:58 -0700 Subject: [PATCH] r/virtual_machine: Relocate VMs after create The host attribute of CreateVM_Task is being ignored in vCenter 6.7. Adding a host check and reloaction step to ensure that VMs land on the correct host. --- vsphere/resource_vsphere_virtual_machine.go | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/vsphere/resource_vsphere_virtual_machine.go b/vsphere/resource_vsphere_virtual_machine.go index 9464d1c58..50d1a09be 100644 --- a/vsphere/resource_vsphere_virtual_machine.go +++ b/vsphere/resource_vsphere_virtual_machine.go @@ -266,6 +266,31 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{ } } + // The host attribute of CreateVM_Task seems to be ignored in vCenter 6.7. + // Ensure that VMs are on the correct host and relocate if necessary. Do this + // near the end of the VM creation since it involves updating the + // ResourceData. + vprops, err := virtualmachine.Properties(vm) + if err != nil { + return err + } + if d.Get("host_system_id").(string) != vprops.Runtime.Host.Reference().Value { + hid := d.Get("host_system_id").(string) + err = resourceVSphereVirtualMachineRead(d, meta) + if err != nil { + return err + } + // Restore the old host_system_id so we can still tell if a relocation is + // necessary. + err = d.Set("host_system_id", hid) + if err != nil { + return err + } + if err = resourceVSphereVirtualMachineUpdateLocation(d, meta); err != nil { + return err + } + } + // Wait for a routable address if we have been set to wait for one err = virtualmachine.WaitForGuestNet( client,