-
Notifications
You must be signed in to change notification settings - Fork 115
/
interface_address.go
56 lines (44 loc) · 1.3 KB
/
interface_address.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
package ip
import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type InterfaceAddress interface {
GetInterfaceName() string
// GetIP gets the exposed internet protocol address of the above interface
GetIP() (string, error)
}
type simpleInterfaceAddress struct {
interfaceName string
ip string
}
func NewSimpleInterfaceAddress(interfaceName string, ip string) InterfaceAddress {
return simpleInterfaceAddress{interfaceName: interfaceName, ip: ip}
}
func (s simpleInterfaceAddress) GetInterfaceName() string { return s.interfaceName }
func (s simpleInterfaceAddress) GetIP() (string, error) { return s.ip, nil }
type resolvingInterfaceAddress struct {
interfaceName string
ipResolver Resolver
ip string
}
func NewResolvingInterfaceAddress(
interfaceName string,
ipResolver Resolver,
) InterfaceAddress {
return &resolvingInterfaceAddress{
interfaceName: interfaceName,
ipResolver: ipResolver,
}
}
func (s resolvingInterfaceAddress) GetInterfaceName() string { return s.interfaceName }
func (s *resolvingInterfaceAddress) GetIP() (string, error) {
if s.ip != "" {
return s.ip, nil
}
ip, err := s.ipResolver.GetPrimaryIPv4(s.interfaceName)
if err != nil {
return "", bosherr.WrapError(err, "Getting primary IPv4")
}
s.ip = ip.IP.String()
return s.ip, nil
}