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

     1  package policies
     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  	ToPolicyCreateMap() (map[string]interface{}, error)
    15  }
    16  
    17  // CreateOpts contains all the values needed to create a new policy.
    18  type CreateOpts struct {
    19  	//Policy name
    20  	Name string `json:"name" required:"true"`
    21  }
    22  
    23  // ToPolicyCreateMap builds a create request body from CreateOpts.
    24  func (opts CreateOpts) ToPolicyCreateMap() (map[string]interface{}, error) {
    25  	return golangsdk.BuildRequestBody(opts, "")
    26  }
    27  
    28  // Create will create a new policy based on the values in CreateOpts.
    29  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    30  	b, err := opts.ToPolicyCreateMap()
    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), 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  	ToPolicyUpdateMap() (map[string]interface{}, error)
    44  }
    45  
    46  // UpdateOpts contains all the values needed to update a policy.
    47  type UpdateOpts struct {
    48  	Name          string        `json:"name,omitempty"`
    49  	Action        *Action       `json:"action,omitempty"`
    50  	Options       *PolicyOption `json:"options,omitempty"`
    51  	Level         int           `json:"level,omitempty"`
    52  	FullDetection *bool         `json:"full_detection,omitempty"`
    53  }
    54  
    55  // ToPolicyUpdateMap builds a update request body from UpdateOpts.
    56  func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) {
    57  	return golangsdk.BuildRequestBody(opts, "")
    58  }
    59  
    60  // Update accepts a UpdateOpts struct and uses the values to update a policy.The response code from api is 200
    61  func Update(c *golangsdk.ServiceClient, policyID string, opts UpdateOptsBuilder) (r UpdateResult) {
    62  	b, err := opts.ToPolicyUpdateMap()
    63  	if err != nil {
    64  		r.Err = err
    65  		return
    66  	}
    67  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
    68  	_, r.Err = c.Patch(resourceURL(c, policyID), b, nil, reqOpt)
    69  	return
    70  }
    71  
    72  // UpdateHostsOptsBuilder allows extensions to add additional parameters to the
    73  // Update request.
    74  type UpdateHostsOptsBuilder interface {
    75  	ToUpdateHostsQuery() (string, error)
    76  }
    77  
    78  // UpdateHostsOpts contains all the values needed to update a policy hosts.
    79  type UpdateHostsOpts struct {
    80  	//Domain ID
    81  	Hosts []string `q:"hosts" required:"true"`
    82  }
    83  
    84  // ListPolicyOpts
    85  type ListPolicyOpts struct {
    86  	Page     int `q:"page"`
    87  	Pagesize int `q:"pagesize"`
    88  	// policy name
    89  	Name string `q:"name"`
    90  }
    91  
    92  // ToUpdateHostsQuery builds a update request query from UpdateHostsOpts.
    93  func (opts UpdateHostsOpts) ToUpdateHostsQuery() (string, error) {
    94  	q, err := golangsdk.BuildQueryString(opts)
    95  	return q.String(), err
    96  }
    97  
    98  // UpdateHosts accepts a UpdateHostsOpts struct and uses the values to update a policy hosts.The response code from api is 200
    99  func UpdateHosts(c *golangsdk.ServiceClient, policyId string, opts UpdateHostsOptsBuilder) (r UpdateResult) {
   100  	url := resourceURL(c, policyId)
   101  	if opts != nil {
   102  		var query string
   103  		query, r.Err = opts.ToUpdateHostsQuery()
   104  		if r.Err != nil {
   105  			return
   106  		}
   107  		url += query
   108  	}
   109  	reqOpt := &golangsdk.RequestOpts{
   110  		OkCodes:     []int{200},
   111  		MoreHeaders: RequestOpts.MoreHeaders,
   112  	}
   113  	_, r.Err = c.Put(url, nil, r.Body, reqOpt)
   114  	return
   115  }
   116  
   117  // Get retrieves a particular policy based on its unique ID.
   118  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   119  	reqOpt := &golangsdk.RequestOpts{
   120  		OkCodes:     []int{200},
   121  		MoreHeaders: RequestOpts.MoreHeaders,
   122  	}
   123  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, reqOpt)
   124  	return
   125  }
   126  
   127  // Delete will permanently delete a particular policy based on its unique ID.
   128  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   129  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200},
   130  		MoreHeaders: RequestOpts.MoreHeaders}
   131  	_, r.Err = c.Delete(resourceURL(c, id), reqOpt)
   132  	return
   133  }
   134  
   135  // ListPolicy retrieve waf policy by ListPolicyOpts
   136  func ListPolicy(c *golangsdk.ServiceClient, opts ListPolicyOpts) (*ListPolicyRst, error) {
   137  	url := rootURL(c)
   138  	query, err := golangsdk.BuildQueryString(opts)
   139  	if err != nil {
   140  		return nil, err
   141  	}
   142  	url += query.String()
   143  
   144  	var rst golangsdk.Result
   145  	_, err = c.Get(url, &rst.Body, &golangsdk.RequestOpts{
   146  		MoreHeaders: RequestOpts.MoreHeaders,
   147  	})
   148  
   149  	if err == nil {
   150  		var r ListPolicyRst
   151  		rst.ExtractInto(&r)
   152  		return &r, nil
   153  	}
   154  	return nil, err
   155  }