github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/networking/v2/extensions/fwaas_v2/policies/requests.go (about)

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