forked from infobloxopen/infoblox-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
210 lines (168 loc) · 5.1 KB
/
utils.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package ibclient
import (
"fmt"
"net"
"net/url"
"regexp"
"strings"
"golang.org/x/net/idna"
)
type NotFoundError struct {
msg string
}
func (e *NotFoundError) Error() string {
return e.msg
}
func NewNotFoundError(msg string) *NotFoundError {
return &NotFoundError{msg: msg}
}
func BuildNetworkViewFromRef(ref string) *NetworkView {
// networkview/ZG5zLm5ldHdvcmtfdmlldyQyMw:global_view/false
r := regexp.MustCompile(`networkview/\w+:([^/]+)/\w+`)
m := r.FindStringSubmatch(ref)
if m == nil {
return nil
}
return &NetworkView{
Ref: ref,
Name: &m[1],
}
}
func BuildNetworkFromRef(ref string) (*Network, error) {
// network/ZG5zLm5ldHdvcmskODkuMC4wLjAvMjQvMjU:89.0.0.0/24/global_view
r := regexp.MustCompile(`network/\w+:(\d+\.\d+\.\d+\.\d+/\d+)/(.+)`)
m := r.FindStringSubmatch(ref)
if m == nil {
return nil, fmt.Errorf("CIDR format not matched")
}
newNet := NewNetwork(m[2], m[1], false, "", nil)
newNet.Ref = ref
return newNet, nil
}
func BuildNetworkContainerFromRef(ref string) (*NetworkContainer, error) {
// networkcontainer/ZG5zLm5ldHdvcmskODkuMC4wLjAvMjQvMjU:89.0.0.0/24/global_view
r := regexp.MustCompile(`networkcontainer/\w+:(\d+\.\d+\.\d+\.\d+/\d+)/(.+)`)
m := r.FindStringSubmatch(ref)
if m == nil {
return nil, fmt.Errorf("CIDR format not matched")
}
newNet := NewNetworkContainer(m[2], m[1], false, "", nil)
newNet.Ref = ref
return newNet, nil
}
func BuildIPv6NetworkContainerFromRef(ref string) (*NetworkContainer, error) {
// ipv6networkcontainer/ZG5zLm5ldHdvcmskODkuMC4wLjAvMjQvMjU:2001%3Adb8%3Aabcd%3A0012%3A%3A0/64/global_view
r := regexp.MustCompile(`ipv6networkcontainer/[^:]+:(([^\/]+)\/\d+)\/(.+)`)
m := r.FindStringSubmatch(ref)
if m == nil {
return nil, fmt.Errorf("CIDR format not matched")
}
cidr, err := url.QueryUnescape(m[1])
if err != nil {
return nil, fmt.Errorf(
"cannot extract network CIDR information from the reference '%s': %s",
ref, err.Error())
}
if _, _, err = net.ParseCIDR(cidr); err != nil {
return nil, fmt.Errorf("CIDR format not matched")
}
newNet := NewNetworkContainer(m[3], cidr, true, "", nil)
newNet.Ref = ref
return newNet, nil
}
func GetIPAddressFromRef(ref string) string {
// fixedaddress/ZG5zLmJpbmRfY25h:12.0.10.1/external
r := regexp.MustCompile(`fixedaddress/\w+:(\d+\.\d+\.\d+\.\d+)/.+`)
m := r.FindStringSubmatch(ref)
if m != nil {
return m[1]
}
return ""
}
// validation for match_client
func validateMatchClient(value string) bool {
matchClientList := [5]string{
"MAC_ADDRESS",
"CLIENT_ID",
"RESERVED",
"CIRCUIT_ID",
"REMOTE_ID"}
for _, val := range matchClientList {
if val == value {
return true
}
}
return false
}
func BuildIPv6NetworkFromRef(ref string) (*Network, error) {
// ipv6network/ZG5zLm5ldHdvcmskODkuMC4wLjAvMjQvMjU:2001%3Adb8%3Aabcd%3A0012%3A%3A0/64/global_view
r := regexp.MustCompile(`ipv6network/[^:]+:(([^\/]+)\/\d+)\/(.+)`)
m := r.FindStringSubmatch(ref)
if m == nil {
return nil, fmt.Errorf("CIDR format not matched")
}
cidr, err := url.QueryUnescape(m[1])
if err != nil {
return nil, fmt.Errorf(
"cannot extract network CIDR information from the reference '%s': %s",
ref, err.Error())
}
if _, _, err = net.ParseCIDR(cidr); err != nil {
return nil, fmt.Errorf("CIDR format not matched")
}
newNet := NewNetwork(m[3], cidr, true, "", nil)
newNet.Ref = ref
return newNet, nil
}
const dnsLabelFormat = "[a-z0-9]+(([a-z0-9-]*[a-z0-9]+))?"
// ValidateDomainName return an error if the domain name does not conform to standards.
// The domain name may be in Unicode format (internationalized domain name)
func ValidateDomainName(name string) error {
domainRegexpTemplate := fmt.Sprintf("^(?i)%s(\\.%s)*\\.?$", dnsLabelFormat, dnsLabelFormat)
domainRegexp := regexp.MustCompile(domainRegexpTemplate)
_, err := idna.ToASCII(name)
if err != nil {
return err
}
if !domainRegexp.MatchString(name) {
return fmt.Errorf("the name '%s' is not a valid domain name", name)
}
return nil
}
// ValidateSrvRecName return an error if the record's name does not conform to standards.
func ValidateSrvRecName(name string) error {
const protoLabelFormat = "[a-z0-9]+"
const errorMsgFormat = "SRV-record's name '%s' does not conform to standards"
var (
srvNamePartRegExp = regexp.MustCompile(fmt.Sprintf("^_%s", dnsLabelFormat))
srvProtoPartRegExp = regexp.MustCompile(fmt.Sprintf("^_%s", protoLabelFormat))
)
nameParts := strings.SplitN(name, ".", 3)
if len(nameParts) != 3 {
return fmt.Errorf(errorMsgFormat, name)
}
if !srvNamePartRegExp.MatchString(nameParts[0]) {
return fmt.Errorf(errorMsgFormat, name)
}
if !srvProtoPartRegExp.MatchString(nameParts[1]) {
return fmt.Errorf(errorMsgFormat, name)
}
if err := ValidateDomainName(nameParts[2]); err != nil {
return err
}
return nil
}
func CheckIntRange(name string, value int, min int, max int) error {
if value < min || value > max {
return fmt.Errorf("'%s' must be integer and must be in the range from 0 to 65535 inclusively", name)
}
return nil
}
func ValidateMultiValue(v string) ([]string, bool) {
res := strings.Split(v, ",")
if len(res) > 1 {
return res, true
} else {
return nil, false
}
}