Skip to content

Commit

Permalink
r/virtual_machine: Relocate VMs after create
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bill-rich committed Aug 20, 2018
1 parent 29d1531 commit 948c633
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions vsphere/resource_vsphere_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 948c633

Please # to comment.