look at all the IPs of the VM when waiting for an IP

This commit is contained in:
Joshua Foster 2020-06-17 17:40:21 -04:00
parent f92c52fc29
commit 50b6c86b1c
1 changed files with 17 additions and 27 deletions

View File

@ -493,38 +493,28 @@ func (vm *VirtualMachine) PowerOn() error {
} }
func (vm *VirtualMachine) WaitForIP(ctx context.Context, ipNet *net.IPNet) (string, error) { func (vm *VirtualMachine) WaitForIP(ctx context.Context, ipNet *net.IPNet) (string, error) {
var ip string netIP, err := vm.vm.WaitForNetIP(ctx, false)
p := property.DefaultCollector(vm.vm.Client())
err := property.Wait(ctx, p, vm.vm.Reference(), []string{"guest.ipAddress"}, func(pc []types.PropertyChange) bool {
for _, c := range pc {
if c.Name != "guest.ipAddress" {
continue
}
if c.Op != types.PropertyChangeOpAssign {
continue
}
if c.Val == nil {
continue
}
ip = c.Val.(string)
if ipNet != nil && !ipNet.Contains(net.ParseIP(ip)) {
// ip address is not in range
return false
}
return true
}
return false
})
if err != nil { if err != nil {
return "", err return "", err
} }
for _, ips := range netIP {
for _, ip := range ips {
parseIP := net.ParseIP(ip)
if ipNet != nil && !ipNet.Contains(parseIP) {
// ip address is not in range
continue
}
// default to an ipv4 addresses if no ipNet is defined
if ipNet == nil && parseIP.To4() == nil {
continue
}
return ip, nil return ip, nil
} }
}
return "", fmt.Errorf("unable to find an IP")
}
func (vm *VirtualMachine) PowerOff() error { func (vm *VirtualMachine) PowerOff() error {
state, err := vm.vm.PowerState(vm.driver.ctx) state, err := vm.vm.PowerState(vm.driver.ctx)