forked from andrexus/goinwx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nameserver.go
279 lines (233 loc) · 7.43 KB
/
nameserver.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package goinwx
import (
"errors"
"time"
"fmt"
"github.com/fatih/structs"
"github.com/mitchellh/mapstructure"
)
const (
methodNameserverCheck = "nameserver.check"
methodNameserverCreate = "nameserver.create"
methodNameserverCreateRecord = "nameserver.createRecord"
methodNameserverDelete = "nameserver.delete"
methodNameserverDeleteRecord = "nameserver.deleteRecord"
methodNameserverInfo = "nameserver.info"
methodNameserverList = "nameserver.list"
methodNameserverUpdate = "nameserver.update"
methodNameserverUpdateRecord = "nameserver.updateRecord"
)
type NameserverService interface {
Check(domain string, nameservers []string) (*NameserverCheckResponse, error)
Create(*NameserverCreateRequest) (int, error)
Info(domain string, domainId int) (*NamserverInfoResponse, error)
List(domain string) (*NamserverListResponse, error)
CreateRecord(*NameserverRecordRequest) (int, error)
UpdateRecord(recId int, request *NameserverRecordRequest) error
DeleteRecord(recId int) error
FindRecordById(recId int) (*NameserverRecord, *NameserverDomain, error)
}
type NameserverServiceOp struct {
client *Client
}
var _ NameserverService = &NameserverServiceOp{}
type NameserverCheckResponse struct {
Details []string
Status string
}
type NameserverRecordRequest struct {
RoId int `structs:"roId,omitempty"`
Domain string `structs:"domain,omitempty"`
Type string `structs:"type"`
Content string `structs:"content"`
Name string `structs:"name,omitempty"`
Ttl int `structs:"ttl,omitempty"`
Priority int `structs:"prio,omitempty"`
UrlRedirectType string `structs:"urlRedirectType,omitempty"`
UrlRedirectTitle string `structs:"urlRedirectTitle,omitempty"`
UrlRedirectDescription string `structs:"urlRedirectDescription,omitempty"`
UrlRedirectFavIcon string `structs:"urlRedirectFavIcon,omitempty"`
UrlRedirectKeywords string `structs:"urlRedirectKeywords,omitempty"`
}
type NameserverCreateRequest struct {
Domain string `structs:"domain"`
Type string `structs:"type"`
Nameservers []string `structs:"ns,omitempty"`
MasterIp string `structs:"masterIp,omitempty"`
Web string `structs:"web,omitempty"`
Mail string `structs:"mail,omitempty"`
SoaEmail string `structs:"soaEmail,omitempty"`
UrlRedirectType string `structs:"urlRedirectType,omitempty"`
UrlRedirectTitle string `structs:"urlRedirectTitle,omitempty"`
UrlRedirectDescription string `structs:"urlRedirectDescription,omitempty"`
UrlRedirectFavIcon string `structs:"urlRedirectFavIcon,omitempty"`
UrlRedirectKeywords string `structs:"urlRedirectKeywords,omitempty"`
Testing bool `structs:"testing,omitempty"`
}
type NamserverInfoResponse struct {
RoId int
Domain string
Type string
MasterIp string
LastZoneCheck time.Time
SlaveDns interface{}
SOAserial string
Count int
Records []NameserverRecord `mapstructure:"record"`
}
type NameserverRecord struct {
Id int
Name string
Type string
Content string
Ttl int
Prio int
UrlRedirectType string
UrlRedirectTitle string
UrlRedirectDescription string
UrlRedirectKeywords string
UrlRedirectFavIcon string
}
type NamserverListResponse struct {
Count int
Domains []NameserverDomain `mapstructure:"domains"`
}
type NameserverDomain struct {
RoId int `mapstructure:"roId"`
Domain string `mapstructure:"domain"`
Type string `mapstructure:"type"`
MasterIp string `mapstructure:"masterIp"`
Mail string `mapstructure:"mail"`
Web string `mapstructure:"web"`
Url string `mapstructure:"url"`
Ipv4 string `mapstructure:"ipv4"`
Ipv6 string `mapstructure:"ipv6"`
}
func (s *NameserverServiceOp) Check(domain string, nameservers []string) (*NameserverCheckResponse, error) {
req := s.client.NewRequest(methodNameserverCheck, map[string]interface{}{
"domain": domain,
"ns": nameservers,
})
resp, err := s.client.Do(*req)
if err != nil {
return nil, err
}
var result NameserverCheckResponse
err = mapstructure.Decode(*resp, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (s *NameserverServiceOp) Info(domain string, domainId int) (*NamserverInfoResponse, error) {
var requestMap = make(map[string]interface{})
if domain != "" {
requestMap["domain"] = domain
}
if domainId != 0 {
requestMap["roId"] = domainId
}
req := s.client.NewRequest(methodNameserverInfo, requestMap)
resp, err := s.client.Do(*req)
if err != nil {
return nil, err
}
var result NamserverInfoResponse
err = mapstructure.Decode(*resp, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (s *NameserverServiceOp) List(domain string) (*NamserverListResponse, error) {
requestMap := map[string]interface{}{
"domain": "*",
"wide": 2,
}
if domain != "" {
requestMap["domain"] = domain
}
req := s.client.NewRequest(methodNameserverList, requestMap)
resp, err := s.client.Do(*req)
if err != nil {
return nil, err
}
var result NamserverListResponse
err = mapstructure.Decode(*resp, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (s *NameserverServiceOp) Create(request *NameserverCreateRequest) (int, error) {
req := s.client.NewRequest(methodNameserverCreate, structs.Map(request))
resp, err := s.client.Do(*req)
if err != nil {
return 0, err
}
var result map[string]int
err = mapstructure.Decode(*resp, &result)
if err != nil {
return 0, err
}
return result["roId"], nil
}
func (s *NameserverServiceOp) CreateRecord(request *NameserverRecordRequest) (int, error) {
req := s.client.NewRequest(methodNameserverCreateRecord, structs.Map(request))
//fmt.Println("Args", req.Args)
resp, err := s.client.Do(*req)
if err != nil {
return 0, err
}
var result map[string]int
err = mapstructure.Decode(*resp, &result)
if err != nil {
return 0, err
}
return result["id"], nil
}
func (s *NameserverServiceOp) UpdateRecord(recId int, request *NameserverRecordRequest) error {
if request == nil {
return errors.New("Request can't be nil")
}
requestMap := structs.Map(request)
requestMap["id"] = recId
req := s.client.NewRequest(methodNameserverUpdateRecord, requestMap)
_, err := s.client.Do(*req)
if err != nil {
return err
}
return nil
}
func (s *NameserverServiceOp) DeleteRecord(recId int) error {
req := s.client.NewRequest(methodNameserverDeleteRecord, map[string]interface{}{
"id": recId,
})
_, err := s.client.Do(*req)
if err != nil {
return err
}
return nil
}
func (s *NameserverServiceOp) FindRecordById(recId int) (*NameserverRecord, *NameserverDomain, error) {
var domain *NameserverDomain
var rec *NameserverRecord
listResp, err := s.client.Nameservers.List("")
if err != nil {
return nil, nil, err
}
for _, domainItem := range listResp.Domains {
resp, err := s.client.Nameservers.Info("", domainItem.RoId)
if err != nil {
return nil, nil, err
}
for _, record := range resp.Records {
if record.Id == recId {
rec = &record
domain = &domainItem
return rec, domain, nil
}
}
}
return nil, nil, errors.New(fmt.Sprintf("Couldn't find INWX Record for id %d", recId))
}