-
Notifications
You must be signed in to change notification settings - Fork 115
/
interface_addresses_validator.go
51 lines (42 loc) · 1.61 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
package ip
import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type InterfaceAddressesValidator interface {
Validate(desiredInterfaceAddresses []InterfaceAddress) error
}
type interfaceAddressesValidator struct {
interfaceAddrsProvider InterfaceAddressesProvider
}
func NewInterfaceAddressesValidator(interfaceAddrsProvider InterfaceAddressesProvider) InterfaceAddressesValidator {
return &interfaceAddressesValidator{
interfaceAddrsProvider: interfaceAddrsProvider,
}
}
func (i *interfaceAddressesValidator) Validate(desiredInterfaceAddresses []InterfaceAddress) error {
systemInterfaceAddresses, err := i.interfaceAddrsProvider.Get()
if err != nil {
return bosherr.WrapError(err, "Getting network interface addresses")
}
for _, desiredInterfaceAddress := range desiredInterfaceAddresses {
ifaceName := desiredInterfaceAddress.GetInterfaceName()
iface, found := i.findInterfaceByName(ifaceName, systemInterfaceAddresses)
if !found {
return bosherr.WrapErrorf(err, "Validating network interface '%s' IP addresses, no interface configured with that name", ifaceName)
}
desiredIP, _ := desiredInterfaceAddress.GetIP()
actualIP, _ := iface.GetIP()
if desiredIP != actualIP {
return bosherr.WrapErrorf(err, "Validating network interface '%s' IP addresses, expected: '%s', actual: '%s'", ifaceName, desiredIP, actualIP)
}
}
return nil
}
func (i *interfaceAddressesValidator) findInterfaceByName(ifaceName string, ifaces []InterfaceAddress) (InterfaceAddress, bool) {
for _, iface := range ifaces {
if iface.GetInterfaceName() == ifaceName {
return iface, true
}
}
return nil, false
}