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

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