forked from billputer/go-namecheap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.go
101 lines (87 loc) · 2.55 KB
/
dns.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
package namecheap
import (
"fmt"
"net/url"
"strconv"
)
const (
domainsDNSGetHosts = "namecheap.domains.dns.getHosts"
domainsDNSSetHosts = "namecheap.domains.dns.setHosts"
domainsDNSSetCustom = "namecheap.domains.dns.setCustom"
)
type DomainDNSGetHostsResult struct {
Domain string `xml:"Domain,attr"`
IsUsingOurDNS bool `xml:"IsUsingOurDNS,attr"`
Hosts []DomainDNSHost `xml:"host"`
}
type DomainDNSHost struct {
ID int `xml:"HostId,attr"`
Name string `xml:"Name,attr"`
Type string `xml:"Type,attr"`
Address string `xml:"Address,attr"`
MXPref int `xml:"MXPref,attr"`
TTL int `xml:"TTL,attr"`
}
type DomainDNSSetHostsResult struct {
Domain string `xml:"Domain,attr"`
IsSuccess bool `xml:"IsSuccess,attr"`
}
func (client *Client) DomainsDNSGetHosts(sld, tld string) (*DomainDNSGetHostsResult, error) {
requestInfo := &ApiRequest{
command: domainsDNSGetHosts,
method: "POST",
params: url.Values{},
}
requestInfo.params.Set("SLD", sld)
requestInfo.params.Set("TLD", tld)
resp, err := client.do(requestInfo)
if err != nil {
return nil, err
}
return resp.DomainDNSHosts, nil
}
func (client *Client) DomainDNSSetHosts(
sld, tld string, hosts []DomainDNSHost,
) (*DomainDNSSetHostsResult, error) {
requestInfo := &ApiRequest{
command: domainsDNSSetHosts,
method: "POST",
params: url.Values{},
}
requestInfo.params.Set("SLD", sld)
requestInfo.params.Set("TLD", tld)
for i, h := range hosts {
requestInfo.params.Set(fmt.Sprintf("HostName%v", i+1), h.Name)
requestInfo.params.Set(fmt.Sprintf("RecordType%v", i+1), h.Type)
requestInfo.params.Set(fmt.Sprintf("Address%v", i+1), h.Address)
if h.Type == "MX" {
requestInfo.params.Set(fmt.Sprintf("MXPref%v", i+1), strconv.Itoa(h.MXPref))
requestInfo.params.Set("EmailType", "MX")
}
requestInfo.params.Set(fmt.Sprintf("TTL%v", i+1), strconv.Itoa(h.TTL))
}
resp, err := client.do(requestInfo)
if err != nil {
return nil, err
}
return resp.DomainDNSSetHosts, nil
}
type DomainDNSSetCustomResult struct {
Domain string `xml:"Domain,attr"`
Update bool `xml:"Update,attr"`
}
func (client *Client) DomainDNSSetCustom(sld, tld, nameservers string) (*DomainDNSSetCustomResult, error) {
requestInfo := &ApiRequest{
command: domainsDNSSetCustom,
method: "POST",
params: url.Values{},
}
requestInfo.params.Set("SLD", sld)
requestInfo.params.Set("TLD", tld)
requestInfo.params.Set("Nameservers", nameservers)
resp, err := client.do(requestInfo)
if err != nil {
return nil, err
}
return resp.DomainDNSSetCustom, nil
}