-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudflare.go
191 lines (174 loc) · 5.37 KB
/
cloudflare.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
package main
import (
"bytes"
"encoding/json"
"net/http"
"net/url"
)
const cfAPIBase = "https://api.cloudflare.com/client/v4/"
type cfCreateDNSRecord struct {
Type string `json:"type"`
Name string `json:"name"`
Content string `json:"content"`
TTL uint32 `json:"ttl,omitempty"`
Priority uint16 `json:"priority,omitempty"`
Proxied bool `json:"proxied,omitempty"`
}
type cfResponseError struct {
Code int `json:"code"`
Message string `json:"message"`
}
type cfResponseOwner struct {
ID string `json:"id"`
Email string `json:"email"`
OwnerType string `json:"owner_type"`
}
type cfResponsePlan struct {
ID string `json:"id"`
Name string `json:"name"`
Price int `json:"price"`
Currency string `json:"currency"`
Frequency string `json:"frequency"`
LegacyID string `json:"legacy_id"`
IsSubscribed bool `json:"is_subscribed"`
CanSubscribe bool `json:"can_subscribe"`
}
type cfResponseZone struct {
ID string `json:"id"`
Name string `json:"name"`
DevelopmentMode int `json:"development_mode"`
OriginalNameservers []string `json:"original_name_servers"`
OriginalRegistrar string `json:"original_registrar"`
OriginalDNSHost string `json:"original_dns_host"`
CreatedOn string `json:"created_on"`
ModifiedOn string `json:"modified_on"`
Owner cfResponseOwner `json:"owner"`
Permissions []string `json:"permissions"`
Plan cfResponsePlan `json:"plan"`
PlanPending cfResponsePlan `json:"plan_pending"`
Status string `json:"status"`
Paused bool `json:"paused"`
Type string `json:"type"`
Nameservers []string `json:"name_servers"`
}
type cfResponseRecordID struct {
ID string `json:"id"`
}
type cfResponseRecord struct {
cfResponseRecordID
Type string `json:"type"`
Name string `json:"name"`
Content string `json:"content"`
Proxiable bool `json:"proxiable"`
Proxied bool `json:"proxied"`
TTL int `json:"ttl"`
Locked bool `json:"locked"`
ZoneID string `json:"zone_id"`
ZoneName string `json:"zone_name"`
CreatedOn string `json:"created_on"`
ModifiedOn string `json:"modified_on"`
}
type cfResponseResultInfo struct {
Page int `json:"page"`
PerPage int `json:"per_page"`
Count int `json:"count"`
TotalCount int `json:"total_count"`
}
type cfListZonesResponse struct {
Success bool `json:"success"`
Errors []cfResponseError `json:"errors"`
Result []cfResponseZone `json:"result"`
ResultInfo cfResponseResultInfo `json:"result_info"`
}
type cfListRecordsResponse struct {
Success bool `json:"success"`
Errors []cfResponseError `json:"errors"`
Result []cfResponseRecord `json:"result"`
ResultInfo cfResponseResultInfo `json:"result_info"`
}
type cfDeleteRecordResponse struct {
Success bool `json:"success"`
Errors []cfResponseError `json:"errors"`
Result cfResponseRecordID `json:"result"`
}
type cfCreateRecordResponse struct {
Success bool `json:"success"`
Errors []cfResponseError `json:"errors"`
Result cfResponseRecord `json:"result"`
}
func cfGet(apiAccessToken, apiEmail, apiKey, urlExt string, v url.Values) (*http.Response, error) {
base, err := url.Parse(cfAPIBase)
if err != nil {
return nil, err
}
ext, err := url.Parse(urlExt)
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", base.ResolveReference(ext).String(), nil)
if err != nil {
return nil, err
}
if v != nil {
req.URL.RawQuery = v.Encode()
}
req.Header.Set("Content-Type", "application/json")
if apiAccessToken != "" {
req.Header.Set("Authorization", "Bearer "+apiAccessToken)
} else {
req.Header.Set("X-Auth-Email", apiEmail)
req.Header.Set("X-Auth-Key", apiKey)
}
return http.DefaultClient.Do(req)
}
func cfDelete(apiAccessToken, apiEmail, apiKey, urlExt string, v url.Values) (*http.Response, error) {
base, err := url.Parse(cfAPIBase)
if err != nil {
return nil, err
}
ext, err := url.Parse(urlExt)
if err != nil {
return nil, err
}
req, err := http.NewRequest("DELETE", base.ResolveReference(ext).String(), nil)
if err != nil {
return nil, err
}
if v != nil {
req.URL.RawQuery = v.Encode()
}
req.Header.Set("Content-Type", "application/json")
if apiAccessToken != "" {
req.Header.Set("Authorization", "Bearer "+apiAccessToken)
} else {
req.Header.Set("X-Auth-Email", apiEmail)
req.Header.Set("X-Auth-Key", apiKey)
}
return http.DefaultClient.Do(req)
}
func cfPostJSON(apiAccessToken, apiEmail, apiKey, urlExt string, v interface{}) (*http.Response, error) {
base, err := url.Parse(cfAPIBase)
if err != nil {
return nil, err
}
ext, err := url.Parse(urlExt)
if err != nil {
return nil, err
}
data, err := json.Marshal(v)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", base.ResolveReference(ext).String(), bytes.NewReader(data))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
if apiAccessToken != "" {
req.Header.Set("Authorization", "Bearer "+apiAccessToken)
} else {
req.Header.Set("X-Auth-Email", apiEmail)
req.Header.Set("X-Auth-Key", apiKey)
}
return http.DefaultClient.Do(req)
}