/
zones.go
144 lines (117 loc) · 3.88 KB
/
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
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
package cloudflare
import (
"context"
"fmt"
"github.com/goccy/go-json"
)
const defaultZonesPerPage = 100
type ZonesService service
type ZoneCreateParams struct {
Name string `json:"name"`
JumpStart bool `json:"jump_start"`
Type string `json:"type"`
Account *Account `json:"organization,omitempty"`
}
type ZoneListParams struct {
Match string `url:"match,omitempty"`
Name string `url:"name,omitempty"`
AccountName string `url:"account.name,omitempty"`
Status string `url:"status,omitempty"`
AccountID string `url:"account.id,omitempty"`
Direction string `url:"direction,omitempty"`
ResultInfo // Rename `ResultInfo` in the next major version.
}
type ZoneUpdateParams struct {
ID string
Paused *bool `json:"paused"`
VanityNameServers []string `json:"vanity_name_servers,omitempty"`
Plan ZonePlan `json:"plan,omitempty"`
Type string `json:"type,omitempty"`
}
// New creates a new zone.
//
// API reference: https://api.cloudflare.com/#zone-zone-details
func (s *ZonesService) New(ctx context.Context, zone *ZoneCreateParams) (Zone, error) {
res, err := s.client.post(ctx, "/zones", zone)
if err != nil {
return Zone{}, err
}
var r ZoneResponse
err = json.Unmarshal(res, &r)
if err != nil {
return Zone{}, fmt.Errorf("failed to unmarshal zone JSON data: %w", err)
}
return r.Result, nil
}
// Get fetches a single zone.
//
// API reference: https://api.cloudflare.com/#zone-zone-details
func (s *ZonesService) Get(ctx context.Context, rc *ResourceContainer) (Zone, error) {
uri := fmt.Sprintf("/zones/%s", rc.Identifier)
res, err := s.client.get(ctx, uri, nil)
if err != nil {
return Zone{}, fmt.Errorf("failed to fetch zones: %w", err)
}
var r ZoneResponse
err = json.Unmarshal(res, &r)
if err != nil {
return Zone{}, fmt.Errorf("failed to unmarshal zone JSON data: %w", err)
}
return r.Result, nil
}
// List returns all zones that match the provided `ZoneParams` struct.
//
// Pagination is automatically handled unless `params.Page` is supplied.
//
// API reference: https://api.cloudflare.com/#zone-list-zones
func (s *ZonesService) List(ctx context.Context, params *ZoneListParams) ([]Zone, *ResultInfo, error) {
res, _ := s.client.get(ctx, buildURI("/zones", params), nil)
var r ZonesResponse
err := json.Unmarshal(res, &r)
if err != nil {
return []Zone{}, &ResultInfo{}, fmt.Errorf("failed to unmarshal zone JSON data: %w", err)
}
if params.Page < 1 && params.PerPage < 1 {
var zones []Zone
params.PerPage = defaultZonesPerPage
params.Page = 1
for !params.ResultInfo.Done() {
res, _ := s.client.get(ctx, buildURI("/zones", params), nil)
var zResponse ZonesResponse
err := json.Unmarshal(res, &zResponse)
if err != nil {
return []Zone{}, &ResultInfo{}, fmt.Errorf("failed to unmarshal zone JSON data: %w", err)
}
zones = append(zones, zResponse.Result...)
params.ResultInfo = zResponse.ResultInfo.Next()
}
r.Result = zones
}
return r.Result, &r.ResultInfo, nil
}
// Update modifies an existing zone.
//
// API reference: https://api.cloudflare.com/#zone-edit-zone
func (s *ZonesService) Update(ctx context.Context, params *ZoneUpdateParams) ([]Zone, error) {
uri := fmt.Sprintf("/zones/%s", params.ID)
res, _ := s.client.patch(ctx, uri, params)
var r ZonesResponse
err := json.Unmarshal(res, &r)
if err != nil {
return []Zone{}, fmt.Errorf("failed to unmarshal zone JSON data: %w", err)
}
return r.Result, nil
}
// Delete deletes a zone based on ID.
//
// API reference: https://api.cloudflare.com/#zone-delete-zone
func (s *ZonesService) Delete(ctx context.Context, rc *ResourceContainer) error {
uri := fmt.Sprintf("/zones/%s", rc.Identifier)
res, _ := s.client.delete(ctx, uri, nil)
var r ZoneResponse
err := json.Unmarshal(res, &r)
if err != nil {
return fmt.Errorf("failed to unmarshal zone JSON data: %w", err)
}
return nil
}