Skip to content

Commit

Permalink
Cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich Abdill authored and sethvargo committed Dec 22, 2017
1 parent 787809f commit 313dc49
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 33 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## v0.4.3 (Unreleased)

- Add WAF methods for fetching status of rules, both one at a time and in lists
- Add WAF methods for fetching status of rules, both one at a time and in filtered lists
- Add WAF methods for modifying the status of rules, both one at a time and based on tags
- Rename `UpdateWafRuleSets` function to `UpdateWAFRuleSets` to match other names

## v0.4.2 (September 5, 2017)
Expand Down
1 change: 1 addition & 0 deletions fastly/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (c *Client) RawRequest(verb, p string, ro *RequestOptions) (*http.Request,
params.Add(k, v)
}
u.RawQuery = params.Encode()

// Create the request object.
request, err := http.NewRequest(verb, u.String(), ro.Body)
if err != nil {
Expand Down
26 changes: 10 additions & 16 deletions fastly/waf.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ type WAF struct {
ConfigurationSet *WAFConfigurationSet `jsonapi:"relation,configuration_set"`
}

func (waf *WAF) String() string {
return fmt.Sprintf("<ID: |%v|, Version: |%v|>", waf.ID, waf.Version)
}

// wafType is used for reflection because JSONAPI wants to know what it's
// decoding into.
var wafType = reflect.TypeOf(new(WAF))
Expand Down Expand Up @@ -584,7 +580,7 @@ type WAFRuleStatus struct {
ID string `jsonapi:"primary,rule_status"` // This is the ID of the status, not the ID of the rule. Currently, it is of the format ${WAF_ID}-${rule_ID}, if you want to infer those based on this field.
Status string `jsonapi:"attr,status"`

Tag string `jsonapi:"attr,name,omitempty"` // NOTE: This will only be set in a response for modifying rules based on tag.
Tag string `jsonapi:"attr,name,omitempty"` // This will only be set in a response for modifying rules based on tag.

// HACK: These two fields are supposed to be sent in response
// to requests for rule status data, but the entire "Relationships"
Expand All @@ -603,12 +599,12 @@ type GetWAFRuleStatusesFilters struct {
Message string
Revision int
RuleID string
TagID int // filter by single tag ID
TagName string // filter by single tag name
TagID int // Filter by a single tag ID.
TagName string // Filter by single tag name.
Version string
Tags []int // return all rules with any of the specified tag IDs
MaxResults int // max number of returned results
Page int // which page of results to return
Tags []int // Return all rules with any of the specified tag IDs.
MaxResults int // Max number of returned results per request.
Page int // Which page of results to return.
}

// formatFilters converts user input into query parameters for filtering
Expand Down Expand Up @@ -671,9 +667,7 @@ func (c *Client) GetWAFRuleStatuses(i *GetWAFRuleStatusesInput) (GetWAFRuleStatu
}

path := fmt.Sprintf("/service/%s/wafs/%s/rule_statuses", i.Service, i.WAF)
filters := &RequestOptions{
Params: i.formatFilters(),
}
filters := &RequestOptions{Params: i.formatFilters()}

resp, err := c.Get(path, filters)
if err != nil {
Expand All @@ -697,8 +691,7 @@ func (c *Client) interpretWAFRuleStatusesPage(answer *GetWAFRuleStatusesResponse
if err != nil {
return err
}
var statusType = reflect.TypeOf(new(WAFRuleStatus))
data, err := jsonapi.UnmarshalManyPayload(body, statusType)
data, err := jsonapi.UnmarshalManyPayload(body, reflect.TypeOf(new(WAFRuleStatus)))
if err != nil {
return err
}
Expand Down Expand Up @@ -882,7 +875,8 @@ type updateWAFRuleTagStatusData struct {
}

// validate makes sure the UpdateWAFRuleStatusInput instance has all
// fields we need to request a change.
// fields we need to request a change. Almost, but not quite, identical to
// UpdateWAFRuleStatusInput.validate()
func (i UpdateWAFRuleTagStatusInput) validate() error {
if i.Tag == "" {
return ErrMissingTag
Expand Down
81 changes: 65 additions & 16 deletions fastly/waf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,30 +325,79 @@ func TestClient_GetWAF_validation(t *testing.T) {
// }
// }

func TestReceivedWAFRuleStatus_simplify(t *testing.T) {
func TestUpdateWAFRuleStatusesInput_validate(t *testing.T) {
tests := []struct {
input receivedWAFRuleStatus
expected WAFRuleStatus
description string
input UpdateWAFRuleStatusInput
expected error
}{
{
input: receivedWAFRuleStatus{
id: "71a9qq1",
rule: &ruleStatusRuleRelation{id: 7},
waf: &ruleStatusWAFRelation{id: "a18"},
status: "log",
description: "Accepts valid input",
input: UpdateWAFRuleStatusInput{
ID: 8104,
Service: "108asj1",
WAF: "as098k",
Status: "block",
},
expected: WAFRuleStatus{
RuleID: 7,
WAFID: "a18",
StatusID: "71a9qq1",
Status: "log",
expected: nil,
},
{
description: "Rejects input with missing int field",
input: UpdateWAFRuleStatusInput{
Service: "108asj1",
WAF: "as098k",
Status: "block",
},
expected: ErrMissingRuleID,
},
{
description: "Rejects input with missing string field",
input: UpdateWAFRuleStatusInput{
ID: 8104,
WAF: "as098k",
Status: "block",
},
expected: ErrMissingService,
},
}
for _, testcase := range tests {
err := testcase.input.validate()
if err != testcase.expected {
t.Errorf("In test %s: Expected %v,got %v", testcase.description, testcase.expected, err)
}
}
}

func TestUpdateWAFRuleTagStatusInput_validate(t *testing.T) {
tests := []struct {
description string
input UpdateWAFRuleTagStatusInput
expected error
}{
{
description: "Accepts valid input",
input: UpdateWAFRuleTagStatusInput{
Tag: "lala tag la",
Service: "108asj1",
WAF: "as098k",
Status: "block",
},
expected: nil,
},
{
description: "Rejects input with missing string field",
input: UpdateWAFRuleTagStatusInput{
Service: "108asj1",
WAF: "as098k",
Status: "block",
},
expected: ErrMissingTag,
},
}
for _, testcase := range tests {
answer := testcase.input.simplify()
if answer != testcase.expected {
t.Errorf("Expected %+v,got %+v", testcase.expected, answer)
err := testcase.input.validate()
if err != testcase.expected {
t.Errorf("In test %s: Expected %v,got %v", testcase.description, testcase.expected, err)
}
}
}
Expand Down

0 comments on commit 313dc49

Please sign in to comment.