look at all the IPs of the VM when waiting for an IP
This commit is contained in:
parent
f92c52fc29
commit
50b6c86b1c
|
@ -493,37 +493,27 @@ func (vm *VirtualMachine) PowerOn() error {
|
|||
}
|
||||
|
||||
func (vm *VirtualMachine) WaitForIP(ctx context.Context, ipNet *net.IPNet) (string, error) {
|
||||
var ip string
|
||||
|
||||
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
|
||||
})
|
||||
|
||||
netIP, err := vm.vm.WaitForNetIP(ctx, false)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return ip, nil
|
||||
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 "", fmt.Errorf("unable to find an IP")
|
||||
}
|
||||
|
||||
func (vm *VirtualMachine) PowerOff() error {
|
||||
|
|
Loading…
Reference in New Issue