github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/fwaas/policies/requests.go (about)

     1  package policies
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  // ListOptsBuilder allows extensions to add additional parameters to the
     9  // List request.
    10  type ListOptsBuilder interface {
    11  	ToPolicyListQuery() (string, error)
    12  }
    13  
    14  // ListOpts allows the filtering and sorting of paginated collections through
    15  // the API. Filtering is achieved by passing in struct field values that map to
    16  // the firewall policy attributes you want to see returned. SortKey allows you
    17  // to sort by a particular firewall policy attribute. SortDir sets the direction,
    18  // and is either `asc' or `desc'. Marker and Limit are used for pagination.
    19  type ListOpts struct {
    20  	TenantID    string `q:"tenant_id"`
    21  	ProjectID   string `q:"project_id"`
    22  	Name        string `q:"name"`
    23  	Description string `q:"description"`
    24  	Shared      *bool  `q:"shared"`
    25  	Audited     *bool  `q:"audited"`
    26  	ID          string `q:"id"`
    27  	Limit       int    `q:"limit"`
    28  	Marker      string `q:"marker"`
    29  	SortKey     string `q:"sort_key"`
    30  	SortDir     string `q:"sort_dir"`
    31  }
    32  
    33  // ToPolicyListQuery formats a ListOpts into a query string.
    34  func (opts ListOpts) ToPolicyListQuery() (string, error) {
    35  	q, err := golangsdk.BuildQueryString(opts)
    36  	return q.String(), err
    37  }
    38  
    39  // List returns a Pager which allows you to iterate over a collection of
    40  // firewall policies. It accepts a ListOpts struct, which allows you to filter
    41  // and sort the returned collection for greater efficiency.
    42  //
    43  // Default policy settings return only those firewall policies that are owned by
    44  // the tenant who submits the request, unless an admin user submits the request.
    45  func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    46  	url := rootURL(c)
    47  	if opts != nil {
    48  		query, err := opts.ToPolicyListQuery()
    49  		if err != nil {
    50  			return pagination.Pager{Err: err}
    51  		}
    52  		url += query
    53  	}
    54  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    55  		return PolicyPage{pagination.LinkedPageBase{PageResult: r}}
    56  	})
    57  }
    58  
    59  // CreateOptsBuilder allows extensions to add additional parameters to the
    60  // Create request.
    61  type CreateOptsBuilder interface {
    62  	ToFirewallPolicyCreateMap() (map[string]interface{}, error)
    63  }
    64  
    65  // CreateOpts contains all the values needed to create a new firewall policy.
    66  type CreateOpts struct {
    67  	// TenantID specifies a tenant to own the firewall. The caller must have
    68  	// an admin role in order to set this. Otherwise, this field is left unset
    69  	// and the caller will be the owner.
    70  	TenantID    string   `json:"tenant_id,omitempty"`
    71  	ProjectID   string   `json:"project_id,omitempty"`
    72  	Name        string   `json:"name,omitempty"`
    73  	Description string   `json:"description,omitempty"`
    74  	Shared      *bool    `json:"shared,omitempty"`
    75  	Audited     *bool    `json:"audited,omitempty"`
    76  	Rules       []string `json:"firewall_rules,omitempty"`
    77  }
    78  
    79  // ToFirewallPolicyCreateMap casts a CreateOpts struct to a map.
    80  func (opts CreateOpts) ToFirewallPolicyCreateMap() (map[string]interface{}, error) {
    81  	return golangsdk.BuildRequestBody(opts, "firewall_policy")
    82  }
    83  
    84  // Create accepts a CreateOpts struct and uses the values to create a new
    85  // firewall policy.
    86  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    87  	b, err := opts.ToFirewallPolicyCreateMap()
    88  	if err != nil {
    89  		r.Err = err
    90  		return
    91  	}
    92  	_, r.Err = c.Post(rootURL(c), b, &r.Body, nil)
    93  	return
    94  }
    95  
    96  // Get retrieves a particular firewall policy based on its unique ID.
    97  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
    98  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
    99  	return
   100  }
   101  
   102  // UpdateOptsBuilder allows extensions to add additional parameters to the
   103  // Update request.
   104  type UpdateOptsBuilder interface {
   105  	ToFirewallPolicyUpdateMap() (map[string]interface{}, error)
   106  }
   107  
   108  // UpdateOpts contains the values used when updating a firewall policy.
   109  type UpdateOpts struct {
   110  	Name        string   `json:"name,omitempty"`
   111  	Description string   `json:"description,omitempty"`
   112  	Shared      *bool    `json:"shared,omitempty"`
   113  	Audited     *bool    `json:"audited,omitempty"`
   114  	Rules       []string `json:"firewall_rules,omitempty"`
   115  }
   116  
   117  // ToFirewallPolicyUpdateMap casts a CreateOpts struct to a map.
   118  func (opts UpdateOpts) ToFirewallPolicyUpdateMap() (map[string]interface{}, error) {
   119  	return golangsdk.BuildRequestBody(opts, "firewall_policy")
   120  }
   121  
   122  // Update allows firewall policies to be updated.
   123  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   124  	b, err := opts.ToFirewallPolicyUpdateMap()
   125  	if err != nil {
   126  		r.Err = err
   127  		return
   128  	}
   129  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   130  		OkCodes: []int{200},
   131  	})
   132  	return
   133  }
   134  
   135  // Delete will permanently delete a particular firewall policy based on its
   136  // unique ID.
   137  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   138  	_, r.Err = c.Delete(resourceURL(c, id), nil)
   139  	return
   140  }
   141  
   142  // InsertRuleOptsBuilder allows extensions to add additional parameters to the
   143  // InsertRule request.
   144  type InsertRuleOptsBuilder interface {
   145  	ToFirewallPolicyInsertRuleMap() (map[string]interface{}, error)
   146  }
   147  
   148  // InsertRuleOpts contains the values used when updating a policy's rules.
   149  type InsertRuleOpts struct {
   150  	ID           string `json:"firewall_rule_id" required:"true"`
   151  	BeforeRuleID string `json:"insert_before,omitempty"`
   152  	AfterRuleID  string `json:"insert_after,omitempty"`
   153  }
   154  
   155  func (opts InsertRuleOpts) ToFirewallPolicyInsertRuleMap() (map[string]interface{}, error) {
   156  	return golangsdk.BuildRequestBody(opts, "")
   157  }
   158  
   159  // AddRule will add a rule to a policy.
   160  func AddRule(c *golangsdk.ServiceClient, id string, opts InsertRuleOptsBuilder) (r InsertRuleResult) {
   161  	b, err := opts.ToFirewallPolicyInsertRuleMap()
   162  	if err != nil {
   163  		r.Err = err
   164  		return
   165  	}
   166  	_, r.Err = c.Put(insertURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   167  		OkCodes: []int{200},
   168  	})
   169  	return
   170  }
   171  
   172  // RemoveRule will add a rule to a policy.
   173  func RemoveRule(c *golangsdk.ServiceClient, id, ruleID string) (r RemoveRuleResult) {
   174  	b := map[string]interface{}{"firewall_rule_id": ruleID}
   175  	_, r.Err = c.Put(removeURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   176  		OkCodes: []int{200},
   177  	})
   178  	return
   179  }