forked from infobloxopen/infoblox-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_manager_a-record.go
166 lines (149 loc) · 4.14 KB
/
object_manager_a-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
package ibclient
import (
"fmt"
"net"
"strings"
)
func (objMgr *ObjectManager) CreateARecord(
netView string,
dnsView string,
name string,
cidr string,
ipAddr string,
ttl uint32,
useTTL bool,
comment string,
eas EA) (*RecordA, error) {
cleanName := strings.TrimSpace(name)
if cleanName == "" || cleanName != name {
return nil, fmt.Errorf(
"'name' argument is expected to be non-empty and it must NOT contain leading/trailing spaces")
}
recordA := NewRecordA(dnsView, "", name, "", ttl, useTTL, comment, eas, "")
if ipAddr == "" {
if cidr == "" {
return nil, fmt.Errorf("CIDR must not be empty")
}
ip, _, err := net.ParseCIDR(cidr)
if err != nil {
return nil, fmt.Errorf("cannot parse CIDR value: %s", err.Error())
}
if ip.To4() == nil {
return nil, fmt.Errorf("CIDR value must be an IPv4 CIDR, not an IPv6 one")
}
if netView == "" {
recordA.Ipv4Addr = fmt.Sprintf("func:nextavailableip:%s", cidr)
} else {
recordA.Ipv4Addr = fmt.Sprintf("func:nextavailableip:%s,%s", cidr, netView)
}
} else {
ip := net.ParseIP(ipAddr)
if ip == nil {
return nil, fmt.Errorf("'IP address for the record is not valid")
}
if ip.To4() == nil {
return nil, fmt.Errorf("IP address must be an IPv4 address, not an IPv6 one")
}
recordA.Ipv4Addr = ipAddr
}
ref, err := objMgr.connector.CreateObject(recordA)
if err != nil {
return nil, err
}
newRec, err := objMgr.GetARecordByRef(ref)
if err != nil {
return nil, err
}
return newRec, nil
}
func (objMgr *ObjectManager) UpdateARecord(
ref string,
name string,
ipAddr string,
cidr string,
netView string,
ttl uint32,
useTTL bool,
comment string,
eas EA) (*RecordA, error) {
cleanName := strings.ToLower(strings.TrimSpace(name))
if cleanName == "" || cleanName != name {
return nil, fmt.Errorf(
"'name' argument is expected to be non-empty and it must NOT contain leading/trailing spaces")
}
rec, err := objMgr.GetARecordByRef(ref)
if err != nil {
return nil, err
}
newIpAddr := rec.Ipv4Addr
if ipAddr == "" {
if cidr != "" {
ip, _, err := net.ParseCIDR(cidr)
if err != nil {
return nil, fmt.Errorf("cannot parse CIDR value: %s", err.Error())
}
if ip.To4() == nil {
return nil, fmt.Errorf("CIDR value must be an IPv4 CIDR, not an IPv6 one")
}
if netView == "" {
newIpAddr = fmt.Sprintf("func:nextavailableip:%s", cidr)
} else {
newIpAddr = fmt.Sprintf("func:nextavailableip:%s,%s", cidr, netView)
}
}
// else: leaving ipv4addr field untouched
} else {
ip := net.ParseIP(ipAddr)
if ip == nil {
return nil, fmt.Errorf("'IP address for the record is not valid")
}
if ip.To4() == nil {
return nil, fmt.Errorf("IP address must be an IPv4 address, not an IPv6 one")
}
newIpAddr = ipAddr
}
rec = NewRecordA(
"", "", name, newIpAddr, ttl, useTTL, comment, eas, ref)
ref, err = objMgr.connector.UpdateObject(rec, ref)
if err != nil {
return nil, err
}
rec.Ref = ref
rec, err = objMgr.GetARecordByRef(ref)
if err != nil {
return nil, err
}
return rec, nil
}
func (objMgr *ObjectManager) GetARecord(dnsview string, recordName string, ipAddr string) (*RecordA, error) {
var res []RecordA
recordA := NewEmptyRecordA()
if dnsview == "" || recordName == "" || ipAddr == "" {
return nil, fmt.Errorf("DNS view, IPv4 address and record name of the record are required to retreive a unique A record")
}
sf := map[string]string{
"view": dnsview,
"name": recordName,
"ipv4addr": ipAddr,
}
queryParams := NewQueryParams(false, sf)
err := objMgr.connector.GetObject(recordA, "", queryParams, &res)
if err != nil {
return nil, err
} else if res == nil || len(res) == 0 {
return nil, NewNotFoundError(
fmt.Sprintf(
"A record with name '%s' and IPv4 address '%s' in DNS view '%s' is not found",
recordName, ipAddr, dnsview))
}
return &res[0], nil
}
func (objMgr *ObjectManager) GetARecordByRef(ref string) (*RecordA, error) {
recordA := NewEmptyRecordA()
err := objMgr.connector.GetObject(
recordA, ref, NewQueryParams(false, nil), &recordA)
return recordA, err
}
func (objMgr *ObjectManager) DeleteARecord(ref string) (string, error) {
return objMgr.connector.DeleteObject(ref)
}