github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/waf_hw/v1/datamasking_rules/requests.go (about)

     1  /*
     2   Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
     3  */
     4  
     5  package datamasking_rules
     6  
     7  import (
     8  	"github.com/chnsz/golangsdk"
     9  	"github.com/chnsz/golangsdk/openstack/utils"
    10  )
    11  
    12  var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{
    13  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    14  }
    15  
    16  // CreateOptsBuilder allows extensions to add additional parameters to the
    17  // Create request.
    18  type CreateOptsBuilder interface {
    19  	ToDataMaskingCreateMap() (map[string]interface{}, error)
    20  }
    21  
    22  // CreateOpts contains all the values needed to create a new datamasking rule.
    23  type CreateOpts struct {
    24  	Path                string `json:"url" required:"true"`
    25  	Category            string `json:"category" required:"true"`
    26  	Index               string `json:"index" required:"true"`
    27  	Description         string `json:"description,omitempty"`
    28  	EnterpriseProjectId string `q:"enterprise_project_id" json:"-"`
    29  }
    30  
    31  // ToDataMaskingCreateMap builds a create request body from CreateOpts.
    32  func (opts CreateOpts) ToDataMaskingCreateMap() (map[string]interface{}, error) {
    33  	return golangsdk.BuildRequestBody(opts, "")
    34  }
    35  
    36  // Create will create a new datamasking rule based on the values in CreateOpts.
    37  func Create(c *golangsdk.ServiceClient, policyID string, opts CreateOptsBuilder) (r CreateResult) {
    38  	b, err := opts.ToDataMaskingCreateMap()
    39  	if err != nil {
    40  		r.Err = err
    41  		return
    42  	}
    43  	query, err := golangsdk.BuildQueryString(opts)
    44  	if err != nil {
    45  		r.Err = err
    46  		return
    47  	}
    48  
    49  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
    50  	_, r.Err = c.Post(rootURL(c, policyID)+query.String(), b, &r.Body, reqOpt)
    51  	return
    52  }
    53  
    54  // UpdateOptsBuilder allows extensions to add additional parameters to the
    55  // Update request.
    56  type UpdateOptsBuilder interface {
    57  	ToDataMaskingUpdateMap() (map[string]interface{}, error)
    58  }
    59  
    60  // UpdateOpts contains all the values needed to update a datamasking rule.
    61  type UpdateOpts struct {
    62  	Path                string `json:"url" required:"true"`
    63  	Category            string `json:"category" required:"true"`
    64  	Index               string `json:"index" required:"true"`
    65  	Description         string `json:"description,omitempty"`
    66  	EnterpriseProjectId string `q:"enterprise_project_id" json:"-"`
    67  }
    68  
    69  // ToDataMaskingUpdateMap builds a update request body from UpdateOpts.
    70  func (opts UpdateOpts) ToDataMaskingUpdateMap() (map[string]interface{}, error) {
    71  	return golangsdk.BuildRequestBody(opts, "")
    72  }
    73  
    74  // Update accepts a UpdateOpts struct and uses the values to update a rule.The response code from api is 200
    75  func Update(c *golangsdk.ServiceClient, policyID, ruleID string, opts UpdateOptsBuilder) (r UpdateResult) {
    76  	b, err := opts.ToDataMaskingUpdateMap()
    77  	if err != nil {
    78  		r.Err = err
    79  		return
    80  	}
    81  	query, err := golangsdk.BuildQueryString(opts)
    82  	if err != nil {
    83  		r.Err = err
    84  		return
    85  	}
    86  
    87  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
    88  	_, r.Err = c.Put(resourceURL(c, policyID, ruleID)+query.String(), b, nil, reqOpt)
    89  	return
    90  }
    91  
    92  // Get retrieves a particular datamasking rule based on its unique ID.
    93  func Get(c *golangsdk.ServiceClient, policyID, ruleID string) (r GetResult) {
    94  	return GetWithEpsID(c, policyID, ruleID, "")
    95  }
    96  
    97  func GetWithEpsID(c *golangsdk.ServiceClient, policyID, ruleID, epsID string) (r GetResult) {
    98  	reqOpt := &golangsdk.RequestOpts{
    99  		MoreHeaders: RequestOpts.MoreHeaders,
   100  	}
   101  
   102  	_, r.Err = c.Get(resourceURL(c, policyID, ruleID)+utils.GenerateEpsIDQuery(epsID), &r.Body, reqOpt)
   103  	return
   104  }
   105  
   106  // Delete will permanently delete a particular datamasking rule based on its unique ID.
   107  func Delete(c *golangsdk.ServiceClient, policyID, ruleID string) (r DeleteResult) {
   108  	return DeleteWithEpsID(c, policyID, ruleID, "")
   109  }
   110  
   111  func DeleteWithEpsID(c *golangsdk.ServiceClient, policyID, ruleID, epsID string) (r DeleteResult) {
   112  	reqOpt := &golangsdk.RequestOpts{
   113  		OkCodes:     []int{200},
   114  		MoreHeaders: RequestOpts.MoreHeaders,
   115  	}
   116  	_, r.Err = c.Delete(resourceURL(c, policyID, ruleID)+utils.GenerateEpsIDQuery(epsID), reqOpt)
   117  	return
   118  }