-
Notifications
You must be signed in to change notification settings - Fork 2
/
domainservice.go
152 lines (130 loc) · 4.21 KB
/
domainservice.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
package transip
import (
"bytes"
"errors"
"fmt"
"strconv"
)
const domainService = "DomainService"
type DomainService struct {
Creds Client
}
func (c *DomainService) DomainNames() ([]string, error) {
rawbody, e := lookup(c.Creds, request{Service: domainService, Method: "getDomainNames", Body: `<ns1:getDomainNames/>`})
if e != nil {
return nil, e
}
domains := &domainNames{}
e = decode(rawbody, &domains)
return domains.Item, e
}
func (c *DomainService) Domain(name string) (*Domain, error) {
rawbody, e := lookup(c.Creds, request{
Service: domainService,
ExtraParams: []kV{
{Key: "0", Value: name},
},
Method: "getInfo",
Body: fmt.Sprintf(`<ns1:getInfo><domainName xsi:type="xsd:string">%s</domainName></ns1:getInfo>`, name),
})
if e != nil {
return nil, e
}
domain := &Domain{}
e = decode(rawbody, &domain)
return domain, e
}
func (c *DomainService) Domains(names []string) ([]Domain, error) {
entryTemplate := `<item xsi:type="xsd:string">%s</item>`
params := []kV{}
xml := ``
for idx, v := range names {
xml = xml + fmt.Sprintf(entryTemplate, v)
params = append(params, []kV{
{Key: fmt.Sprintf("0[%d]", idx), Value: v},
}...)
}
rawbody, e := lookup(c.Creds, request{
Service: domainService,
ExtraParams: params,
Method: "batchGetInfo",
Body: fmt.Sprintf(`<ns1:batchGetInfo><domainNames SOAP-ENC:arrayType="xsd:string[%d]" xsi:type="ns1:ArrayOfString">%s</domainNames></ns1:batchGetInfo>`, len(names), xml),
})
if e != nil {
return nil, e
}
domains := &Domains{}
e = decode(rawbody, &domains)
return domains.Domains, e
}
func (c *DomainService) SetDNSEntries(domain string, entries []DomainDNSentry) error {
entryTemplate := `<item xsi:type="ns1:DnsEntry"><name xsi:type="xsd:string">%s</name><expire xsi:type="xsd:int">%d</expire><type xsi:type="xsd:string">%s</type><content xsi:type="xsd:string">%s</content></item>`
params := []kV{
{Key: "0", Value: domain},
}
xml := ``
for idx, entry := range entries {
xml = xml + fmt.Sprintf(entryTemplate, entry.Name, entry.Expire, entry.Type, entry.Content)
params = append(params, []kV{
{fmt.Sprintf("1[%d][name]", idx), entry.Name},
{fmt.Sprintf("1[%d][expire]", idx), strconv.Itoa(entry.Expire)},
{fmt.Sprintf("1[%d][type]", idx), entry.Type},
{fmt.Sprintf("1[%d][content]", idx), entry.Content},
}...)
}
rawbody, e := lookup(c.Creds, request{
Service: domainService,
ExtraParams: params,
Method: "setDnsEntries",
Body: fmt.Sprintf(
`<ns1:setDnsEntries><domainName xsi:type="xsd:string">%s</domainName><dnsEntries SOAP-ENC:arrayType="ns1:DnsEntry[%d]" xsi:type="ns1:ArrayOfDnsEntry">%s</dnsEntries></ns1:setDnsEntries>`,
domain, len(entries), xml,
),
})
if e != nil {
return e
}
if !bytes.Contains(rawbody, []byte(`<ns1:setDnsEntriesResponse/>`)) {
return errors.New("Unexpected XML-reply: " + string(rawbody))
}
return nil
}
func (c *DomainService) CheckAvailability(name string) (string, error) {
rawbody, e := lookup(c.Creds, request{
Service: domainService,
ExtraParams: []kV{
{Key: "0", Value: name},
},
Method: "checkAvailability",
Body: fmt.Sprintf(`<ns1:checkAvailability><domainName xsi:type="xsd:string">%s</domainName></ns1:checkAvailability>`, name),
})
if e != nil {
return "", e
}
availability := &availability{}
e = decode(rawbody, &availability)
return availability.Item, e
}
func (c *DomainService) BatchCheckAvailability(names []string) ([]DomainCheckResult, error) {
entryTemplate := `<item xsi:type="xsd:string">%s</item>`
params := []kV{}
xml := ``
for idx, v := range names {
xml = xml + fmt.Sprintf(entryTemplate, v)
params = append(params, []kV{
{Key: fmt.Sprintf("0[%d]", idx), Value: v},
}...)
}
rawbody, e := lookup(c.Creds, request{
Service: domainService,
ExtraParams: params,
Method: "batchCheckAvailability",
Body: fmt.Sprintf(`<ns1:batchCheckAvailability soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><domainNames xsi:type="soap:ArrayOfstring" soapenc:arrayType="xsd:string[%d]">%s</domainNames></ns1:batchCheckAvailability>`, len(names), xml),
})
if e != nil {
return nil, e
}
dcResults := &domainCheckResults{}
e = decode(rawbody, &dcResults)
return dcResults.Results, e
}