Skip to content

Commit

Permalink
Added zonelockdown feature on flarectl (cloudflare#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
viggy28 authored and patryk committed Aug 8, 2019
1 parent 9b4f9c3 commit ee401f0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
24 changes: 24 additions & 0 deletions cmd/flarectl/flarectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,30 @@ func main() {
},
},
},
{
Name: "lockdown",
Aliases: []string{"lo"},
Action: zoneCreateLockdown,
Usage: "Lockdown a zone based on config",
Flags: []cli.Flag{
cli.StringFlag{
Name: "zone",
Usage: "zone name",
},
cli.StringSliceFlag{
Name: "urls",
Usage: "a list of [exact] URLs to lockdown",
},
cli.StringSliceFlag{
Name: "targets",
Usage: "a list of targets type",
},
cli.StringSliceFlag{
Name: "values",
Usage: "a list of values such as ip, ip_range etc.",
},
},
},
{
Name: "plan",
Aliases: []string{"p"},
Expand Down
47 changes: 47 additions & 0 deletions cmd/flarectl/zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,47 @@ func zoneList(c *cli.Context) {
writeTable(output, "ID", "Name", "Plan", "Status")
}

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 {
Expand Down Expand Up @@ -247,3 +288,9 @@ func formatCacheResponse(resp cloudflare.PurgeCacheResponse) []string {
resp.Result.ID,
}
}

func formatLockdownResponse(resp *cloudflare.ZoneLockdownResponse) []string {
return []string{
resp.Result.ID,
}
}

0 comments on commit ee401f0

Please sign in to comment.