forked from infobloxopen/infoblox-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
143 lines (114 loc) · 3.3 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
package ibclient
import (
"fmt"
"net"
"net/url"
"regexp"
)
type NotFoundError struct {
msg string
}
func (e *NotFoundError) Error() string {
return "not found"
}
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
}