github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/fwaas/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
    44  // the 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 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 gophercloud.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 *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    87  	b, err := opts.ToFirewallPolicyCreateMap()
    88  	if err != nil {
    89  		r.Err = err
    90  		return
    91  	}
    92  	resp, err := c.Post(rootURL(c), b, &r.Body, nil)
    93  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    94  	return
    95  }
    96  
    97  // Get retrieves a particular firewall policy based on its unique ID.
    98  func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
    99  	resp, err := c.Get(resourceURL(c, id), &r.Body, nil)
   100  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   101  	return
   102  }
   103  
   104  // UpdateOptsBuilder allows extensions to add additional parameters to the
   105  // Update request.
   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  	Rules       []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  	resp, err := c.Put(resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   132  		OkCodes: []int{200},
   133  	})
   134  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   135  	return
   136  }
   137  
   138  // Delete will permanently delete a particular firewall policy based on its
   139  // unique ID.
   140  func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) {
   141  	resp, err := c.Delete(resourceURL(c, id), nil)
   142  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   143  	return
   144  }
   145  
   146  // InsertRuleOptsBuilder allows extensions to add additional parameters to the
   147  // InsertRule request.
   148  type InsertRuleOptsBuilder interface {
   149  	ToFirewallPolicyInsertRuleMap() (map[string]interface{}, error)
   150  }
   151  
   152  // InsertRuleOpts contains the values used when updating a policy's rules.
   153  type InsertRuleOpts struct {
   154  	ID           string `json:"firewall_rule_id" required:"true"`
   155  	BeforeRuleID string `json:"insert_before,omitempty"`
   156  	AfterRuleID  string `json:"insert_after,omitempty"`
   157  }
   158  
   159  func (opts InsertRuleOpts) ToFirewallPolicyInsertRuleMap() (map[string]interface{}, error) {
   160  	return gophercloud.BuildRequestBody(opts, "")
   161  }
   162  
   163  // AddRule will add a rule to a policy.
   164  func AddRule(c *gophercloud.ServiceClient, id string, opts InsertRuleOptsBuilder) (r InsertRuleResult) {
   165  	b, err := opts.ToFirewallPolicyInsertRuleMap()
   166  	if err != nil {
   167  		r.Err = err
   168  		return
   169  	}
   170  	resp, err := c.Put(insertURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   171  		OkCodes: []int{200},
   172  	})
   173  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   174  	return
   175  }
   176  
   177  // RemoveRule will add a rule to a policy.
   178  func RemoveRule(c *gophercloud.ServiceClient, id, ruleID string) (r RemoveRuleResult) {
   179  	b := map[string]interface{}{"firewall_rule_id": ruleID}
   180  	resp, err := c.Put(removeURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   181  		OkCodes: []int{200},
   182  	})
   183  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   184  	return
   185  }