forked from infobloxopen/infoblox-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_manager_fixed_address.go
166 lines (148 loc) · 4.23 KB
/
object_manager_fixed_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package ibclient
import (
"fmt"
"net"
"regexp"
)
func (objMgr *ObjectManager) AllocateIP(
netview string,
cidr string,
ipAddr string,
isIPv6 bool,
macOrDuid string,
name string,
comment string,
eas EA) (*FixedAddress, error) {
if isIPv6 {
if len(macOrDuid) == 0 {
return nil, fmt.Errorf("the DUID field cannot be left empty")
}
} else {
if len(macOrDuid) == 0 {
macOrDuid = MACADDR_ZERO
}
}
if ipAddr == "" && cidr != "" {
if netview == "" {
netview = "default"
}
ipAddr = fmt.Sprintf("func:nextavailableip:%s,%s", cidr, netview)
}
fixedAddr := NewFixedAddress(
netview, name, ipAddr, cidr, macOrDuid, "", eas, "", isIPv6, comment)
ref, err := objMgr.connector.CreateObject(fixedAddr)
if err != nil {
return nil, err
}
fixedAddr.Ref = ref
fixedAddr, err = objMgr.GetFixedAddressByRef(ref)
return fixedAddr, err
}
func (objMgr *ObjectManager) GetFixedAddress(netview string, cidr string, ipAddr string, isIpv6 bool, macOrDuid string) (*FixedAddress, error) {
var res []FixedAddress
fixedAddr := NewEmptyFixedAddress(isIpv6)
sf := map[string]string{
"network_view": netview,
"network": cidr,
}
if isIpv6 {
sf["ipv6addr"] = ipAddr
if macOrDuid != "" {
sf["duid"] = macOrDuid
}
} else {
sf["ipv4addr"] = ipAddr
if macOrDuid != "" {
sf["mac"] = macOrDuid
}
}
queryParams := NewQueryParams(false, sf)
err := objMgr.connector.GetObject(fixedAddr, "", queryParams, &res)
if err != nil || res == nil || len(res) == 0 {
return nil, err
}
return &res[0], nil
}
func (objMgr *ObjectManager) GetFixedAddressByRef(ref string) (*FixedAddress, error) {
r := regexp.MustCompile("^ipv6fixedaddress/.+")
isIPv6 := r.MatchString(ref)
fixedAddr := NewEmptyFixedAddress(isIPv6)
err := objMgr.connector.GetObject(
fixedAddr, ref, NewQueryParams(false, nil), &fixedAddr)
return fixedAddr, err
}
func (objMgr *ObjectManager) UpdateFixedAddress(
fixedAddrRef string,
netview string,
name string,
cidr string,
ipAddr string,
matchClient string,
macOrDuid string,
comment string,
eas EA) (*FixedAddress, error) {
r := regexp.MustCompile("^ipv6fixedaddress/.+")
isIPv6 := r.MatchString(fixedAddrRef)
if !isIPv6 {
if !validateMatchClient(matchClient) {
return nil, fmt.Errorf("wrong value for match_client passed %s \n ", matchClient)
}
}
updateFixedAddr := NewFixedAddress(
"", name, "", "",
macOrDuid, matchClient, eas, fixedAddrRef, isIPv6, comment)
if ipAddr == "" {
if cidr != "" {
ipAddress, _, err := net.ParseCIDR(cidr)
if err != nil {
return nil, fmt.Errorf("cannot parse CIDR value: %s", err.Error())
}
if netview == "" {
netview = "default"
}
if isIPv6 {
if ipAddress.To4() != nil || ipAddress.To16() == nil {
return nil, fmt.Errorf("CIDR value must be an IPv6 CIDR, not an IPv4 one")
}
updateFixedAddr.IPv6Address = fmt.Sprintf("func:nextavailableip:%s,%s", cidr, netview)
} else {
if ipAddress.To4() == nil {
return nil, fmt.Errorf("CIDR value must be an IPv4 CIDR, not an IPv6 one")
}
updateFixedAddr.IPv4Address = fmt.Sprintf("func:nextavailableip:%s,%s", cidr, netview)
}
}
} else {
ipAddress := net.ParseIP(ipAddr)
if ipAddress == nil {
return nil, fmt.Errorf("IP address for the record is not valid")
}
if isIPv6 {
if ipAddress.To4() != nil || ipAddress.To16() == nil {
return nil, fmt.Errorf("IP address must be an IPv6 address, not an IPv4 one")
}
updateFixedAddr.IPv6Address = ipAddr
} else {
if ipAddress.To4() == nil {
return nil, fmt.Errorf("IP address must be an IPv4 address, not an IPv6 one")
}
updateFixedAddr.IPv4Address = ipAddr
}
}
refResp, err := objMgr.connector.UpdateObject(updateFixedAddr, fixedAddrRef)
updateFixedAddr, err = objMgr.GetFixedAddressByRef(refResp)
if err != nil {
return nil, err
}
return updateFixedAddr, nil
}
func (objMgr *ObjectManager) ReleaseIP(netview string, cidr string, ipAddr string, isIpv6 bool, macOrDuid string) (string, error) {
fixAddress, _ := objMgr.GetFixedAddress(netview, cidr, ipAddr, isIpv6, macOrDuid)
if fixAddress == nil {
return "", nil
}
return objMgr.connector.DeleteObject(fixAddress.Ref)
}
func (objMgr *ObjectManager) DeleteFixedAddress(ref string) (string, error) {
return objMgr.connector.DeleteObject(ref)
}