github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/waf/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  	//Policy name
    49  	Name string `json:"name,omitempty"`
    50  	//Protective Action
    51  	Action *Action `json:"action,omitempty"`
    52  	//Protection Switches
    53  	Options *Options `json:"options,omitempty"`
    54  	//Protection Level
    55  	Level int `json:"level,omitempty"`
    56  	//Detection Mode
    57  	FullDetection *bool `json:"full_detection,omitempty"`
    58  }
    59  
    60  // ToPolicyUpdateMap builds a update request body from UpdateOpts.
    61  func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) {
    62  	return golangsdk.BuildRequestBody(opts, "")
    63  }
    64  
    65  // Update accepts a UpdateOpts struct and uses the values to update a policy.The response code from api is 200
    66  func Update(c *golangsdk.ServiceClient, policyID string, opts UpdateOptsBuilder) (r UpdateResult) {
    67  	b, err := opts.ToPolicyUpdateMap()
    68  	if err != nil {
    69  		r.Err = err
    70  		return
    71  	}
    72  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
    73  	_, r.Err = c.Put(resourceURL(c, policyID), b, nil, reqOpt)
    74  	return
    75  }
    76  
    77  // UpdateHostsOptsBuilder allows extensions to add additional parameters to the
    78  // Update request.
    79  type UpdateHostsOptsBuilder interface {
    80  	ToPolicyHostsUpdateMap() (map[string]interface{}, error)
    81  }
    82  
    83  // UpdateHostsOpts contains all the values needed to update a policy hosts.
    84  type UpdateHostsOpts struct {
    85  	//Domain IDs
    86  	Hosts []string `json:"hosts" required:"true"`
    87  }
    88  
    89  // ToPolicyHostsUpdateMap builds a update request body from UpdateHostsOpts.
    90  func (opts UpdateHostsOpts) ToPolicyHostsUpdateMap() (map[string]interface{}, error) {
    91  	return golangsdk.BuildRequestBody(opts, "")
    92  }
    93  
    94  // Update accepts a UpdateHostsOpts struct and uses the values to update a policy hosts.The response code from api is 200
    95  func UpdateHosts(c *golangsdk.ServiceClient, policyID string, opts UpdateHostsOptsBuilder) (r UpdateResult) {
    96  	b, err := opts.ToPolicyHostsUpdateMap()
    97  	if err != nil {
    98  		r.Err = err
    99  		return
   100  	}
   101  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
   102  	_, r.Err = c.Put(hostsURL(c, policyID), b, nil, reqOpt)
   103  	return
   104  }
   105  
   106  // Get retrieves a particular policy based on its unique ID.
   107  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   108  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, &golangsdk.RequestOpts{
   109  		MoreHeaders: RequestOpts.MoreHeaders,
   110  	})
   111  	return
   112  }
   113  
   114  // Delete will permanently delete a particular policy based on its unique ID.
   115  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   116  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{204},
   117  		MoreHeaders: RequestOpts.MoreHeaders}
   118  	_, r.Err = c.Delete(resourceURL(c, id), reqOpt)
   119  	return
   120  }