-
Notifications
You must be signed in to change notification settings - Fork 2
/
client_nameservers.go
126 lines (108 loc) · 2.98 KB
/
client_nameservers.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
package ovh
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"golang.org/x/net/context"
)
// NameServers - OVH Domains Name Servers API Client.
type NameServers struct {
// Options - Client options.
*Options
}
// List - Gets domain nameservers info by domain name.
func (nameservers *NameServers) List(ctx context.Context, domain string) (result []*NameServer, err error) {
response, err := httpDo(ctx, nameservers.Options, "GET", apiURL("/domain/%s/nameServer", domain), nil)
if err != nil {
return
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, unexpectedStatusError(response)
}
var r []int64
err = json.NewDecoder(response.Body).Decode(&r)
if err != nil {
return
}
for _, id := range r {
ns, err := nameservers.Details(ctx, domain, fmt.Sprintf("%d", id))
if err != nil {
return nil, err
}
result = append(result, ns)
}
return
}
// Details - Gets domain nameserver info by domain name and name server ID.
func (nameservers *NameServers) Details(ctx context.Context, domain, id string) (result *NameServer, err error) {
response, err := httpDo(ctx, nameservers.Options, "GET", apiURL("/domain/%s/nameServer/%s", domain, id), nil)
if err != nil {
return
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, unexpectedStatusError(response)
}
r := new(struct {
ID int64 `json:"id,omitempty"`
Host string `json:"host,omitempty"`
ToDelete bool `json:"toDelete,omitempty"`
IsUsed bool `json:"isUsed,omitempty"`
})
err = json.NewDecoder(response.Body).Decode(&r)
if err != nil {
return
}
return &NameServer{
ID: fmt.Sprintf("%d", r.ID),
Host: r.Host,
ToDelete: r.ToDelete,
IsUsed: r.IsUsed,
}, nil
}
// Delete - Deletes domain nameserver by domain name and name server ID.
func (nameservers *NameServers) Delete(ctx context.Context, domain, id string) (err error) {
response, err := httpDo(ctx, nameservers.Options, "DELETE", apiURL("/domain/%s/nameServer/%s", domain, id), nil)
if err != nil {
return
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return unexpectedStatusError(response)
}
return
}
// Insert - Inserts domain nameservers to domain.
func (nameservers *NameServers) Insert(ctx context.Context, domain string, nameserver ...string) (err error) {
if len(nameserver) == 0 {
return
}
buffer := new(bytes.Buffer)
req := new(struct {
NameServer []*struct {
Host string `json:"host,omitempty"`
} `json:"nameServer,omitempty"`
})
for _, ns := range nameserver {
req.NameServer = append(req.NameServer, &struct {
Host string `json:"host,omitempty"`
}{
Host: ns,
})
}
err = json.NewEncoder(buffer).Encode(req)
if err != nil {
return
}
response, err := httpDo(ctx, nameservers.Options, "POST", apiURL("/domain/%s/nameServer", domain), buffer)
if err != nil {
return
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return unexpectedStatusError(response)
}
return
}