forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzone.go
314 lines (278 loc) · 6.65 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package main
import (
"fmt"
"os"
"strings"
cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/urfave/cli"
)
func zoneCerts(*cli.Context) {
}
func zoneKeyless(*cli.Context) {
}
func zoneRailgun(*cli.Context) {
}
func zoneCreate(c *cli.Context) {
if err := checkFlags(c, "zone"); err != nil {
return
}
zone := c.String("zone")
jumpstart := c.Bool("jumpstart")
accountID := c.String("account-id")
zoneType := c.String("type")
var account cloudflare.Account
if accountID != "" {
account.ID = accountID
}
if zoneType != "partial" {
zoneType = "full"
}
_, err := api.CreateZone(zone, jumpstart, account, zoneType)
if err != nil {
fmt.Fprintln(os.Stderr, fmt.Sprintf("%s", err))
return
}
}
func zoneCheck(c *cli.Context) {
if err := checkFlags(c, "zone"); err != nil {
return
}
zone := c.String("zone")
zoneID, err := api.ZoneIDByName(zone)
if err != nil {
fmt.Println(err)
return
}
res, err := api.ZoneActivationCheck(zoneID)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%s\n", res.Messages[0].Message)
}
func zoneList(c *cli.Context) {
zones, err := api.ListZones()
if err != nil {
fmt.Println(err)
return
}
output := make([][]string, 0, len(zones))
for _, z := range zones {
output = append(output, []string{
z.ID,
z.Name,
z.Plan.Name,
z.Status,
})
}
writeTable(output, "ID", "Name", "Plan", "Status")
}
func zoneDelete(c *cli.Context) {
if err := checkFlags(c, "zone"); err != nil {
return
}
zoneID, err := api.ZoneIDByName(c.String("zone"))
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
_, err = api.DeleteZone(zoneID)
if err != nil {
fmt.Fprintln(os.Stderr, fmt.Sprintf("%s", err))
return
}
}
func zoneCreateLockdown(c *cli.Context) {
if err := checkFlags(c, "zone", "urls", "targets", "values"); err != nil {
return
}
zoneID, err := api.ZoneIDByName(c.String("zone"))
if err != nil {
fmt.Println(err)
return
}
targets := c.StringSlice("targets")
values := c.StringSlice("values")
if len(targets) != len(values) {
cli.ShowCommandHelp(c, "targets and values does not match")
return
}
var zonelockdownconfigs = []cloudflare.ZoneLockdownConfig{}
for index := 0; index < len(targets); index++ {
zonelockdownconfigs = append(zonelockdownconfigs, cloudflare.ZoneLockdownConfig{
Target: c.StringSlice("targets")[index],
Value: c.StringSlice("values")[index],
})
}
lockdown := cloudflare.ZoneLockdown{
Description: c.String("description"),
URLs: c.StringSlice("urls"),
Configurations: zonelockdownconfigs,
}
var resp *cloudflare.ZoneLockdownResponse
resp, err = api.CreateZoneLockdown(zoneID, lockdown)
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating ZONE lock down: ", err)
return
}
output := make([][]string, 0, 1)
output = append(output, formatLockdownResponse(resp))
writeTable(output, "ID")
}
func zoneInfo(c *cli.Context) {
var zone string
if len(c.Args()) > 0 {
zone = c.Args()[0]
} else if c.String("zone") != "" {
zone = c.String("zone")
} else {
cli.ShowSubcommandHelp(c)
return
}
zones, err := api.ListZones(zone)
if err != nil {
fmt.Println(err)
return
}
output := make([][]string, 0, len(zones))
for _, z := range zones {
var nameservers []string
if len(z.VanityNS) > 0 {
nameservers = z.VanityNS
} else {
nameservers = z.NameServers
}
output = append(output, []string{
z.ID,
z.Name,
z.Plan.Name,
z.Status,
strings.Join(nameservers, ", "),
fmt.Sprintf("%t", z.Paused),
z.Type,
})
}
writeTable(output, "ID", "Zone", "Plan", "Status", "Name Servers", "Paused", "Type")
}
func zonePlan(*cli.Context) {
}
func zoneSettings(*cli.Context) {
}
func zoneCachePurge(c *cli.Context) {
if err := checkFlags(c, "zone"); err != nil {
cli.ShowSubcommandHelp(c)
return
}
zoneName := c.String("zone")
zoneID, err := api.ZoneIDByName(c.String("zone"))
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
var resp cloudflare.PurgeCacheResponse
// Purge everything
if c.Bool("everything") {
resp, err = api.PurgeEverything(zoneID)
if err != nil {
fmt.Fprintf(os.Stderr, "Error purging all from zone %q: %s\n", zoneName, err)
return
}
} else {
var (
files = c.StringSlice("files")
tags = c.StringSlice("tags")
hosts = c.StringSlice("hosts")
)
if len(files) == 0 && len(tags) == 0 && len(hosts) == 0 {
fmt.Fprintln(os.Stderr, "You must provide at least one of the --files, --tags or --hosts flags")
return
}
// Purge selectively
purgeReq := cloudflare.PurgeCacheRequest{
Files: c.StringSlice("files"),
Tags: c.StringSlice("tags"),
Hosts: c.StringSlice("hosts"),
}
resp, err = api.PurgeCache(zoneID, purgeReq)
if err != nil {
fmt.Fprintf(os.Stderr, "Error purging the cache from zone %q: %s\n", zoneName, err)
return
}
}
output := make([][]string, 0, 1)
output = append(output, formatCacheResponse(resp))
writeTable(output, "ID")
}
func zoneRecords(c *cli.Context) {
var zone string
if len(c.Args()) > 0 {
zone = c.Args()[0]
} else if c.String("zone") != "" {
zone = c.String("zone")
} else {
cli.ShowSubcommandHelp(c)
return
}
zoneID, err := api.ZoneIDByName(zone)
if err != nil {
fmt.Println(err)
return
}
// Create a an empty record for searching for records
rr := cloudflare.DNSRecord{}
var records []cloudflare.DNSRecord
if c.String("id") != "" {
rec, err := api.DNSRecord(zoneID, c.String("id"))
if err != nil {
fmt.Println(err)
return
}
records = append(records, rec)
} else {
if c.String("name") != "" {
rr.Name = c.String("name")
}
if c.String("content") != "" {
rr.Name = c.String("content")
}
var err error
records, err = api.DNSRecords(zoneID, rr)
if err != nil {
fmt.Println(err)
return
}
}
output := make([][]string, 0, len(records))
for _, r := range records {
switch r.Type {
case "MX":
r.Content = fmt.Sprintf("%d %s", r.Priority, r.Content)
case "SRV":
dp := r.Data.(map[string]interface{})
r.Content = fmt.Sprintf("%.f %s", dp["priority"], r.Content)
// Cloudflare's API, annoyingly, automatically prepends the weight
// and port into content, separated by tabs.
// XXX: File this as a bug. LOC doesn't do this.
r.Content = strings.Replace(r.Content, "\t", " ", -1)
}
output = append(output, []string{
r.ID,
r.Type,
r.Name,
r.Content,
fmt.Sprintf("%t", r.Proxied),
fmt.Sprintf("%d", r.TTL),
})
}
writeTable(output, "ID", "Type", "Name", "Content", "Proxied", "TTL")
}
func formatCacheResponse(resp cloudflare.PurgeCacheResponse) []string {
return []string{
resp.Result.ID,
}
}
func formatLockdownResponse(resp *cloudflare.ZoneLockdownResponse) []string {
return []string{
resp.Result.ID,
}
}