github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/extensions/fwaas_v2/policies/requests.go (about)

     1  package policies
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/vnpaycloud-console/gophercloud/v2"
     7  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
     8  )
     9  
    10  // ListOptsBuilder allows extensions to add additional parameters to the
    11  // List request.
    12  type ListOptsBuilder interface {
    13  	ToPolicyListQuery() (string, error)
    14  }
    15  
    16  // ListOpts allows the filtering and sorting of paginated collections through
    17  // the API. Filtering is achieved by passing in struct field values that map to
    18  // the firewall policy attributes you want to see returned. SortKey allows you
    19  // to sort by a particular firewall policy attribute. SortDir sets the direction,
    20  // and is either `asc' or `desc'. Marker and Limit are used for pagination.
    21  type ListOpts struct {
    22  	TenantID    string `q:"tenant_id"`
    23  	ProjectID   string `q:"project_id"`
    24  	Name        string `q:"name"`
    25  	Description string `q:"description"`
    26  	Shared      *bool  `q:"shared"`
    27  	Audited     *bool  `q:"audited"`
    28  	ID          string `q:"id"`
    29  	Limit       int    `q:"limit"`
    30  	Marker      string `q:"marker"`
    31  	SortKey     string `q:"sort_key"`
    32  	SortDir     string `q:"sort_dir"`
    33  }
    34  
    35  // ToPolicyListQuery formats a ListOpts into a query string.
    36  func (opts ListOpts) ToPolicyListQuery() (string, error) {
    37  	q, err := gophercloud.BuildQueryString(opts)
    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 *gophercloud.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]any, 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  	ProjectID     string   `json:"project_id,omitempty"`
    75  	Name          string   `json:"name,omitempty"`
    76  	Description   string   `json:"description,omitempty"`
    77  	Shared        *bool    `json:"shared,omitempty"`
    78  	Audited       *bool    `json:"audited,omitempty"`
    79  	FirewallRules []string `json:"firewall_rules,omitempty"`
    80  }
    81  
    82  // ToFirewallPolicyCreateMap casts a CreateOpts struct to a map.
    83  func (opts CreateOpts) ToFirewallPolicyCreateMap() (map[string]any, error) {
    84  	return gophercloud.BuildRequestBody(opts, "firewall_policy")
    85  }
    86  
    87  // Create accepts a CreateOpts struct and uses the values to create a new firewall policy
    88  func Create(ctx context.Context, c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    89  	b, err := opts.ToFirewallPolicyCreateMap()
    90  	if err != nil {
    91  		r.Err = err
    92  		return
    93  	}
    94  	resp, err := c.Post(ctx, rootURL(c), b, &r.Body, nil)
    95  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    96  	return
    97  }
    98  
    99  // Get retrieves a particular firewall policy based on its unique ID.
   100  func Get(ctx context.Context, c *gophercloud.ServiceClient, id string) (r GetResult) {
   101  	resp, err := c.Get(ctx, resourceURL(c, id), &r.Body, nil)
   102  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   103  	return
   104  }
   105  
   106  // UpdateOptsBuilder is the interface options structs have to satisfy in order
   107  // to be used in the main Update operation in this package. Since many
   108  // extensions decorate or modify the common logic, it is useful for them to
   109  // satisfy a basic interface in order for them to be used.
   110  type UpdateOptsBuilder interface {
   111  	ToFirewallPolicyUpdateMap() (map[string]any, error)
   112  }
   113  
   114  // UpdateOpts contains the values used when updating a firewall policy.
   115  type UpdateOpts struct {
   116  	Name          *string   `json:"name,omitempty"`
   117  	Description   *string   `json:"description,omitempty"`
   118  	Shared        *bool     `json:"shared,omitempty"`
   119  	Audited       *bool     `json:"audited,omitempty"`
   120  	FirewallRules *[]string `json:"firewall_rules,omitempty"`
   121  }
   122  
   123  // ToFirewallPolicyUpdateMap casts a CreateOpts struct to a map.
   124  func (opts UpdateOpts) ToFirewallPolicyUpdateMap() (map[string]any, error) {
   125  	return gophercloud.BuildRequestBody(opts, "firewall_policy")
   126  }
   127  
   128  // Update allows firewall policies to be updated.
   129  func Update(ctx context.Context, c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   130  	b, err := opts.ToFirewallPolicyUpdateMap()
   131  	if err != nil {
   132  		r.Err = err
   133  		return
   134  	}
   135  	resp, err := c.Put(ctx, resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   136  		OkCodes: []int{200},
   137  	})
   138  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   139  	return
   140  }
   141  
   142  // Delete will permanently delete a particular firewall policy based on its unique ID.
   143  func Delete(ctx context.Context, c *gophercloud.ServiceClient, id string) (r DeleteResult) {
   144  	resp, err := c.Delete(ctx, resourceURL(c, id), nil)
   145  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   146  	return
   147  }
   148  
   149  type InsertRuleOptsBuilder interface {
   150  	ToFirewallPolicyInsertRuleMap() (map[string]any, error)
   151  }
   152  
   153  type InsertRuleOpts struct {
   154  	ID           string `json:"firewall_rule_id" required:"true"`
   155  	InsertBefore string `json:"insert_before,omitempty" xor:"InsertAfter"`
   156  	InsertAfter  string `json:"insert_after,omitempty" xor:"InsertBefore"`
   157  }
   158  
   159  func (opts InsertRuleOpts) ToFirewallPolicyInsertRuleMap() (map[string]any, error) {
   160  	return gophercloud.BuildRequestBody(opts, "")
   161  }
   162  
   163  func InsertRule(ctx context.Context, c *gophercloud.ServiceClient, id string, opts InsertRuleOptsBuilder) (r InsertRuleResult) {
   164  	b, err := opts.ToFirewallPolicyInsertRuleMap()
   165  	if err != nil {
   166  		r.Err = err
   167  		return
   168  	}
   169  	resp, err := c.Put(ctx, insertURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   170  		OkCodes: []int{200},
   171  	})
   172  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   173  	return
   174  }
   175  
   176  func RemoveRule(ctx context.Context, c *gophercloud.ServiceClient, id, ruleID string) (r RemoveRuleResult) {
   177  	b := map[string]any{"firewall_rule_id": ruleID}
   178  	resp, err := c.Put(ctx, removeURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   179  		OkCodes: []int{200},
   180  	})
   181  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   182  	return
   183  }