forked from ns1/ns1-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzone.go
188 lines (163 loc) · 4.47 KB
/
zone.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
package rest
import (
"errors"
"fmt"
"net/http"
"gopkg.in/ns1/ns1-go.v2/rest/model/dns"
)
// ZonesService handles 'zones' endpoint.
type ZonesService service
// List returns all active zones and basic zone configuration details for each.
//
// NS1 API docs: https://ns1.com/api/#zones-get
func (s *ZonesService) List() ([]*dns.Zone, *http.Response, error) {
req, err := s.client.NewRequest("GET", "zones", nil)
if err != nil {
return nil, nil, err
}
zl := []*dns.Zone{}
var resp *http.Response
if s.client.FollowPagination {
resp, err = s.client.DoWithPagination(req, &zl, s.nextZones)
} else {
resp, err = s.client.Do(req, &zl)
}
if err != nil {
return nil, resp, err
}
return zl, resp, nil
}
// Get takes a zone name and returns a single active zone and its basic configuration details.
//
// NS1 API docs: https://ns1.com/api/#zones-zone-get
func (s *ZonesService) Get(zone string) (*dns.Zone, *http.Response, error) {
path := fmt.Sprintf("zones/%s", zone)
req, err := s.client.NewRequest("GET", path, nil)
if err != nil {
return nil, nil, err
}
var z dns.Zone
var resp *http.Response
if s.client.FollowPagination {
resp, err = s.client.DoWithPagination(req, &z, s.nextRecords)
} else {
resp, err = s.client.Do(req, &z)
}
if err != nil {
switch err.(type) {
case *Error:
if err.(*Error).Message == "zone not found" {
return nil, resp, ErrZoneMissing
}
}
return nil, resp, err
}
return &z, resp, nil
}
// Create takes a *Zone and creates a new DNS zone.
//
// NS1 API docs: https://ns1.com/api/#zones-put
func (s *ZonesService) Create(z *dns.Zone) (*http.Response, error) {
path := fmt.Sprintf("zones/%s", z.Zone)
req, err := s.client.NewRequest("PUT", path, &z)
if err != nil {
return nil, err
}
// Update zones fields with data from api(ensure consistent)
resp, err := s.client.Do(req, &z)
if err != nil {
switch err.(type) {
case *Error:
if err.(*Error).Message == "zone already exists" {
return resp, ErrZoneExists
}
}
return resp, err
}
return resp, nil
}
// Update takes a *Zone and modifies basic details of a DNS zone.
//
// NS1 API docs: https://ns1.com/api/#zones-post
func (s *ZonesService) Update(z *dns.Zone) (*http.Response, error) {
path := fmt.Sprintf("zones/%s", z.Zone)
req, err := s.client.NewRequest("POST", path, &z)
if err != nil {
return nil, err
}
// Update zones fields with data from api(ensure consistent)
resp, err := s.client.Do(req, &z)
if err != nil {
switch err.(type) {
case *Error:
if err.(*Error).Message == "zone not found" {
return resp, ErrZoneMissing
}
}
return resp, err
}
return resp, nil
}
// Delete takes a zone and destroys an existing DNS zone and all records in the zone.
//
// NS1 API docs: https://ns1.com/api/#zones-delete
func (s *ZonesService) Delete(zone string) (*http.Response, error) {
path := fmt.Sprintf("zones/%s", zone)
req, err := s.client.NewRequest("DELETE", path, nil)
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
switch err.(type) {
case *Error:
if err.(*Error).Message == "zone not found" {
return resp, ErrZoneMissing
}
}
return resp, err
}
return resp, nil
}
// nextZones is a pagination helper than gets and appends another list of zones
// to the passed list.
func (s *ZonesService) nextZones(v *interface{}, uri string) (*http.Response, error) {
tmpZl := []*dns.Zone{}
resp, err := s.client.getURI(&tmpZl, uri)
if err != nil {
return resp, err
}
zoneList, ok := (*v).(*[]*dns.Zone)
if !ok {
return nil, fmt.Errorf(
"incorrect value for v, expected value of type *[]*dns.Zone, got: %T", v,
)
}
*zoneList = append(*zoneList, tmpZl...)
return resp, nil
}
// nextRecords is a pagination helper tha gets and appends another set of
// records to the passed zone.
func (s *ZonesService) nextRecords(v *interface{}, uri string) (*http.Response, error) {
var tmpZone dns.Zone
resp, err := s.client.getURI(&tmpZone, uri)
if err != nil {
return resp, err
}
zone, ok := (*v).(*dns.Zone)
if !ok {
return nil, fmt.Errorf(
"incorrect value for v, expected value of type *dns.Zone, got: %T", v,
)
}
// Aside from Records, the rest of the zone data is identical in the
// paginated response.
zone.Records = append(zone.Records, tmpZone.Records...)
return resp, nil
}
var (
// ErrZoneExists bundles PUT create error.
ErrZoneExists = errors.New("zone already exists")
// ErrZoneMissing bundles GET/POST/DELETE error.
ErrZoneMissing = errors.New("zone does not exist")
)