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

     1  /*
     2   Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
     3  */
     4  
     5  package valuelists
     6  
     7  import "github.com/huaweicloud/golangsdk"
     8  
     9  var requestOpts = golangsdk.RequestOpts{
    10  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    11  }
    12  
    13  // CreateOpts the options to create reference table.
    14  type CreateOpts struct {
    15  	Name        string   `json:"name" required:"true"`
    16  	Type        string   `json:"type" required:"true"`
    17  	Values      []string `json:"values,omitempty"`
    18  	Description string   `json:"description,omitempty"`
    19  }
    20  
    21  // Create a reference table.
    22  func Create(c *golangsdk.ServiceClient, opts CreateOpts) (*WafValueList, error) {
    23  	b, err := golangsdk.BuildRequestBody(opts, "")
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  
    28  	var rst golangsdk.Result
    29  	_, err = c.Post(rootURL(c), b, &rst.Body, &golangsdk.RequestOpts{
    30  		OkCodes:     []int{200},
    31  		MoreHeaders: requestOpts.MoreHeaders,
    32  	})
    33  	if err == nil {
    34  		var r WafValueList
    35  		rst.ExtractInto(&r)
    36  		return &r, nil
    37  	}
    38  	return nil, err
    39  }
    40  
    41  //UpdateValueListOpts the options to update reference table.
    42  type UpdateValueListOpts struct {
    43  	Name        string   `json:"name" required:"true"`
    44  	Values      []string `json:"values,omitempty"`
    45  	Type        string   `json:"type,omitempty"`
    46  	Description *string  `json:"description,omitempty"`
    47  }
    48  
    49  // Update reference table according options and id.
    50  func Update(c *golangsdk.ServiceClient, id string, opts UpdateValueListOpts) (*WafValueList, error) {
    51  	b, err := golangsdk.BuildRequestBody(opts, "")
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	var rst golangsdk.Result
    57  	_, err = c.Put(resourceURL(c, id), b, &rst.Body, &golangsdk.RequestOpts{
    58  		OkCodes:     []int{200},
    59  		MoreHeaders: requestOpts.MoreHeaders,
    60  	})
    61  	if err == nil {
    62  		var r WafValueList
    63  		rst.ExtractInto(&r)
    64  		return &r, nil
    65  	}
    66  	return nil, err
    67  }
    68  
    69  // Get a reference table by id.
    70  func Get(c *golangsdk.ServiceClient, id string) (*WafValueList, error) {
    71  	var rst golangsdk.Result
    72  	_, err := c.Get(resourceURL(c, id), &rst.Body, &golangsdk.RequestOpts{
    73  		OkCodes:     []int{200},
    74  		MoreHeaders: requestOpts.MoreHeaders,
    75  	})
    76  	if err == nil {
    77  		var r WafValueList
    78  		rst.ExtractInto(&r)
    79  		return &r, nil
    80  	}
    81  	return nil, err
    82  }
    83  
    84  // ListValueListOpts the options to query a list of reference tables.
    85  type ListValueListOpts struct {
    86  	Page     int `q:"page"`
    87  	Pagesize int `q:"pagesize"`
    88  }
    89  
    90  // List : Query a list of reference tables according to the options.
    91  func List(c *golangsdk.ServiceClient, opts ListValueListOpts) (*ListValueListRst, error) {
    92  	url := rootURL(c)
    93  	query, err := golangsdk.BuildQueryString(opts)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  	url += query.String()
    98  
    99  	var rst golangsdk.Result
   100  	_, err = c.Get(url, &rst.Body, &golangsdk.RequestOpts{
   101  		OkCodes:     []int{200},
   102  		MoreHeaders: requestOpts.MoreHeaders,
   103  	})
   104  	if err == nil {
   105  		var r ListValueListRst
   106  		rst.ExtractInto(&r)
   107  		return &r, nil
   108  	}
   109  	return nil, err
   110  }
   111  
   112  // Delete reference table by id.
   113  func Delete(c *golangsdk.ServiceClient, id string) (*WafValueList, error) {
   114  	var rst golangsdk.Result
   115  	_, err := c.DeleteWithResponse(resourceURL(c, id), &rst.Body, &golangsdk.RequestOpts{
   116  		OkCodes:     []int{200},
   117  		MoreHeaders: requestOpts.MoreHeaders,
   118  	})
   119  	if err == nil {
   120  		var r WafValueList
   121  		rst.ExtractInto(&r)
   122  		return &r, nil
   123  	}
   124  	return nil, err
   125  }