Skip to content

Commit

Permalink
[feature] Implement most Zone and all Zone Plan endpoints
Browse files Browse the repository at this point in the history
* Move Zone types to zone.go
* flarectl: zone info: show vanity name servers. The `zone info` command was always listing the default name servers even if custom name servers are configured.
* Response is an array of code/message, not strings. Add a new ResponseInfo struct containing the Code and Message fields, have the Response struct return a slice of ResponseInfo instead of a slice of strings.
* It seems the response info can also have a field "type" but I've yet to
see it return anything other than null, so omitting that for now.
* zone: Implement ZoneActivationCheck. This is used for initiating a manual zone activation check for new
zones.
* zone: Implement ZoneDetails. This is functionally similar to ListZones, but takes a zoneID instead of
a list of zone names and only returns information about a single zone. The information returned by the API is still an array.
* zone: Add ZoneID and ZoneIDRepsonse types. Some of the Zone endpoints return only the zone ID. This adds a new type to return just the ID rather than using an entire Zone object.
* zone: Implement DeleteZone
* zone: Implement AvailableZonePlans and ZonePlanDetails
* zone: Tidy up godoc comments. We don't need the HTTP call in the comment as it's not useful to the
library user. Add a blank line before "API reference" and put the URL on the same line
to make it look tidier.
* zone: Implement CreateZone
* Add missing godoc comment for ResponseInfo
* flarectl: correct usage for `zone create`
* zone: Split ZoneResponse. Some endpoints return a single Zone, some an array of Zones. Split this
response struct into ZoneResponse and ZonesResponse to handle that and
update all return values as appropriate.
* zone: Implement zone edit methods. Add four new methods: EditZone, ZoneSetPaused, ZoneSetVanityNS and ZoneSetPlan. This also sets most of the ZonePlan fields to `omitempty` so that we can
pass a plan ID when changing a zone's plan. EditZone wouldn't normally be called directly but is made public in case the consumer chooses to do so. ZoneSetPaused currently can only pause and not un-pause. This needs to be fixed.
* zone: Add missing error checks
  • Loading branch information
jamesog authored and elithrar committed May 26, 2016
1 parent 652b7bc commit d0f7110
Show file tree
Hide file tree
Showing 4 changed files with 366 additions and 143 deletions.
93 changes: 10 additions & 83 deletions cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ func New(key, email string, opts ...Option) (*API, error) {
return api, nil
}

// NewZone initializes Zone.
func NewZone() *Zone {
return &Zone{}
}

// ZoneIDByName retrieves a zone's ID from the name.
func (api *API) ZoneIDByName(zoneName string) (string, error) {
res, err := api.ListZones(zoneName)
Expand Down Expand Up @@ -136,12 +131,19 @@ func (api *API) request(method, uri string, reqBody io.Reader) (*http.Response,
return resp, nil
}

// ResponseInfo contains a code and message returned by the API as errors or
// informational messages inside the response.
type ResponseInfo struct {
Code int `json:"code"`
Message string `json:"message"`
}

// Response is a template. There will also be a result struct. There will be a
// unique response type for each response, which will include this type.
type Response struct {
Success bool `json:"success"`
Errors []string `json:"errors"`
Messages []string `json:"messages"`
Success bool `json:"success"`
Errors []ResponseInfo `json:"errors"`
Messages []ResponseInfo `json:"messages"`
}

// ResultInfo contains metadata about the Response.
Expand Down Expand Up @@ -183,81 +185,6 @@ type Owner struct {
OwnerType string `json:"owner_type"`
}

// Zone describes a CloudFlare zone.
type Zone struct {
ID string `json:"id"`
Name string `json:"name"`
DevMode int `json:"development_mode"`
OriginalNS []string `json:"original_name_servers"`
OriginalRegistrar string `json:"original_registrar"`
OriginalDNSHost string `json:"original_dnshost"`
CreatedOn time.Time `json:"created_on"`
ModifiedOn time.Time `json:"modified_on"`
NameServers []string `json:"name_servers"`
Owner Owner `json:"owner"`
Permissions []string `json:"permissions"`
Plan ZonePlan `json:"plan"`
Status string `json:"status"`
Paused bool `json:"paused"`
Type string `json:"type"`
Host struct {
Name string
Website string
} `json:"host"`
VanityNS []string `json:"vanity_name_servers"`
Betas []string `json:"betas"`
DeactReason string `json:"deactivation_reason"`
Meta ZoneMeta `json:"meta"`
}

// ZoneMeta metadata about a zone.
type ZoneMeta struct {
// custom_certificate_quota is broken - sometimes it's a string, sometimes a number!
// CustCertQuota int `json:"custom_certificate_quota"`
PageRuleQuota int `json:"page_rule_quota"`
WildcardProxiable bool `json:"wildcard_proxiable"`
PhishingDetected bool `json:"phishing_detected"`
}

// ZonePlan contains the plan information for a zone.
type ZonePlan 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"`
}

// ZoneResponse represents the response from the Zone endpoint.
type ZoneResponse struct {
Response
Result []Zone `json:"result"`
}

// ZonePlanResponse represents the response from the Zone Plan endpoint.
type ZonePlanResponse struct {
Response
Result []ZonePlan `json:"result"`
}

// ZoneSetting contains settings for a zone.
type ZoneSetting struct {
ID string `json:"id"`
Editable bool `json:"editable"`
ModifiedOn time.Time `json:"modified_on"`
Value interface{} `json:"value"`
TimeRemaining int `json:"time_remaining"`
}

// ZoneSettingResponse represents the response from the Zone Setting endpoint.
type ZoneSettingResponse struct {
Response
Result []ZoneSetting `json:"result"`
}

// DNSRecord represents a DNS record in a zone.
type DNSRecord struct {
ID string `json:"id,omitempty"`
Expand Down
81 changes: 80 additions & 1 deletion cmd/flarectl/flarectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,48 @@ func userInfo(*cli.Context) {
func userUpdate(*cli.Context) {
}

func zoneCreate(c *cli.Context) {
if err := checkEnv(); err != nil {
fmt.Println(err)
return
}
if err := checkFlags(c, "zone"); err != nil {
return
}
zone := c.String("zone")
jumpstart := c.Bool("jumpstart")
orgID := c.String("org-id")
var org cloudflare.Organization
if orgID != "" {
org.ID = orgID
}
api.CreateZone(zone, jumpstart, org)
}

func zoneCheck(c *cli.Context) {
if err := checkEnv(); err != nil {
fmt.Println(err)
return
}
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) {
if err := checkEnv(); err != nil {
fmt.Println(err)
Expand Down Expand Up @@ -162,12 +204,18 @@ func zoneInfo(c *cli.Context) {
}
var output []table
for _, z := range zones {
var nameservers []string
if len(z.VanityNS) > 0 {
nameservers = z.VanityNS
} else {
nameservers = z.NameServers
}
output = append(output, table{
"ID": z.ID,
"Zone": z.Name,
"Plan": z.Plan.LegacyID,
"Status": z.Status,
"Name Servers": strings.Join(z.NameServers, ", "),
"Name Servers": strings.Join(nameservers, ", "),
"Paused": fmt.Sprintf("%t", z.Paused),
"Type": z.Type,
})
Expand Down Expand Up @@ -506,6 +554,37 @@ func main() {
Action: zoneList,
Usage: "List all zones on an account",
},
{
Name: "create",
Aliases: []string{"c"},
Action: zoneCreate,
Usage: "Create a new zone",
Flags: []cli.Flag{
cli.StringFlag{
Name: "zone",
Usage: "zone name",
},
cli.BoolFlag{
Name: "jumpstart",
Usage: "automatically fetch DNS records",
},
cli.StringFlag{
Name: "org-id",
Usage: "organization ID",
},
},
},
{
Name: "check",
Action: zoneCheck,
Usage: "Initiate a zone activation check",
Flags: []cli.Flag{
cli.StringFlag{
Name: "zone",
Usage: "zone name",
},
},
},
{
Name: "info",
Aliases: []string{"i"},
Expand Down
10 changes: 5 additions & 5 deletions organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package cloudflare

// Organization represents a multi-user organization.
type Organization struct {
ID string
Name string
Status string
Permissions []string
Roles []string
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Status string `json:"status,omitempty"`
Permissions []string `json:"permissions,omitempty"`
Roles []string `json:"roles,omitempty"`
}

// OrganizationResponse represents the response from the Organization endpoint.
Expand Down
Loading

0 comments on commit d0f7110

Please sign in to comment.