Skip to content

Commit

Permalink
vSphere Provider - Fix destroy when vm is powered off or has networks
Browse files Browse the repository at this point in the history
This patch adds a wait when powering on a vm so setupVirtualMachine does
not return until the vm is actually powered on.  This allows other
functions to work off the assumption that the current state of the vm
is not in flux. During resourceVSphereVirtualMachineRead(), the wait for
IP would cause a hang for any VM with no network interfaces or for vms
that had been powered off for any reason.  This also means that the user
could not delete a vm with no network interfaces or that is powered off.
Checking power state before trying to check for network interfaces.
Resolves #7168
  • Loading branch information
dkalleg committed Jun 20, 2016
1 parent f65a898 commit 6881fc0
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions builtin/providers/vsphere/resource_vsphere_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ func resourceVSphereVirtualMachineUpdate(d *schema.ResourceData, meta interface{
}
}

ip, err := vm.WaitForIP(context.TODO())
ip, err := vm.WaitForNetIP(context.TODO(), true)
if err != nil {
return err
}
Expand Down Expand Up @@ -897,14 +897,20 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{})
return nil
}

var mvm mo.VirtualMachine

// wait for interfaces to appear
_, err = vm.WaitForNetIP(context.TODO(), true)
state, err := vm.PowerState(context.TODO())
if err != nil {
return err
}

if state == types.VirtualMachinePowerStatePoweredOn {
// wait for interfaces to appear
_, err = vm.WaitForNetIP(context.TODO(), true)
if err != nil {
return err
}
}

var mvm mo.VirtualMachine
collector := property.DefaultCollector(client.Client)
if err := collector.RetrieveOne(context.TODO(), vm.Reference(), []string{"guest", "summary", "datastore", "config"}, &mvm); err != nil {
return err
Expand Down Expand Up @@ -1040,11 +1046,15 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Invalid network interfaces to set: %#v", networkInterfaces)
}

log.Printf("[DEBUG] ip address: %v", networkInterfaces[0]["ipv4_address"].(string))
d.SetConnInfo(map[string]string{
"type": "ssh",
"host": networkInterfaces[0]["ipv4_address"].(string),
})
if len(networkInterfaces) > 0 {
if _, ok := networkInterfaces[0]["ipv4_address"]; ok {
log.Printf("[DEBUG] ip address: %v", networkInterfaces[0]["ipv4_address"].(string))
d.SetConnInfo(map[string]string{
"type": "ssh",
"host": networkInterfaces[0]["ipv4_address"].(string),
})
}
}

var rootDatastore string
for _, v := range mvm.Datastore {
Expand Down Expand Up @@ -1892,6 +1902,10 @@ func (vm *virtualMachine) setupVirtualMachine(c *govmomi.Client) error {

if vm.hasBootableVmdk || vm.template != "" {
newVM.PowerOn(context.TODO())
err = newVM.WaitForPowerState(context.TODO(), types.VirtualMachinePowerStatePoweredOn)
if err != nil {
return err
}
}
return nil
}

0 comments on commit 6881fc0

Please sign in to comment.