diff --git a/builder/vsphere/driver/vm.go b/builder/vsphere/driver/vm.go index dc23f9e94..b62ca984c 100644 --- a/builder/vsphere/driver/vm.go +++ b/builder/vsphere/driver/vm.go @@ -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 {