From fd5cfdcdfb1b0733cace58298654c55517c3e388 Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Wed, 26 Jun 2019 09:25:23 +0000 Subject: [PATCH] Add CC Attack Protection Rule --- .../v1/ccattackprotection_rules/requests.go | 77 +++++++++++++++++++ .../v1/ccattackprotection_rules/results.go | 48 ++++++++++++ .../waf/v1/ccattackprotection_rules/urls.go | 11 +++ 3 files changed, 136 insertions(+) create mode 100644 openstack/waf/v1/ccattackprotection_rules/requests.go create mode 100644 openstack/waf/v1/ccattackprotection_rules/results.go create mode 100644 openstack/waf/v1/ccattackprotection_rules/urls.go diff --git a/openstack/waf/v1/ccattackprotection_rules/requests.go b/openstack/waf/v1/ccattackprotection_rules/requests.go new file mode 100644 index 000000000..e95e2c80e --- /dev/null +++ b/openstack/waf/v1/ccattackprotection_rules/requests.go @@ -0,0 +1,77 @@ +package ccattackprotection_rules + +import ( + "github.com/huaweicloud/golangsdk" +) + +var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{ + MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"}, +} + +// CreateOptsBuilder allows extensions to add additional parameters to the +// Create request. +type CreateOptsBuilder interface { + ToCcAttackCreateMap() (map[string]interface{}, error) +} + +// CreateOpts contains all the values needed to create a new cc attack protection rule. +type CreateOpts struct { + Url string `json:"url" required:"true"` + LimitNum int `json:"limit_num" required:"true"` + LimitPeriod int `json:"limit_period" required:"true"` + LockTime int `json:"lock_time,omitempty"` + TagType string `json:"tag_type" required:"true"` + TagIndex string `json:"tag_index,omitempty"` + TagCondition TagCondition `json:"tag_condition,omitempty"` + Action Action `json:"action" required:"true"` +} + +type TagCondition struct { + Category string `json:"category" required:"true"` + Contents []string `json:"contents" required:"true"` +} + +type Action struct { + Category string `json:"category" required:"true"` + Detail Detail `json:"detail,omitempty"` +} + +type Detail struct { + Response Response `json:"response" required:"true"` +} + +type Response struct { + ContentType string `json:"content_type" required:"true"` + Content string `json:"content" required:"true"` +} + +// ToCcAttackCreateMap builds a create request body from CreateOpts. +func (opts CreateOpts) ToCcAttackCreateMap() (map[string]interface{}, error) { + return golangsdk.BuildRequestBody(opts, "") +} + +// Create will create a new cc attack protection rule based on the values in CreateOpts. +func Create(c *golangsdk.ServiceClient, policyID string, opts CreateOptsBuilder) (r CreateResult) { + b, err := opts.ToCcAttackCreateMap() + if err != nil { + r.Err = err + return + } + reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}} + _, r.Err = c.Post(rootURL(c, policyID), b, &r.Body, reqOpt) + return +} + +// Get retrieves a particular cc attack rule based on its unique ID. +func Get(c *golangsdk.ServiceClient, policyID, ruleID string) (r GetResult) { + _, r.Err = c.Get(resourceURL(c, policyID, ruleID), &r.Body, &RequestOpts) + return +} + +// Delete will permanently delete a particular cc attack rule based on its unique ID. +func Delete(c *golangsdk.ServiceClient, policyID, ruleID string) (r DeleteResult) { + reqOpt := &golangsdk.RequestOpts{OkCodes: []int{204}, + MoreHeaders: RequestOpts.MoreHeaders} + _, r.Err = c.Delete(resourceURL(c, policyID, ruleID), reqOpt) + return +} diff --git a/openstack/waf/v1/ccattackprotection_rules/results.go b/openstack/waf/v1/ccattackprotection_rules/results.go new file mode 100644 index 000000000..630ca00da --- /dev/null +++ b/openstack/waf/v1/ccattackprotection_rules/results.go @@ -0,0 +1,48 @@ +package ccattackprotection_rules + +import ( + "github.com/huaweicloud/golangsdk" +) + +type CcAttack struct { + Id string `json:"id"` + PolicyID string `json:"policyid"` + Url string `json:"url"` + LimitNum int `json:"limit_num"` + LimitPeriod int `json:"limit_period"` + LockTime int `json:"lock_time"` + TagType string `json:"tag_type"` + TagIndex string `json:"tag_index"` + TagCondition TagCondition `json:"tag_condition"` + Action Action `json:"action"` + Default bool `json:"default"` +} + +type commonResult struct { + golangsdk.Result +} + +// Extract is a function that accepts a result and extracts a cc attack protection rule. +func (r commonResult) Extract() (*CcAttack, error) { + var response CcAttack + err := r.ExtractInto(&response) + return &response, err +} + +// CreateResult represents the result of a create operation. Call its Extract +// method to interpret it as a cc attack protection rule. +type CreateResult struct { + commonResult +} + +// GetResult represents the result of a get operation. Call its Extract +// method to interpret it as a cc attack protection rule. +type GetResult struct { + commonResult +} + +// DeleteResult represents the result of a delete operation. Call its ExtractErr +// method to determine if the request succeeded or failed. +type DeleteResult struct { + golangsdk.ErrResult +} diff --git a/openstack/waf/v1/ccattackprotection_rules/urls.go b/openstack/waf/v1/ccattackprotection_rules/urls.go new file mode 100644 index 000000000..672fbb8b8 --- /dev/null +++ b/openstack/waf/v1/ccattackprotection_rules/urls.go @@ -0,0 +1,11 @@ +package ccattackprotection_rules + +import "github.com/huaweicloud/golangsdk" + +func rootURL(c *golangsdk.ServiceClient, policy_id string) string { + return c.ServiceURL("policy", policy_id, "cc") +} + +func resourceURL(c *golangsdk.ServiceClient, policy_id, id string) string { + return c.ServiceURL("policy", policy_id, "cc", id) +}