-
Notifications
You must be signed in to change notification settings - Fork 88
/
object_manager_ptr-record.go
187 lines (175 loc) · 5.24 KB
/
object_manager_ptr-record.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
package ibclient
import (
"fmt"
"github.com/infobloxopen/infoblox-go-client/v2/utils"
"net"
"regexp"
"strings"
)
// TODO check if the respective zone exists before creation of the record
func (objMgr *ObjectManager) CreatePTRRecord(
networkView string,
dnsView string,
ptrdname string,
recordName string,
cidr string,
ipAddr string,
useTtl bool,
ttl uint32,
comment string,
eas EA) (*RecordPTR, error) {
if ptrdname == "" {
return nil, fmt.Errorf("ptrdname is a required field to create a PTR record")
}
recordPTR := NewRecordPTR(dnsView, ptrdname, useTtl, ttl, comment, eas)
if recordName != "" {
recordPTR.Name = &recordName
} else if ipAddr == "" && cidr != "" {
if networkView == "" {
networkView = "default"
}
ipAddress, net, err := net.ParseCIDR(cidr)
if err != nil {
return nil, err
}
if ipAddress.To4() != nil {
if net.String() != cidr {
return nil, fmt.Errorf("%s is an invalid CIDR. Note: leading zeros should be removed if exists", cidr)
}
recordPTR.Ipv4Addr = utils.StringPtr(fmt.Sprintf("func:nextavailableip:%s,%s", cidr, networkView))
} else {
recordPTR.Ipv6Addr = utils.StringPtr(fmt.Sprintf("func:nextavailableip:%s,%s", cidr, networkView))
}
} else if ipAddr != "" {
ipAddress := net.ParseIP(ipAddr)
if ipAddress == nil {
return nil, fmt.Errorf("%s is an invalid IP address", ipAddr)
}
if ipAddress.To4() != nil {
recordPTR.Ipv4Addr = &ipAddr
} else {
recordPTR.Ipv6Addr = &ipAddr
}
} else {
return nil, fmt.Errorf("CIDR and network view are required to allocate a next available IP address\n" +
"IP address is required to create PTR record in reverse mapping zone\n" +
"record name is required to create a record in forwarrd mapping zone")
}
ref, err := objMgr.connector.CreateObject(recordPTR)
if err != nil {
return nil, err
}
recordPTR, err = objMgr.GetPTRRecordByRef(ref)
return recordPTR, err
}
func (objMgr *ObjectManager) GetPTRRecord(dnsview string, ptrdname string, recordName string, ipAddr string) (*RecordPTR, error) {
var res []RecordPTR
recordPtr := NewEmptyRecordPTR()
sf := map[string]string{
"view": dnsview,
"ptrdname": ptrdname,
}
cleanName := strings.TrimSpace(recordName)
if ipAddr != "" {
ipAddress := net.ParseIP(ipAddr)
if ipAddress == nil {
return nil, fmt.Errorf("%s is an invalid IP address", ipAddr)
}
if ipAddress.To4() != nil {
sf["ipv4addr"] = ipAddr
} else {
sf["ipv6addr"] = ipAddr
}
} else if cleanName != "" {
sf["name"] = cleanName
} else {
return nil, fmt.Errorf("record name or IP Address of the record has to be passed to get a unique record")
}
queryParams := NewQueryParams(false, sf)
err := objMgr.connector.GetObject(recordPtr, "", queryParams, &res)
if err != nil {
return nil, err
} else if res == nil || len(res) == 0 {
return nil, NewNotFoundError(
fmt.Sprintf(
"PTR record with name/IP '%v' and ptrdname '%s' in DNS view '%s' is not found",
[]string{recordName, ipAddr}, ptrdname, dnsview))
}
return &res[0], nil
}
func (objMgr *ObjectManager) GetPTRRecordByRef(ref string) (*RecordPTR, error) {
recordPTR := NewEmptyRecordPTR()
err := objMgr.connector.GetObject(
recordPTR, ref, NewQueryParams(false, nil), &recordPTR)
return recordPTR, err
}
func (objMgr *ObjectManager) DeletePTRRecord(ref string) (string, error) {
return objMgr.connector.DeleteObject(ref)
}
func (objMgr *ObjectManager) UpdatePTRRecord(
ref string,
netview string,
ptrdname string,
name string,
cidr string,
ipAddr string,
useTtl bool,
ttl uint32,
comment string,
setEas EA) (*RecordPTR, error) {
recordPTR := NewRecordPTR("", ptrdname, useTtl, ttl, comment, setEas)
recordPTR.Ref = ref
recordPTR.Name = &name
isIPv6, _ := regexp.MatchString(`^record:ptr/.+.ip6.arpa/.+`, ref)
if name == "" {
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")
}
recordPTR.Ipv6Addr = utils.StringPtr(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")
}
recordPTR.Ipv4Addr = utils.StringPtr(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")
}
recordPTR.Ipv6Addr = &ipAddr
} else {
if ipAddress.To4() == nil {
return nil, fmt.Errorf("IP address must be an IPv4 address, not an IPv6 one")
}
recordPTR.Ipv4Addr = &ipAddr
}
}
}
if recordPTR.Name != nil {
if *recordPTR.Name == "" {
recordPTR.Name = nil
}
}
reference, err := objMgr.connector.UpdateObject(recordPTR, ref)
if err != nil {
return nil, err
}
recordPTR, err = objMgr.GetPTRRecordByRef(reference)
return recordPTR, err
}