forked from mpdroog/transip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
domainservice.go
110 lines (94 loc) · 3.12 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
package transip
import (
"bytes"
"errors"
"fmt"
"github.com/mpdroog/transip/creds"
"github.com/mpdroog/transip/soap"
"github.com/mpdroog/transip/soap/signature"
"strconv"
)
const domainService = "DomainService"
type DomainService struct {
Creds creds.Client
}
func (c *DomainService) DomainNames() (*soap.DomainNames, error) {
rawbody, e := soap.Lookup(c.Creds, soap.Request{Service: domainService, Method: "getDomainNames", Body: `<ns1:getDomainNames/>`})
if e != nil {
return nil, e
}
domains := &soap.DomainNames{}
e = soap.Decode(rawbody, &domains)
return domains, e
}
func (c *DomainService) Domain(name string) (*soap.Domain, error) {
rawbody, e := soap.Lookup(c.Creds, soap.Request{
Service: domainService,
ExtraParams: []signature.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 := &soap.Domain{}
e = soap.Decode(rawbody, &domain)
return domain, e
}
func (c *DomainService) Domains(names []string) ([]soap.Domain, error) {
entryTemplate := `<item xsi:type="xsd:string">%s</item>`
params := []signature.KV{}
xml := ``
for idx, v := range names {
xml = xml + fmt.Sprintf(entryTemplate, v)
params = append(params, []signature.KV{
{Key: fmt.Sprintf("0[%d]", idx), Value: v},
}...)
}
rawbody, e := soap.Lookup(c.Creds, soap.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 := &soap.Domains{}
e = soap.Decode(rawbody, &domains)
return domains.Domains, e
}
func (c *DomainService) SetDNSEntries(domain string, entries []soap.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 := []signature.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, []signature.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 := soap.Lookup(c.Creds, soap.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
}