Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions openstack/waf/v1/whiteblackip_rules/requests.go
Original file line number Diff line number Diff line change
@@ -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
}
51 changes: 51 additions & 0 deletions openstack/waf/v1/whiteblackip_rules/results.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions openstack/waf/v1/whiteblackip_rules/urls.go
Original file line number Diff line number Diff line change
@@ -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)
}