-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomain.go
executable file
·182 lines (135 loc) · 4.46 KB
/
domain.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
package godaddyclient
import (
"errors"
"fmt"
"net"
"net/http"
)
type Domain struct {
api IGodaddyAPI
}
// IDomain is the GoaddyClient interface.
type IDomain interface {
CreateSubDomain(domainName string, subDomainName string, forwardIPAddr string) Result
UpdateSubDomain(domainName string, subDomainName string, forwardIPAddr string) Result
DeleteSubDomain(domainName string, subDomainName string) Result
UpdateDomain(domainName string, forwardIPAddr string) Result
DomainAvailable(domainName string) Result
// CreateDNSRecord creates a new record.
// domainName: top-level domain name,
// recordType: type of DNS record,
// name: name of the redord,
// value: the value of the record,
// ttl: the amount of time before the record is udpated (refrered)
CreateDNSRecord(domainName string, recordType dnsRecordType, name string, value string, ttl int) Result
UpdateDNSRecord(domainName string, recordType dnsRecordType, name string, value string, ttl int) Result
DeleteDNSRecord(domainName string, recordType dnsRecordType, name string) Result
}
func getRecordType(rt dnsRecordType) (string, error) {
switch rt {
case a:
return "A", nil
case aaaa:
return "AAAAA", nil
case cname:
return "CName", nil
case mx:
return "MX", nil
case txt:
return "TXT", nil
case srv:
return "SRV", nil
case caa:
return "CAA", nil
case ns:
return "NS", nil
}
return "", errors.New("invalid dns record type")
}
func (d *Domain) DomainAvailable(domainName string) Result {
var res Result
url := fmt.Sprintf("/domains/available?domain=%s&checkType=fast&forTransfer=false", domainName)
res = d.api.HTTPExec(GET, url, "")
return res
}
func (d *Domain) UpdateDomain(domainName string, forwardIPAddr string) Result {
var res Result
n := net.ParseIP(forwardIPAddr)
if n == nil {
res.Message = "invalid ip address"
return res
}
subDomainName := "@"
url := fmt.Sprintf("/domains/%s/records/A/%s", domainName, subDomainName)
jsonStr := fmt.Sprintf(`[{"data": "%s","ttl": 600}]`, forwardIPAddr)
res = d.api.HTTPExec(PUT, url, jsonStr)
return res
}
func (d *Domain) UpdateSubDomain(domainName string, subDomainName string, forwardIPAddr string) Result {
var res Result
n := net.ParseIP(forwardIPAddr)
if n == nil {
res.Message = "invalid ip address"
return res
}
url := fmt.Sprintf("/domains/%s/records/A/%s", domainName, subDomainName)
jsonStr := fmt.Sprintf(`[{"data": "%s","ttl": 600}]`, forwardIPAddr)
res = d.api.HTTPExec(PUT, url, jsonStr)
return res
}
func (d *Domain) CreateSubDomain(domainName string, subDomainName string, forwardIPAddr string) Result {
var res Result
n := net.ParseIP(forwardIPAddr)
if n == nil {
res.Message = "invalid ip address"
return res
}
url := fmt.Sprintf("/domains/%s/records/A/%s", domainName, subDomainName)
jsonStr := fmt.Sprintf(`[{"data": "%s","ttl": 600}]`, forwardIPAddr)
res = d.api.HTTPExec(PUT, url, jsonStr)
return res
}
func (d *Domain) DeleteSubDomain(domainName string, subDomainName string) Result {
var res Result
url := fmt.Sprintf("/domains/%s/records/A/%s", domainName, subDomainName)
res = d.api.HTTPExec(DELETE, url, "")
return res
}
func (d *Domain) DeleteDNSRecord(domainName string, recordType dnsRecordType, name string) Result {
var res Result
rt, err := getRecordType(dnsRecordType(recordType))
if err != nil {
res.StatusCode = http.StatusBadRequest
res.Message = err.Error()
return res
}
url := fmt.Sprintf("/domains/%s/records/%v/%s", domainName, rt, name)
res = d.api.HTTPExec(DELETE, url, "")
return res
}
func (d *Domain) CreateDNSRecord(domainName string, recordType dnsRecordType, name string, value string, ttl int) Result {
var res Result
rt, err := getRecordType(dnsRecordType(recordType))
if err != nil {
res.StatusCode = http.StatusBadRequest
res.Message = err.Error()
return res
}
url := fmt.Sprintf("/domains/%s/records/%v/%s", domainName, rt, name)
jsonStr := fmt.Sprintf(`[{"data": "%s","ttl": %d}]`, value, ttl)
res = d.api.HTTPExec(PUT, url, jsonStr)
return res
}
func (d *Domain) UpdateDNSRecord(domainName string, recordType dnsRecordType, name string, value string, ttl int) Result {
var res Result
rt, err := getRecordType(dnsRecordType(recordType))
if err != nil {
res.StatusCode = http.StatusBadRequest
res.Message = err.Error()
return res
}
url := fmt.Sprintf("/domains/%s/records/%v/%s", domainName, rt, name)
jsonStr := fmt.Sprintf(`[{"data": "%s","ttl": %d}]`, value, ttl)
res = d.api.HTTPExec(PUT, url, jsonStr)
return res
}