github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/fwaas_v2/policies/requests.go (about)

     1  package policies
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/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 := gophercloud.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 the
    44  // tenant who submits the request, unless an admin user submits the request.
    45  func List(c *gophercloud.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 is the interface options structs have to satisfy in order
    60  // to be used in the main Create operation in this package. Since many
    61  // extensions decorate or modify the common logic, it is useful for them to
    62  // satisfy a basic interface in order for them to be used.
    63  type CreateOptsBuilder interface {
    64  	ToFirewallPolicyCreateMap() (map[string]interface{}, error)
    65  }
    66  
    67  // CreateOpts contains all the values needed to create a new firewall policy.
    68  type CreateOpts struct {
    69  	// Only required if the caller has an admin role and wants to create a firewall policy
    70  	// for another tenant.
    71  	TenantID      string   `json:"tenant_id,omitempty"`
    72  	ProjectID     string   `json:"project_id,omitempty"`
    73  	Name          string   `json:"name,omitempty"`
    74  	Description   string   `json:"description,omitempty"`
    75  	Shared        *bool    `json:"shared,omitempty"`
    76  	Audited       *bool    `json:"audited,omitempty"`
    77  	FirewallRules []string `json:"firewall_rules,omitempty"`
    78  }
    79  
    80  // ToFirewallPolicyCreateMap casts a CreateOpts struct to a map.
    81  func (opts CreateOpts) ToFirewallPolicyCreateMap() (map[string]interface{}, error) {
    82  	return gophercloud.BuildRequestBody(opts, "firewall_policy")
    83  }
    84  
    85  // Create accepts a CreateOpts struct and uses the values to create a new firewall policy
    86  func Create(c *gophercloud.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 *gophercloud.ServiceClient, id string) (r GetResult) {
    98  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
    99  	return
   100  }
   101  
   102  // UpdateOptsBuilder is the interface options structs have to satisfy in order
   103  // to be used in the main Update operation in this package. Since many
   104  // extensions decorate or modify the common logic, it is useful for them to
   105  // satisfy a basic interface in order for them to be used.
   106  type UpdateOptsBuilder interface {
   107  	ToFirewallPolicyUpdateMap() (map[string]interface{}, error)
   108  }
   109  
   110  // UpdateOpts contains the values used when updating a firewall policy.
   111  type UpdateOpts struct {
   112  	Name          *string   `json:"name,omitempty"`
   113  	Description   *string   `json:"description,omitempty"`
   114  	Shared        *bool     `json:"shared,omitempty"`
   115  	Audited       *bool     `json:"audited,omitempty"`
   116  	FirewallRules *[]string `json:"firewall_rules,omitempty"`
   117  }
   118  
   119  // ToFirewallPolicyUpdateMap casts a CreateOpts struct to a map.
   120  func (opts UpdateOpts) ToFirewallPolicyUpdateMap() (map[string]interface{}, error) {
   121  	return gophercloud.BuildRequestBody(opts, "firewall_policy")
   122  }
   123  
   124  // Update allows firewall policies to be updated.
   125  func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   126  	b, err := opts.ToFirewallPolicyUpdateMap()
   127  	if err != nil {
   128  		r.Err = err
   129  		return
   130  	}
   131  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   132  		OkCodes: []int{200},
   133  	})
   134  	return
   135  }
   136  
   137  // Delete will permanently delete a particular firewall policy based on its unique ID.
   138  func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) {
   139  	_, r.Err = c.Delete(resourceURL(c, id), nil)
   140  	return
   141  }
   142  
   143  type InsertRuleOptsBuilder interface {
   144  	ToFirewallPolicyInsertRuleMap() (map[string]interface{}, error)
   145  }
   146  
   147  type InsertRuleOpts struct {
   148  	ID           string `json:"firewall_rule_id" required:"true"`
   149  	InsertBefore string `json:"insert_before,omitempty" xor:"InsertAfter"`
   150  	InsertAfter  string `json:"insert_after,omitempty" xor:"InsertBefore"`
   151  }
   152  
   153  func (opts InsertRuleOpts) ToFirewallPolicyInsertRuleMap() (map[string]interface{}, error) {
   154  	return gophercloud.BuildRequestBody(opts, "")
   155  }
   156  
   157  func InsertRule(c *gophercloud.ServiceClient, id string, opts InsertRuleOptsBuilder) (r InsertRuleResult) {
   158  	b, err := opts.ToFirewallPolicyInsertRuleMap()
   159  	if err != nil {
   160  		r.Err = err
   161  		return
   162  	}
   163  	_, r.Err = c.Put(insertURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   164  		OkCodes: []int{200},
   165  	})
   166  	return
   167  }
   168  
   169  func RemoveRule(c *gophercloud.ServiceClient, id, ruleID string) (r RemoveRuleResult) {
   170  	b := map[string]interface{}{"firewall_rule_id": ruleID}
   171  	_, r.Err = c.Put(removeURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   172  		OkCodes: []int{200},
   173  	})
   174  	return
   175  }