From 7b2bce52db0a50f724614758cf3aa1815312675b Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Mon, 24 Jun 2019 07:17:21 +0000 Subject: [PATCH 1/2] Add WhiteBlack IP rule --- .../waf/v1/whiteblackip_rules/requests.go | 81 +++++++++++++++++++ .../waf/v1/whiteblackip_rules/results.go | 51 ++++++++++++ openstack/waf/v1/whiteblackip_rules/urls.go | 11 +++ 3 files changed, 143 insertions(+) create mode 100644 openstack/waf/v1/whiteblackip_rules/requests.go create mode 100644 openstack/waf/v1/whiteblackip_rules/results.go create mode 100644 openstack/waf/v1/whiteblackip_rules/urls.go diff --git a/openstack/waf/v1/whiteblackip_rules/requests.go b/openstack/waf/v1/whiteblackip_rules/requests.go new file mode 100644 index 000000000..f5993c794 --- /dev/null +++ b/openstack/waf/v1/whiteblackip_rules/requests.go @@ -0,0 +1,81 @@ +package whiteblackip_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 { + ToWhiteBlackIPCreateMap() (map[string]interface{}, error) +} + +// CreateOpts contains all the values needed to create a new whiteblackip rule. +type CreateOpts struct { + Addr string `json:"addr" required:"true"` + White int `json:"white,omitempty"` +} + +// ToWhiteBlackIPCreateMap builds a create request body from CreateOpts. +func (opts CreateOpts) ToWhiteBlackIPCreateMap() (map[string]interface{}, error) { + return golangsdk.BuildRequestBody(opts, "") +} + +// Create will create a new whiteblackip rule based on the values in CreateOpts. +func Create(c *golangsdk.ServiceClient, policyID string, opts CreateOptsBuilder) (r CreateResult) { + b, err := opts.ToWhiteBlackIPCreateMap() + 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 +} + +// UpdateOptsBuilder allows extensions to add additional parameters to the +// Update request. +type UpdateOptsBuilder interface { + ToWhiteBlackIPUpdateMap() (map[string]interface{}, error) +} + +// UpdateOpts contains all the values needed to update a whiteblackip rule. +type UpdateOpts struct { + Addr string `json:"addr" required:"true"` + White *int `json:"white" required:"true"` +} + +// ToWhiteBlackIPUpdateMap builds a update request body from UpdateOpts. +func (opts UpdateOpts) ToWhiteBlackIPUpdateMap() (map[string]interface{}, error) { + return golangsdk.BuildRequestBody(opts, "") +} + +// Update accepts a UpdateOpts struct and uses the values to update a rule.The response code from api is 200 +func Update(c *golangsdk.ServiceClient, policyID, ruleID string, opts UpdateOptsBuilder) (r UpdateResult) { + b, err := opts.ToWhiteBlackIPUpdateMap() + if err != nil { + r.Err = err + return + } + reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}} + _, r.Err = c.Put(resourceURL(c, policyID, ruleID), b, nil, reqOpt) + return +} + +// Get retrieves a particular whiteblackip 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 whiteblackip 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/whiteblackip_rules/results.go b/openstack/waf/v1/whiteblackip_rules/results.go new file mode 100644 index 000000000..62e7c8454 --- /dev/null +++ b/openstack/waf/v1/whiteblackip_rules/results.go @@ -0,0 +1,51 @@ +package whiteblackip_rules + +import ( + "github.com/huaweicloud/golangsdk" +) + +type WhiteBlackIP struct { + //WhiteBlackIP Rule ID + Id string `json:"id"` + //WhiteBlackIP Rule Addr + Addr string `json:"addr"` + //IP address type + White int `json:"white"` + //Policy ID + PolicyID string `json:"policyid"` +} + +type commonResult struct { + golangsdk.Result +} + +// Extract is a function that accepts a result and extracts a whiteblackip rule. +func (r commonResult) Extract() (*WhiteBlackIP, error) { + var response WhiteBlackIP + err := r.ExtractInto(&response) + return &response, err +} + +// CreateResult represents the result of a create operation. Call its Extract +// method to interpret it as a WhiteBlackIP rule. +type CreateResult struct { + commonResult +} + +// UpdateResult represents the result of a update operation. Call its Extract +// method to interpret it as a WhiteBlackIP rule. +type UpdateResult struct { + commonResult +} + +// GetResult represents the result of a get operation. Call its Extract +// method to interpret it as a WhiteBlackIP 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/whiteblackip_rules/urls.go b/openstack/waf/v1/whiteblackip_rules/urls.go new file mode 100644 index 000000000..1ee87fcdc --- /dev/null +++ b/openstack/waf/v1/whiteblackip_rules/urls.go @@ -0,0 +1,11 @@ +package whiteblackip_rules + +import "github.com/huaweicloud/golangsdk" + +func rootURL(c *golangsdk.ServiceClient, policy_id string) string { + return c.ServiceURL("policy", policy_id, "whiteblackip") +} + +func resourceURL(c *golangsdk.ServiceClient, policy_id, id string) string { + return c.ServiceURL("policy", policy_id, "whiteblackip", id) +} From 3f6a50d2f97e4fc64022dbbf66999d5f45017e97 Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Tue, 25 Jun 2019 06:48:53 +0000 Subject: [PATCH 2/2] Format code --- openstack/waf/v1/whiteblackip_rules/requests.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/openstack/waf/v1/whiteblackip_rules/requests.go b/openstack/waf/v1/whiteblackip_rules/requests.go index f5993c794..e5dcd54d5 100644 --- a/openstack/waf/v1/whiteblackip_rules/requests.go +++ b/openstack/waf/v1/whiteblackip_rules/requests.go @@ -16,8 +16,8 @@ type CreateOptsBuilder interface { // CreateOpts contains all the values needed to create a new whiteblackip rule. type CreateOpts struct { - Addr string `json:"addr" required:"true"` - White int `json:"white,omitempty"` + Addr string `json:"addr" required:"true"` + White int `json:"white,omitempty"` } // ToWhiteBlackIPCreateMap builds a create request body from CreateOpts. @@ -45,8 +45,8 @@ type UpdateOptsBuilder interface { // UpdateOpts contains all the values needed to update a whiteblackip rule. type UpdateOpts struct { - Addr string `json:"addr" required:"true"` - White *int `json:"white" required:"true"` + Addr string `json:"addr" required:"true"` + White *int `json:"white" required:"true"` } // ToWhiteBlackIPUpdateMap builds a update request body from UpdateOpts.