-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns_add.go
149 lines (128 loc) · 3.25 KB
/
dns_add.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
package lbapi
import (
"errors"
"fmt"
"net/url"
)
// AddARecord adds A or AAAA records.
func (c *Client) AddARecord(domain, address, host string, ttl int64, six bool) error {
if six {
return c.addRecord(APIDNSAddIPv6, domain, address, host, ttl)
}
return c.addRecord(APIDNSAddIPv4, domain, address, host, ttl)
}
// AddCNAME does exactly that.
func (c *Client) AddCNAME(domain, value, host string, ttl int64) error {
return c.addRecord(APIDNSAddCNAME, domain, value, host, ttl)
}
// AddMX adds MX records for mail servers.
func (c *Client) AddMX(domain, value, host string, ttl int64, priority uint16) error {
return c.addRecordPri(APIDNSAddMX, domain, value, host, ttl, priority)
}
// AddNS adds name server records.
func (c *Client) AddNS(domain, value, host string, ttl int64, priority uint16) error {
return c.addRecord(APIDNSAddNS, domain, value, host, ttl)
}
// AddTXT adds TXT records.
func (c *Client) AddTXT(domain, value, host string, ttl int64, priority uint16) error {
return c.addRecord(APIDNSAddTXT, domain, value, host, ttl)
}
// AddSRV adds SRV records.
func (c *Client) AddSRV(domain, value, host string, ttl int64, priority, port, weight uint16) error {
var err error
u, err := url.Parse(c.URL)
if err != nil {
return err
}
u.Path = APIDNSAddSRV
q := u.Query()
q.Set("auth-userid", c.ID)
q.Set("api-key", c.Key)
q.Set("domain-name", domain)
q.Set("value", value)
q.Set("host", host)
if ttl < 7200 {
ttl = 7200
}
q.Set("ttl", fmt.Sprintf("%d", ttl))
if priority > 0 {
q.Set("priority", fmt.Sprintf("%d", priority))
}
if port > 0 {
q.Set("port", fmt.Sprintf("%d", port))
}
if weight > 0 {
q.Set("weight", fmt.Sprintf("%d", weight))
}
u.RawQuery = q.Encode()
res, err := PostResponse(c.Client, u.String())
if err != nil {
return err
}
list := *res
if list["status"] != "Success" {
return errors.New("couldn't add SRV record - check that FQDN is correct")
}
return nil
}
func (c *Client) addRecord(call, domain, address, host string, ttl int64) error {
var err error
u, err := url.Parse(c.URL)
if err != nil {
return err
}
u.Path = call
q := u.Query()
q.Set("auth-userid", c.ID)
q.Set("api-key", c.Key)
q.Set("domain-name", domain)
q.Set("value", address)
if host != "" {
q.Set("host", host)
}
if ttl == 0 || ttl < 7200 {
ttl = 7200
}
q.Set("ttl", fmt.Sprintf("%d", ttl))
u.RawQuery = q.Encode()
res, err := PostResponse(c.Client, u.String())
if err != nil {
return err
}
list := *res
if list["status"] == "ERROR" {
return errors.New((fmt.Sprintf("%v", list["message"])))
}
return nil
}
func (c *Client) addRecordPri(call, domain, address, host string, ttl int64, priority uint16) error {
var err error
u, err := url.Parse(c.URL)
if err != nil {
return err
}
u.Path = call
q := u.Query()
q.Set("auth-userid", c.ID)
q.Set("api-key", c.Key)
q.Set("domain-name", domain)
q.Set("value", address)
if host != "" {
q.Set("host", host)
}
if ttl == 0 || ttl < 7200 {
ttl = 7200
}
q.Set("ttl", fmt.Sprintf("%d", ttl))
q.Set("priority", fmt.Sprintf("%d", priority))
u.RawQuery = q.Encode()
res, err := PostResponse(c.Client, u.String())
if err != nil {
return err
}
list := *res
if list["status"] == "ERROR" {
return errors.New((fmt.Sprintf("%v", list["message"])))
}
return nil
}