Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vSphere Provider - Fix destroy when vm is powered off or has no networks #7206

Merged
merged 1 commit into from
Jul 12, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions builtin/providers/vsphere/resource_vsphere_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,12 +619,6 @@ func resourceVSphereVirtualMachineUpdate(d *schema.ResourceData, meta interface{
}
}

ip, err := vm.WaitForIP(context.TODO())
if err != nil {
return err
}
log.Printf("[DEBUG] ip address: %v", ip)

return resourceVSphereVirtualMachineRead(d, meta)
}

Expand Down Expand Up @@ -897,14 +891,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 +1040,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 +1896,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
}