Skip to content

Commit

Permalink
rule-statues functions (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
cleanshavenalex authored and sethvargo committed Dec 22, 2017
1 parent 10e52ed commit ad02a14
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 0 deletions.
44 changes: 44 additions & 0 deletions fastly/fixtures/wafs/rule_status/get.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
version: 1
rwmutex: {}
interactions:
- request:
body: ""
form: {}
headers:
Fastly-Key:
- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
User-Agent:
- FastlyGo/0.4.1 (+github.com/sethvargo/go-fastly; go1.9)
url: https://api.fastly.com/service/7i6HN3TK9wS159v2gPAZ8A/wafs/4fuxZ7bpag8laOjYwxlqZM/rules/933120/rule_status
method: GET
response:
body: '{"data": { "attributes": { "status": "log"}, "id":"4fuxZ7bpag8laOjYwxlqZM-933120","type":"rule_status" } }'
headers:
Accept-Ranges:
- bytes
Age:
- "0"
Content-Length:
- "281"
Content-Type:
- application/vnd.api+json
Date:
- Tue, 05 Sep 2017 17:56:54 GMT
Status:
- 200 OK
Via:
- 1.1 varnish
- 1.1 varnish
X-Cache:
- MISS, MISS
X-Cache-Hits:
- 0, 0
X-Content-Type-Options:
- nosniff
X-Served-By:
- app-slwdc9051-SL, cache-dca17730-DCA
X-Timer:
- S1504634214.963647,VS0,VE133
status: 200 OK
code: 200
49 changes: 49 additions & 0 deletions fastly/fixtures/wafs/rule_status/update.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
version: 1
rwmutex: {}
interactions:
- request:
body: |
{"data": { "attributes": { "status": "block" }, "id": "4fuxZ7bpag8laOjYwxlqZM-933120", "type": "rule_status" }
form: {}
headers:
Accept:
- application/vnd.api+json
Content-Type:
- application/vnd.api+json
Fastly-Key:
- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
User-Agent:
- FastlyGo/0.4.1 (+github.com/sethvargo/go-fastly; go1.9)
url: https://api.fastly.com/service/7i6HN3TK9wS159v2gPAZ8A/wafs/4fuxZ7bpag8laOjYwxlqZM/rules/933120/rule_status
method: PATCH
response:
body: '{"data": { "attributes": { "status": "block"}, "id":"4fuxZ7bpag8laOjYwxlqZM-933120","type":"rule_status" } }'
headers:
Accept-Ranges:
- bytes
Age:
- "0"
Content-Length:
- "281"
Content-Type:
- application/vnd.api+json
Date:
- Tue, 05 Sep 2017 17:56:54 GMT
Status:
- 200 OK
Via:
- 1.1 varnish
- 1.1 varnish
X-Cache:
- MISS, MISS
X-Cache-Hits:
- 0, 0
X-Content-Type-Options:
- nosniff
X-Served-By:
- app-slwdc9051-SL, cache-dca17730-DCA
X-Timer:
- S1504634214.963647,VS0,VE133
status: 200 OK
code: 200
110 changes: 110 additions & 0 deletions fastly/waf.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/google/jsonapi"
"strconv"
)

// WAFConfigurationSet represents information about a configuration_set.
Expand Down Expand Up @@ -499,13 +500,122 @@ func (c *Client) GetWAFRuleVCL(i *GetWAFRuleVCLInput) (*RuleVCL, error) {
return &vcl, nil
}


// RuleStatus is the status of a rule for a particular service.
type RuleStatus struct {
ID string `jsonapi:"primary,rule_status"`
Status string `jsonapi:"attr,status,omitempty"`
}

// GetWAFRuleStatusInput is used as input to the GetWAFRuleStatusesInput
type GetWAFRuleStatusesInput struct {
Service string
ID string
}

// Needed for jsonapi to parse rule_statuses response
var ruleStatusType = reflect.TypeOf(new(RuleStatus))

// GetWAFRuleStatuses gets the rule_statuses for a firewall.
func (c *Client) GetWAFRuleStatuses(i *GetWAFRuleStatusesInput) ([]*RuleStatus, error) {
if i.Service == "" {
return nil, ErrMissingService
}
if i.ID == "" {
return nil, ErrMissingWAFID
}
// TODO add filter and page params
path := fmt.Sprintf( "/service/%s/wafs/%s/rule_statuses", i.Service, i.ID)
resp, err := c.Get(path, nil)
if err != nil {
return nil, err
}

data, err := jsonapi.UnmarshalManyPayload(resp.Body, ruleStatusType)
if err != nil {
return nil, err
}

rule_statuses := make([]*RuleStatus, len(data))
for i := range data {
typed, ok := data[i].(*RuleStatus)
if !ok {
return nil, fmt.Errorf("got back a non-RuleStatus response")
}
rule_statuses[i] = typed
}
return rule_statuses, nil
}

// GetWAFRuleStatusInput is the input to get a rule status
type GetWAFRuleStatusInput struct {
Service string
RuleID int
ID string
}

// GetWafRuleStatus will get the RuleStatus for a RuleID and WAF in a Service.
func (c *Client) GetWAFRuleStatus(i *GetWAFRuleStatusInput) (*RuleStatus, error) {
if i.Service == "" {
return nil, ErrMissingService
}
if i.ID == "" {
return nil, ErrMissingWAFID
}
if i.RuleID == 0 {
return nil, ErrMissingRuleID
}
path := fmt.Sprintf( "/service/%s/wafs/%s/rules/%s/rule_status", i.Service, i.ID, strconv.Itoa(i.RuleID) )
resp, err := c.Get(path, nil)
if err != nil {
return nil, err
}
var rule_status RuleStatus
if err := jsonapi.UnmarshalPayload(resp.Body, &rule_status); err != nil {
return nil, err
}
return &rule_status, nil
}

// Ruleset is the information about a firewall object's ruleset.
type Ruleset struct {
ID string `jsonapi:"primary,ruleset"`
VCL string `jsonapi:"attr,vcl,omitempty"`
LastPush string `jsonapi:"attr,last_push,omitempty"`
}

// UpdateWAFRuleStatusInput is the input to update a rule status.
type UpdateWAFRuleStatusInput struct {
Service string
WafID string
RuleID int
Status string `jsonapi:"attr,status"`
ID string `jsonapi:"primary,rule_status"`
}

// UpdateWAFRuleStatus will update a rule status for a waf.
func (c *Client) UpdateWAFRuleStatus(i *UpdateWAFRuleStatusInput) (*RuleStatus, error) {
if i.Service == "" {
return nil, ErrMissingService
}
if i.WafID == "" {
return nil, ErrMissingWAFID
}
if i.RuleID == 0 {
return nil, ErrMissingRuleID
}
path := fmt.Sprintf( "/service/%s/wafs/%s/rules/%s/rule_status", i.Service, i.WafID, strconv.Itoa(i.RuleID) )
resp, err := c.PatchJSONAPI(path, i ,nil)
if err != nil {
return nil, err
}
var rule_status RuleStatus
if err := jsonapi.UnmarshalPayload(resp.Body, &rule_status); err != nil {
return nil, err
}
return &rule_status, nil
}

// GetWAFRuleRuleSetsInput is used as input to the GetWAFRuleRuleSets function.
type GetWAFRuleRuleSetsInput struct {
// Service is the ID of the service. ID is the ID of the firewall.
Expand Down
32 changes: 32 additions & 0 deletions fastly/waf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"io/ioutil"
"testing"
"fmt"
"strconv"
)

func TestClient_WAFs(t *testing.T) {
Expand Down Expand Up @@ -205,6 +207,33 @@ func TestClient_WAFs(t *testing.T) {
if err != nil {
t.Fatal(err)
}

var rsro *RuleStatus
record(t, "wafs/rule_status/get", func(c *Client) {
rsro, err = c.GetWAFRuleStatus(&GetWAFRuleStatusInput{
Service: testServiceID,
RuleID: 933120,
ID: waf.ID,
})
})
if err != nil {
t.Fatal(err)
}
expectedID := fmt.Sprintf("%v-%v", waf.ID, strconv.Itoa(933120))

if rsro.ID != expectedID {
t.Errorf("Get RuleStatus failed %s\n", rsro.ID)
}

var urs *RuleStatus
record(t, "wafs/rule_status/update", func(c *Client) {
urs, err = c.UpdateWAFRuleStatus(&UpdateWAFRuleStatusInput{
Service: testServiceID,
RuleID: 933120,
ID: waf.ID,
})
})

}

func TestClient_ListWAFs_validation(t *testing.T) {
Expand Down Expand Up @@ -270,6 +299,9 @@ func TestClient_GetWAF_validation(t *testing.T) {
}
}




//
// func TestClient_UpdateWAF_validation(t *testing.T) {
// var err error
Expand Down

0 comments on commit ad02a14

Please sign in to comment.