github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/waf/v1/whiteblackip_rules/requests.go (about)

     1  package whiteblackip_rules
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  )
     6  
     7  var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{
     8  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
     9  }
    10  
    11  // CreateOptsBuilder allows extensions to add additional parameters to the
    12  // Create request.
    13  type CreateOptsBuilder interface {
    14  	ToWhiteBlackIPCreateMap() (map[string]interface{}, error)
    15  }
    16  
    17  // CreateOpts contains all the values needed to create a new whiteblackip rule.
    18  type CreateOpts struct {
    19  	Addr  string `json:"addr" required:"true"`
    20  	White int    `json:"white,omitempty"`
    21  }
    22  
    23  // ToWhiteBlackIPCreateMap builds a create request body from CreateOpts.
    24  func (opts CreateOpts) ToWhiteBlackIPCreateMap() (map[string]interface{}, error) {
    25  	return golangsdk.BuildRequestBody(opts, "")
    26  }
    27  
    28  // Create will create a new whiteblackip rule based on the values in CreateOpts.
    29  func Create(c *golangsdk.ServiceClient, policyID string, opts CreateOptsBuilder) (r CreateResult) {
    30  	b, err := opts.ToWhiteBlackIPCreateMap()
    31  	if err != nil {
    32  		r.Err = err
    33  		return
    34  	}
    35  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
    36  	_, r.Err = c.Post(rootURL(c, policyID), b, &r.Body, reqOpt)
    37  	return
    38  }
    39  
    40  // UpdateOptsBuilder allows extensions to add additional parameters to the
    41  // Update request.
    42  type UpdateOptsBuilder interface {
    43  	ToWhiteBlackIPUpdateMap() (map[string]interface{}, error)
    44  }
    45  
    46  // UpdateOpts contains all the values needed to update a whiteblackip rule.
    47  type UpdateOpts struct {
    48  	Addr  string `json:"addr" required:"true"`
    49  	White *int   `json:"white" required:"true"`
    50  }
    51  
    52  // ToWhiteBlackIPUpdateMap builds a update request body from UpdateOpts.
    53  func (opts UpdateOpts) ToWhiteBlackIPUpdateMap() (map[string]interface{}, error) {
    54  	return golangsdk.BuildRequestBody(opts, "")
    55  }
    56  
    57  // Update accepts a UpdateOpts struct and uses the values to update a rule.The response code from api is 200
    58  func Update(c *golangsdk.ServiceClient, policyID, ruleID string, opts UpdateOptsBuilder) (r UpdateResult) {
    59  	b, err := opts.ToWhiteBlackIPUpdateMap()
    60  	if err != nil {
    61  		r.Err = err
    62  		return
    63  	}
    64  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
    65  	_, r.Err = c.Put(resourceURL(c, policyID, ruleID), b, nil, reqOpt)
    66  	return
    67  }
    68  
    69  // Get retrieves a particular whiteblackip rule based on its unique ID.
    70  func Get(c *golangsdk.ServiceClient, policyID, ruleID string) (r GetResult) {
    71  	_, r.Err = c.Get(resourceURL(c, policyID, ruleID), &r.Body, &golangsdk.RequestOpts{
    72  		MoreHeaders: RequestOpts.MoreHeaders,
    73  	})
    74  	return
    75  }
    76  
    77  // Delete will permanently delete a particular whiteblackip rule based on its unique ID.
    78  func Delete(c *golangsdk.ServiceClient, policyID, ruleID string) (r DeleteResult) {
    79  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{204},
    80  		MoreHeaders: RequestOpts.MoreHeaders}
    81  	_, r.Err = c.Delete(resourceURL(c, policyID, ruleID), reqOpt)
    82  	return
    83  }