Skip to content

Commit

Permalink
Merge pull request #303 from rowanjacobs/master
Browse files Browse the repository at this point in the history
r/virtual_machine: add vApp properties
  • Loading branch information
vancluever authored Jan 13, 2018
2 parents d87e588 + ca780b0 commit 0e3cd6c
Show file tree
Hide file tree
Showing 5 changed files with 1,073 additions and 36 deletions.
1 change: 1 addition & 0 deletions tf-vsphere-devrc.mk.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export VSPHERE_ALLOW_UNVERIFIED_SSL ?= false
# succeed, it's probably best to set all of these to valid values.
export VSPHERE_TEMPLATE ?= base-linux # VM template to clone
export VSPHERE_TEMPLATE_WINDOWS ?= base-win # Windows VM template
export VSPHERE_TEMPLATE_COREOS ?= base-core # CoreOS template from OVA
export VSPHERE_NETWORK_LABEL ?= vm-network # Port group label
export VSPHERE_NETWORK_LABEL_DHCP ?= vm-network # Port group label for DHCP
export VSPHERE_NETWORK_LABEL_PXE ?= vm-network # Port group label for PXE
Expand Down
53 changes: 40 additions & 13 deletions vsphere/resource_vsphere_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,12 @@ func resourceVSphereVirtualMachineUpdate(d *schema.ResourceData, meta interface{
if err != nil {
return fmt.Errorf("error fetching VM properties: %s", err)
}
spec, changed := expandVirtualMachineConfigSpecChanged(d, client, vprops.Config)

spec, changed, err := expandVirtualMachineConfigSpecChanged(d, client, vprops.Config)
if err != nil {
return fmt.Errorf("error in virtual machine configuration: %s", err)
}

devices := object.VirtualDeviceList(vprops.Config.Hardware.Device)
if spec.DeviceChange, err = applyVirtualDevices(d, client, devices); err != nil {
return err
Expand Down Expand Up @@ -648,7 +653,10 @@ func resourceVSphereVirtualMachineCreateBare(d *schema.ResourceData, meta interf
}

// Ready to start making the VM here. First expand our main config spec.
spec := expandVirtualMachineConfigSpec(d, client)
spec, err := expandVirtualMachineConfigSpec(d, client)
if err != nil {
return nil, fmt.Errorf("error in virtual machine configuration: %s", err)
}

// Set the datastore for the VM.
ds, err := datastore.FromID(client, d.Get("datastore_id").(string))
Expand Down Expand Up @@ -741,7 +749,10 @@ func resourceVSphereVirtualMachineCreateClone(d *schema.ResourceData, meta inter
// configuration of the newly cloned VM. This is basically a subset of update
// with the stipulation that there is currently no state to help move this
// along.
cfgSpec := expandVirtualMachineConfigSpec(d, client)
cfgSpec, err := expandVirtualMachineConfigSpec(d, client)
if err != nil {
return nil, resourceVSphereVirtualMachineRollbackCreate(d, meta, vm, fmt.Errorf("error in virtual machine configuration: %s", err))
}

// To apply device changes, we need the current devicecfgSpec from the config
// info. We then filter this list through the same apply process we did for
Expand Down Expand Up @@ -777,16 +788,7 @@ func resourceVSphereVirtualMachineCreateClone(d *schema.ResourceData, meta inter

// Perform updates
if err := virtualmachine.Reconfigure(vm, cfgSpec); err != nil {
// Delete the virtual machine if the update failed here. Updates are
// largely atomic, so more than likely no disks with keep_on_remove were
// attached, but just in case, we run this through delete to make sure to
// safely remove any disk that may have been attached as part of this
// process if it was flagged as such.
if derr := resourceVSphereVirtualMachineDelete(d, meta); derr != nil {
return nil, fmt.Errorf(formatVirtualMachinePostCloneRollbackError, vm.InventoryPath, err, derr)
}
d.SetId("")
return nil, fmt.Errorf("error reconfiguring virtual machine: %s", err)
return nil, resourceVSphereVirtualMachineRollbackCreate(d, meta, vm, fmt.Errorf("error reconfiguring virtual machine: %s", err))
}

var cw *virtualMachineCustomizationWaiter
Expand Down Expand Up @@ -824,6 +826,31 @@ func resourceVSphereVirtualMachineCreateClone(d *schema.ResourceData, meta inter
return vm, nil
}

// resourceVSphereVirtualMachineRollbackCreate attempts to "roll back" a
// resource due to an error that happened post-create that will put the VM in a
// state where it cannot be worked with. This should only be done early on in
// the process, namely on clone operations between when the clone actually
// happens, and no later than after the initial post-clone update is complete.
//
// If the rollback fails, an error is displayed prompting the user to manually
// delete the virtual machine before trying again.
func resourceVSphereVirtualMachineRollbackCreate(
d *schema.ResourceData,
meta interface{},
vm *object.VirtualMachine,
origErr error,
) error {
// Updates are largely atomic, so more than likely no disks with
// keep_on_remove were attached, but just in case, we run this through delete
// to make sure to safely remove any disk that may have been attached as part
// of this process if it was flagged as such.
if err := resourceVSphereVirtualMachineDelete(d, meta); err != nil {
return fmt.Errorf(formatVirtualMachinePostCloneRollbackError, vm.InventoryPath, origErr, err)
}
d.SetId("")
return fmt.Errorf("error reconfiguring virtual machine: %s", origErr)
}

// resourceVSphereVirtualMachineUpdateLocation manages vMotion. This includes
// the migration of a VM from one host to another, or from one datastore to
// another (storage vMotion).
Expand Down
Loading

0 comments on commit 0e3cd6c

Please sign in to comment.