-
Notifications
You must be signed in to change notification settings - Fork 3
/
client_zones.go
112 lines (103 loc) · 2.61 KB
/
client_zones.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
package cloudflare
import (
"bytes"
"encoding/json"
"golang.org/x/net/context"
)
// Zones - Cloudflare Zones API Client.
type Zones struct {
// Options - Client options.
*Options
}
// Create - Creates a zone.
func (zones *Zones) Create(ctx context.Context, domain string) (zone *Zone, err error) {
buffer := new(bytes.Buffer)
err = json.NewEncoder(buffer).Encode(struct {
Name string `json:"name"`
}{
Name: domain,
})
if err != nil {
return
}
response, err := httpDo(ctx, zones.Options, "POST", apiURL("/zones"), buffer)
if err != nil {
return
}
defer response.Body.Close()
result, err := readResponse(response.Body)
if err != nil {
return
}
zone = new(Zone)
err = json.Unmarshal(result.Result, &zone)
return
}
// List - Lists all zones.
func (zones *Zones) List(ctx context.Context) ([]*Zone, error) {
return zones.listPages(ctx, 1)
}
// Details - Requests Zone details by ID.
func (zones *Zones) Details(ctx context.Context, id string) (zone *Zone, err error) {
response, err := httpDo(ctx, zones.Options, "GET", apiURL("/zones/%s", id), nil)
if err != nil {
return
}
defer response.Body.Close()
result, err := readResponse(response.Body)
if err != nil {
return
}
zone = new(Zone)
err = json.Unmarshal(result.Result, &zone)
return
}
// Patch - Patches a zone. It has a limited possibilities.
func (zones *Zones) Patch(ctx context.Context, id string, patch *ZonePatch) (err error) {
buffer := new(bytes.Buffer)
err = json.NewEncoder(buffer).Encode(patch)
if err != nil {
return
}
response, err := httpDo(ctx, zones.Options, "POST", apiURL("/zones/%s", id), buffer)
if err != nil {
return
}
defer response.Body.Close()
_, err = readResponse(response.Body)
return
}
// Delete - Deletes zone by id.
func (zones *Zones) Delete(ctx context.Context, id string) (err error) {
response, err := httpDo(ctx, zones.Options, "DELETE", apiURL("/zones/%s", id), nil)
if err != nil {
return
}
defer response.Body.Close()
_, err = readResponse(response.Body)
return
}
// listPages - Gets all pages starting from `page`.
func (zones *Zones) listPages(ctx context.Context, page int) (list []*Zone, err error) {
response, err := httpDo(ctx, zones.Options, "GET", apiURL("/zones?page=%d&per_page=50", page), nil)
if err != nil {
return
}
defer response.Body.Close()
result, err := readResponse(response.Body)
if err != nil {
return
}
err = json.Unmarshal(result.Result, &list)
if err != nil {
return
}
if result.ResultInfo == nil || page >= result.ResultInfo.TotalPages {
return
}
next, err := zones.listPages(ctx, page+1)
if err != nil {
return
}
return append(list, next...), nil
}