forked from namecheap/go-namecheap-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost.go
115 lines (107 loc) · 2.85 KB
/
host.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
package namecheap
import (
"bytes"
"fmt"
"strconv"
"strings"
)
const (
minTTL int = 60
maxTTL int = 60000
)
var (
allowedRecordTypes = []string{
"A", "AAAA", "ALIAS", "CAA", "CNAME", "MX", "MXE", "TXT", "URL", "URL301", "FRAME",
}
)
func (c *Client) SetHosts(domain string, records []Record) ([]Record, error) {
var ret RecordsCreateResult
var domainSplit = strings.Split(domain, ".")
if len(domainSplit) != 2 {
return nil, fmt.Errorf("Domain %q does not contain SLD and TLD", domainSplit)
}
var numberOfRecords = len(records)
params := map[string]string{
"Command": "namecheap.domains.dns.setHosts",
"SLD": domainSplit[0],
"TLD": domainSplit[1],
}
itr := 0
for itr < numberOfRecords {
var sNumb = strconv.Itoa(itr + 1)
params["HostName"+sNumb] = records[itr].Name
recordType := records[itr].RecordType
if !CheckRecordType(recordType) {
return nil, fmt.Errorf("Invalid record type, allowed types=%q", strings.Join(allowedRecordTypes, ", "))
}
params["RecordType"+sNumb] = recordType
params["Address"+sNumb] = records[itr].Address
params["MXPref"+sNumb] = strconv.Itoa(records[itr].MXPref)
if records[itr].TTL < minTTL || records[itr].TTL > maxTTL {
return nil, fmt.Errorf("Invalid ttl value, min=%d, max=%d", minTTL, maxTTL)
}
params["TTL"+sNumb] = strconv.Itoa(records[itr].TTL)
itr += 1
}
req, err := c.NewRequest(params)
if err != nil {
return nil, err
}
resp, err := c.Http.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
err = c.decode(resp.Body, &ret)
if err != nil {
return nil, err
}
if ret.CommandResponse.DomainDNSSetHostsResult.IsSuccess == false {
var errorBuf bytes.Buffer
for _, responseError := range ret.Errors {
errorBuf.WriteString("Number: ")
errorBuf.WriteString(responseError.Number)
errorBuf.WriteString(" Message: ")
errorBuf.WriteString(responseError.Message)
errorBuf.WriteString("\n")
}
return nil, fmt.Errorf(errorBuf.String())
}
newRecords, err := c.GetHosts(domain)
if err != nil {
return nil, err
}
return newRecords, nil
}
// GetRecords retrieves all the records for the given domain.
func (c *Client) GetHosts(domain string) ([]Record, error) {
var recordsResponse RecordsResponse
var domainSplit = strings.Split(domain, ".")
params := map[string]string{
"Command": "namecheap.domains.dns.getHosts",
"SLD": domainSplit[0],
"TLD": domainSplit[1],
}
req, err := c.NewRequest(params)
if err != nil {
return nil, err
}
resp, err := c.Http.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
err = c.decode(resp.Body, &recordsResponse)
if err != nil {
return nil, err
}
return recordsResponse.CommandResponse.Records, nil
}
func CheckRecordType(recordType string) bool {
for _, legalRecordType := range allowedRecordTypes {
if recordType == legalRecordType {
return true
}
}
return false
}