-
Notifications
You must be signed in to change notification settings - Fork 0
/
domains.go
296 lines (242 loc) · 6.78 KB
/
domains.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package porkbungo
import (
"context"
"errors"
"fmt"
)
// Domain Pricing represents the response of the
// domain pricing API
type DomainPricing struct {
Status string `json:"status"`
Pricing map[string] struct {
Registration string `json:"registration"`
Renewal string `json:"renewal"`
Transfer string `json:"transfer"`
} `json:"pricing"`
}
// UpdateNSOptions fields
type UpdateNameServerOptions struct {
*Keys
Nameservers []string `json:"ns"`
Domain string `json:"-"`
}
// Type to represent yes or no
type BooleanType string
const (
Yes BooleanType = "yes"
No BooleanType = "no"
)
// Constants are the redirect types that can be assigned
// to a URL Forward
type ForwardType string
const (
Temporary ForwardType = "temporary"
Permanent ForwardType = "permanent"
)
// Options for creating a URL Forward
type URLForwardOptions struct {
*Keys
// [Optional] A subdomain that you would like to add email forwarding for.
SubDomain string `json:"subdomain,omitempty"`
// Destination for URL forwarding
Location string `json:"location"`
// Type of forward.
// Valid types: [permament,temporary]
Type ForwardType `json:"type"`
// Include or don't include URI path in redirection.
// Valid: [yes,no]
IncludePath BooleanType `json:"includePath"`
// Forward all subdomains of this domain
// Valid: [yes,no]
Wildcard BooleanType `json:"wildcard"`
Domain string `json:"-"`
}
// URL forward returned from porkbun API
type URLForward struct {
// Subdomain id
Id string `json:"id"`
// Name of subdomain
Subdomain string `json:"subdomain"`
// URL forwarding destination
Location string `json:"location"`
// Type of forward temporary or permanent
Type ForwardType `json:"type"`
// Whether URI path is included in redirection
IncludePath BooleanType `json:"includePath"`
//Forward all subdomains of this domain/subdomain
Wildcard BooleanType `json:"wildcard"`
}
// Domain represents a second level domain owned by the account
type Domain struct {
Domain string `json:"domain"`
Status string `json:"status"`
TLD string `json:"tld"`
CreateDate string `json:"createDate"`
ExpireDate string `json:"expireDate"`
SecurityLock string `json:"securityLock"`
WhoisPrivacy string `json:"whoisPrivacy"`
AutoRenew int `json:"autoRenew,string"`
NotLocal int `json:"notLocal"`
}
// Checks default domain pricing information for all supported TLDs.
func (c *Client) GetDomainPricing(ctx context.Context) (*DomainPricing, error) {
request := c.resty.NewRequest().SetContext(ctx)
response, err := request.SetResult(&DomainPricing{}).Get("/pricing/get")
if err != nil || response.StatusCode() != 200{
if err == nil {
e,_ := response.Error().(*APIError)
err = errors.New(e.Message)
}
return nil,err
}
if val,ok := response.Result().(*DomainPricing); ok {
return val,nil
}
return nil,errors.New("error data received is not in correct format")
}
// Update the authoritative nameservers for the specified domain
func (c *Client) UpdateNameServers(ctx context.Context, opts UpdateNameServerOptions) error {
req := c.resty.NewRequest().SetContext(ctx)
result := struct {
Status string `json:"status"`
}{}
req.SetResult(&result)
u := fmt.Sprintf("/domain/updateNs/%s", opts.Domain)
if opts.Keys == nil {
opts.Keys = c.keys
}
req.SetBody(opts)
resp,err := req.Post(u)
if err != nil || resp.StatusCode() != 200 {
if err == nil {
e,_ := resp.Error().(*APIError)
err = errors.New(e.Message)
}
return err
}
return nil
}
// Get Authoritative nameservers listed at the registry for the specified domain
func (c *Client) GetNameServers(ctx context.Context, domain string, keys *Keys) ([]string,error) {
result := struct {
Status string `json:"status"`
NS []string `json:"ns"`
}{}
req := c.resty.NewRequest().SetContext(ctx).SetResult(&result)
u := fmt.Sprintf("/domain/getNs/%s", domain)
if keys == nil {
keys = c.keys
}
req.SetBody(keys)
resp,err := req.Post(u)
if err != nil || resp.StatusCode() != 200 {
if err == nil {
e,_ := resp.Error().(*APIError)
err = errors.New(e.Message)
}
return nil,err
}
return result.NS,nil
}
// Create a URL forward for the specified domain
func (c *Client) CreateURLForward(ctx context.Context, opts URLForwardOptions) error {
req := c.resty.NewRequest().SetContext(ctx)
u := fmt.Sprintf("/domain/addUrlForward/%s", opts.Domain)
if opts.Keys == nil {
opts.Keys = c.keys
}
req.SetBody(opts)
resp,err := req.Post(u)
if err != nil || resp.StatusCode() != 200 {
if err == nil {
e,_ := resp.Error().(*APIError)
err = errors.New(e.Message)
}
return err
}
return nil
}
// Get URL forwarding for the specified domain
func (c *Client) GetUrlForwards(ctx context.Context, domain string, keys *Keys) ([]URLForward,error) {
result := struct {
Status string `json:"status"`
Forwards []URLForward `json:"forwards"`
}{}
req := c.resty.NewRequest().SetContext(ctx).SetResult(&result)
u := fmt.Sprintf("/domain/getUrlForwarding/%s", domain)
if keys == nil {
keys = c.keys
}
req.SetBody(keys)
resp,err := req.Post(u)
if err != nil || resp.StatusCode() != 200 {
if err == nil {
e,_ := resp.Error().(*APIError)
err = errors.New(e.Message)
}
return nil,err
}
return result.Forwards,nil
}
// Delete the specified URL forwarding for a domain
func (c *Client) DeleteURLForward(ctx context.Context, domain string, forwardId string, keys *Keys) error {
req := c.resty.NewRequest().SetContext(ctx)
u := fmt.Sprintf("/domain/deleteUrlForward/%s/%s", domain, forwardId)
if keys == nil {
keys = c.keys
}
req.SetBody(keys)
resp,err := req.Post(u)
if err != nil || resp.StatusCode() != 200 {
if err == nil {
e,_ := resp.Error().(*APIError)
err = errors.New(e.Message)
}
return err
}
return nil
}
// Get all domain names for the current account
func (c *Client) GetAllDomains(ctx context.Context, keys *Keys) ([]Domain,error) {
respResult := struct {
Status string `json:"status"`
Domains []Domain `json:"domains"`
}{}
body := struct {
Keys
Start int `json:"start"`
}{}
if keys == nil {
body.Keys = *c.keys
} else {
body.Keys = *keys
}
body.Start = 0
u := "/domain/listAll"
req := c.resty.NewRequest().SetContext(ctx).SetResult(&respResult)
result := make([]Domain,0)
req.SetBody(body)
resp,err := req.Post(u)
// TODO: Turn this into reusable function
if err != nil || resp.StatusCode() != 200 {
if err == nil {
e,_ := resp.Error().(*APIError)
err = errors.New(e.Message)
}
return result,err
}
for len(respResult.Domains) > 0 {
result = append(result, respResult.Domains...)
body.Start += 1000
req.SetBody(body)
resp,err = req.Post(u)
if err != nil || resp.StatusCode() != 200 {
if err == nil {
e,_ := resp.Error().(*APIError)
err = errors.New(e.Message)
}
return result,err
}
}
return result,nil
}