-
Notifications
You must be signed in to change notification settings - Fork 88
/
object_manager_mx_record.go
139 lines (113 loc) · 2.98 KB
/
object_manager_mx_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
package ibclient
import (
"fmt"
)
func (objMgr *ObjectManager) CreateMXRecord(
dnsView string,
fqdn string,
mx string,
preference uint32,
ttl uint32,
useTtl bool,
comment string,
eas EA) (*RecordMX, error) {
if dnsView == "" {
dnsView = "default"
}
if fqdn == "" || mx == "" {
return nil, fmt.Errorf("'fqdn' and 'mail_exchanger' fields must not be empty")
}
if preference < 0 || preference > 65535 {
return nil, fmt.Errorf("'preference' is not in range 0 to 65535")
}
recordMx := NewRecordMX(RecordMX{
View: &dnsView,
Name: &fqdn,
MailExchanger: &mx,
Preference: &preference,
Ttl: &ttl,
UseTtl: &useTtl,
Comment: &comment,
Ea: eas,
})
ref, err := objMgr.connector.CreateObject(recordMx)
if err != nil {
return nil, err
}
recordMx.Ref = ref
return recordMx, err
}
func (objMgr *ObjectManager) GetMXRecordByRef(ref string) (*RecordMX, error) {
recordMX := NewEmptyRecordMX()
err := objMgr.connector.GetObject(recordMX, ref, NewQueryParams(false, nil), &recordMX)
return recordMX, err
}
func (objMgr *ObjectManager) GetMXRecord(dnsView string, fqdn string, mx string, preference uint32) (*RecordMX, error) {
if dnsView == "" || fqdn == "" {
return nil, fmt.Errorf("'DNS view' and 'fqdn' are required to retrieve a unique mx record")
}
var res []RecordMX
recordMX := NewEmptyRecordMX()
sf := map[string]string{
"view": dnsView,
"name": fqdn,
"mail_exchanger": mx,
"preference": fmt.Sprintf("%d", preference),
}
queryParams := NewQueryParams(false, sf)
err := objMgr.connector.GetObject(recordMX, "", queryParams, &res)
if err != nil {
return nil, err
}
if res == nil || len(res) == 0 {
return nil, NewNotFoundError(
fmt.Sprintf(
"MX record with name '%s' and MX '%s' in DNS view '%s' is not found",
fqdn, mx, dnsView))
}
return &res[0], nil
}
func (objMgr *ObjectManager) UpdateMXRecord(
ref string,
dnsView string,
fqdn string,
mx string,
preference uint32,
ttl uint32,
useTtl bool,
comment string,
eas EA) (*RecordMX, error) {
res, err := objMgr.GetMXRecordByRef(ref)
if err != nil {
return nil, err
}
if dnsView != *res.View {
return nil, fmt.Errorf("changing 'dns_view' field after object creation is not allowed")
}
if preference < 0 || preference > 65535 {
return nil, fmt.Errorf("'preference' is not in range 0 to 65535")
}
if mx == "" {
return nil, fmt.Errorf("'mail_exchanger' field must not be empty")
}
recordMx := NewRecordMX(RecordMX{
View: &dnsView,
Name: &fqdn,
MailExchanger: &mx,
Preference: &preference,
Ttl: &ttl,
UseTtl: &useTtl,
Comment: &comment,
Ea: eas,
})
recordMx.Ref = ref
nw_ref, err := objMgr.connector.UpdateObject(recordMx, ref)
if err != nil {
return nil, err
}
recordMx.Ref = nw_ref
return recordMx, err
}
func (objMgr *ObjectManager) DeleteMXRecord(ref string) (string, error) {
return objMgr.connector.DeleteObject(ref)
}