Skip to content

Commit 9cbdc80

Browse files
dkallegstack72
authored andcommitted
vSphere Provider - Fix destroy when vm is powered off or has networks (hashicorp#7206)
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 hashicorp#7168
1 parent 2f3f1da commit 9cbdc80

1 file changed

Lines changed: 23 additions & 15 deletions

File tree

builtin/providers/vsphere/resource_vsphere_virtual_machine.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -638,12 +638,6 @@ func resourceVSphereVirtualMachineUpdate(d *schema.ResourceData, meta interface{
638638
}
639639
}
640640

641-
ip, err := vm.WaitForIP(context.TODO())
642-
if err != nil {
643-
return err
644-
}
645-
log.Printf("[DEBUG] ip address: %v", ip)
646-
647641
return resourceVSphereVirtualMachineRead(d, meta)
648642
}
649643

@@ -916,14 +910,20 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{})
916910
return nil
917911
}
918912

919-
var mvm mo.VirtualMachine
920-
921-
// wait for interfaces to appear
922-
_, err = vm.WaitForNetIP(context.TODO(), true)
913+
state, err := vm.PowerState(context.TODO())
923914
if err != nil {
924915
return err
925916
}
926917

918+
if state == types.VirtualMachinePowerStatePoweredOn {
919+
// wait for interfaces to appear
920+
_, err = vm.WaitForNetIP(context.TODO(), true)
921+
if err != nil {
922+
return err
923+
}
924+
}
925+
926+
var mvm mo.VirtualMachine
927927
collector := property.DefaultCollector(client.Client)
928928
if err := collector.RetrieveOne(context.TODO(), vm.Reference(), []string{"guest", "summary", "datastore", "config"}, &mvm); err != nil {
929929
return err
@@ -1059,11 +1059,15 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{})
10591059
return fmt.Errorf("Invalid network interfaces to set: %#v", networkInterfaces)
10601060
}
10611061

1062-
log.Printf("[DEBUG] ip address: %v", networkInterfaces[0]["ipv4_address"].(string))
1063-
d.SetConnInfo(map[string]string{
1064-
"type": "ssh",
1065-
"host": networkInterfaces[0]["ipv4_address"].(string),
1066-
})
1062+
if len(networkInterfaces) > 0 {
1063+
if _, ok := networkInterfaces[0]["ipv4_address"]; ok {
1064+
log.Printf("[DEBUG] ip address: %v", networkInterfaces[0]["ipv4_address"].(string))
1065+
d.SetConnInfo(map[string]string{
1066+
"type": "ssh",
1067+
"host": networkInterfaces[0]["ipv4_address"].(string),
1068+
})
1069+
}
1070+
}
10671071

10681072
var rootDatastore string
10691073
for _, v := range mvm.Datastore {
@@ -1989,6 +1993,10 @@ func (vm *virtualMachine) setupVirtualMachine(c *govmomi.Client) error {
19891993

19901994
if vm.hasBootableVmdk || vm.template != "" {
19911995
newVM.PowerOn(context.TODO())
1996+
err = newVM.WaitForPowerState(context.TODO(), types.VirtualMachinePowerStatePoweredOn)
1997+
if err != nil {
1998+
return err
1999+
}
19922000
}
19932001
return nil
19942002
}

0 commit comments

Comments
 (0)