-
Notifications
You must be signed in to change notification settings - Fork 115
/
interface_addresses_validator.go
61 lines (48 loc) · 1.82 KB
/
interface_addresses_validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package ip
import (
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type InterfaceAddressesValidator struct {
interfaceAddrsProvider InterfaceAddressesProvider
desiredInterfaceAddresses []InterfaceAddress
}
func NewInterfaceAddressesValidator(interfaceAddrsProvider InterfaceAddressesProvider, desiredInterfaceAddresses []InterfaceAddress) InterfaceAddressesValidator {
return InterfaceAddressesValidator{
interfaceAddrsProvider: interfaceAddrsProvider,
desiredInterfaceAddresses: desiredInterfaceAddresses,
}
}
func (i InterfaceAddressesValidator) Attempt() (bool, error) {
systemInterfaceAddresses, err := i.interfaceAddrsProvider.Get()
if err != nil {
return true, bosherr.WrapError(err, "Getting network interface addresses")
}
for _, desiredInterfaceAddress := range i.desiredInterfaceAddresses {
ifaceName := desiredInterfaceAddress.GetInterfaceName()
ifaces := i.findInterfaceByName(ifaceName, systemInterfaceAddresses)
if len(ifaces) == 0 {
return true, bosherr.Errorf("Validating network interface '%s' IP addresses, no interface configured with that name", ifaceName)
}
actualIPs := []string{}
desiredIP, _ := desiredInterfaceAddress.GetIP()
for _, iface := range ifaces {
actualIP, _ := iface.GetIP()
if desiredIP == actualIP {
return false, nil
}
actualIPs = append(actualIPs, actualIP)
}
return true, bosherr.Errorf("Validating network interface '%s' IP addresses, expected: '%s', actual: [%s]", ifaceName, desiredIP, strings.Join(actualIPs, ", "))
}
return false, nil
}
func (i InterfaceAddressesValidator) findInterfaceByName(ifaceName string, ifaces []InterfaceAddress) []InterfaceAddress {
result := []InterfaceAddress{}
for _, iface := range ifaces {
if iface.GetInterfaceName() == ifaceName {
result = append(result, iface)
}
}
return result
}