-
Notifications
You must be signed in to change notification settings - Fork 90
/
net_interface.go
79 lines (68 loc) · 2.12 KB
/
net_interface.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package spec
// NetInterface represents network interface informations
type NetInterface struct {
Name string `json:"name"`
Encap string `json:"encap,omitempty"`
IPv4Addresses []string `json:"ipv4Addresses"`
IPv6Addresses []string `json:"ipv6Addresses"`
Address string `json:"address,omitempty"`
V6Address string `json:"v6address,omitempty"`
MacAddress string `json:"macAddress,omitempty"`
DefaultGateway string `json:"defaultGateway,omitempty"`
}
// NetInterfaces are map of network interfaces per name
type NetInterfaces map[string]NetInterface
func (ifs NetInterfaces) getOrNew(name string) NetInterface {
iface, ok := ifs[name]
if ok {
return iface
}
return NetInterface{Name: name}
}
// SetEncap sets the encap
func (ifs NetInterfaces) SetEncap(name, encap string) {
iface := ifs.getOrNew(name)
iface.Encap = encap
ifs[name] = iface
}
// SetAddress sets the address
func (ifs NetInterfaces) SetAddress(name, addr string) {
iface := ifs.getOrNew(name)
iface.Address = addr
ifs[name] = iface
}
// SetV6Address sets the v6address
func (ifs NetInterfaces) SetV6Address(name, addr string) {
iface := ifs.getOrNew(name)
iface.V6Address = addr
ifs[name] = iface
}
// SetMacAddress sets the macaddress
func (ifs NetInterfaces) SetMacAddress(name, addr string) {
iface := ifs.getOrNew(name)
iface.MacAddress = addr
ifs[name] = iface
}
// SetDefaultGateway sets the defaultGateway
func (ifs NetInterfaces) SetDefaultGateway(name, gateway string) {
iface := ifs.getOrNew(name)
iface.DefaultGateway = gateway
ifs[name] = iface
}
// AppendIPv4Address appends ipv4address
func (ifs NetInterfaces) AppendIPv4Address(name, addr string) {
iface := ifs.getOrNew(name)
iface.IPv4Addresses = append(iface.IPv4Addresses, addr)
ifs[name] = iface
}
// AppendIPv6Address appends ipv6address
func (ifs NetInterfaces) AppendIPv6Address(name, addr string) {
iface := ifs.getOrNew(name)
iface.IPv6Addresses = append(iface.IPv6Addresses, addr)
ifs[name] = iface
}
// InterfaceGenerator retrieve network informations
type InterfaceGenerator interface {
Key() string
Generate() ([]NetInterface, error)
}