From 313dc494e149a53cee03f858d197a8cc40bebee0 Mon Sep 17 00:00:00 2001 From: Rich Abdill Date: Tue, 19 Dec 2017 16:27:43 -0600 Subject: [PATCH] Cleaning up --- CHANGELOG.md | 3 +- fastly/request.go | 1 + fastly/waf.go | 26 ++++++--------- fastly/waf_test.go | 81 +++++++++++++++++++++++++++++++++++++--------- 4 files changed, 78 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac627ddd9..83ae3d714 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/fastly/request.go b/fastly/request.go index b10d179f5..fecb5502b 100644 --- a/fastly/request.go +++ b/fastly/request.go @@ -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 { diff --git a/fastly/waf.go b/fastly/waf.go index 2d4de91ca..4d6b82560 100644 --- a/fastly/waf.go +++ b/fastly/waf.go @@ -30,10 +30,6 @@ type WAF struct { ConfigurationSet *WAFConfigurationSet `jsonapi:"relation,configuration_set"` } -func (waf *WAF) String() string { - return fmt.Sprintf("", 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)) @@ -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" @@ -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 @@ -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 { @@ -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 } @@ -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 diff --git a/fastly/waf_test.go b/fastly/waf_test.go index 556d8deb6..9b30e96d4 100644 --- a/fastly/waf_test.go +++ b/fastly/waf_test.go @@ -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) } } }