Skip to content

Commit

Permalink
Add rules functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Bridget Lane authored and sethvargo committed Oct 26, 2017
1 parent 74c03ce commit 34f0b9f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
4 changes: 4 additions & 0 deletions fastly/errors.go
Expand Up @@ -85,6 +85,10 @@ var ErrMissingWAFID = errors.New("Missing required field 'WAFID'")
// requires a "OWASPID" key, but one was not set
var ErrMissingOWASPID = errors.New("Missing required field 'OWASPID'")

// ErrMissingRuleID is an error that is returned was an input struct
// requires a "RuleID" key, but one was not set
var ErrMissingRuleID = errors.New("Missing required field 'RuleID'")

// Ensure HTTPError is, in fact, an error.
var _ error = (*HTTPError)(nil)

Expand Down
64 changes: 63 additions & 1 deletion fastly/waf.go
Expand Up @@ -276,7 +276,7 @@ func (c *Client) GetOWASP(i *GetOWASPInput) (*OWASP, error) {

// CreateOWASPInput is used as input to the CreateOWASP function.
type CreateOWASPInput struct {
// Service is the ID of the service. WafID is the ID of the firewall.
// Service is the ID of the service. ID is the ID of the firewall.
// Both fields are required.
Service string
ID string `jsonapi:"primary,owasp"`
Expand Down Expand Up @@ -373,3 +373,65 @@ func (c *Client) UpdateOWASP(i *UpdateOWASPInput) (*OWASP, error) {
}
return &owasp, nil
}

// RuleVCL is the information about a Rule's VCL.
type RuleVCL struct {
ID string `jsonapi:"primary,rule_vcl"`
VCL string `jsonapi:"attr,vcl,omitempty"`
}

// GetRuleVCLInput is used as input to the GetRuleVCL function.
type GetRuleVCLInput struct {
// RuleID is the ID of the rule and is required.
RuleID string
}

// GetRuleVCL gets the VCL for a Rule.
func (c *Client) GetRuleVCL(i *GetRuleVCLInput) (*RuleVCL, error) {
if i.RuleID == "" {
return nil, ErrMissingRuleID
}

path := fmt.Sprintf("/wafs/rules/%s/vcl", i.RuleID)
resp, err := c.Get(path, nil)
if err != nil {
return nil, err
}

var vcl RuleVCL
if err := jsonapi.UnmarshalPayload(resp.Body, &vcl); err != nil {
return nil, err
}
return &vcl, nil
}

// GetWAFRuleVCLInput is used as input to the GetWAFRuleVCL function.
type GetWAFRuleVCLInput struct {
// ID is the ID of the firewall. RuleID is the ID of the rule.
// Both are required.
ID string
RuleID string
}

// GetWAFRuleVCL gets the VCL for a role associated with a firewall WAF.
func (c *Client) GetWAFRuleVCL(i *GetWAFRuleVCLInput) (*RuleVCL, error) {
if i.ID == "" {
return nil, ErrMissingWAFID
}

if i.RuleID == "" {
return nil, ErrMissingRuleID
}

path := fmt.Sprintf("/wafs/%s/rules/%s/vcl", i.ID, i.RuleID)
resp, err := c.Get(path, nil)
if err != nil {
return nil, err
}

var vcl RuleVCL
if err := jsonapi.UnmarshalPayload(resp.Body, &vcl); err != nil {
return nil, err
}
return &vcl, nil
}

0 comments on commit 34f0b9f

Please sign in to comment.