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

     1  package groups
     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  	ToGroupListQuery() (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 group attributes you want to see returned. SortKey allows you
    19  // to sort by a particular firewall group 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  	Name                    string    `q:"name"`
    24  	Description             string    `q:"description"`
    25  	IngressFirewallPolicyID string    `q:"ingress_firewall_policy_id"`
    26  	EgressFirewallPolicyID  string    `q:"egress_firewall_policy_id"`
    27  	AdminStateUp            *bool     `q:"admin_state_up"`
    28  	Ports                   *[]string `q:"ports"`
    29  	Status                  string    `q:"status"`
    30  	ID                      string    `q:"id"`
    31  	Shared                  *bool     `q:"shared"`
    32  	ProjectID               string    `q:"project_id"`
    33  	Limit                   int       `q:"limit"`
    34  	Marker                  string    `q:"marker"`
    35  	SortKey                 string    `q:"sort_key"`
    36  	SortDir                 string    `q:"sort_dir"`
    37  }
    38  
    39  // ToGroupListQuery formats a ListOpts into a query string.
    40  func (opts ListOpts) ToGroupListQuery() (string, error) {
    41  	q, err := gophercloud.BuildQueryString(opts)
    42  	if err != nil {
    43  		return "", err
    44  	}
    45  	return q.String(), err
    46  }
    47  
    48  // List returns a Pager which allows you to iterate over a collection of
    49  // firewall groups. It accepts a ListOpts struct, which allows you to filter
    50  // and sort the returned collection for greater efficiency.
    51  //
    52  // Default group settings return only those firewall groups that are owned by the
    53  // tenant who submits the request, unless an admin user submits the request.
    54  func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    55  	url := rootURL(c)
    56  
    57  	if opts != nil {
    58  		query, err := opts.ToGroupListQuery()
    59  		if err != nil {
    60  			return pagination.Pager{Err: err}
    61  		}
    62  		url += query
    63  	}
    64  
    65  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    66  		return GroupPage{pagination.LinkedPageBase{PageResult: r}}
    67  	})
    68  }
    69  
    70  // Get retrieves a particular firewall group based on its unique ID.
    71  func Get(ctx context.Context, c *gophercloud.ServiceClient, id string) (r GetResult) {
    72  	resp, err := c.Get(ctx, resourceURL(c, id), &r.Body, nil)
    73  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    74  	return
    75  }
    76  
    77  // CreateOptsBuilder is the interface options structs have to satisfy in order
    78  // to be used in the main Create operation in this package. Since many
    79  // extensions decorate or modify the common logic, it is useful for them to
    80  // satisfy a basic interface in order for them to be used.
    81  type CreateOptsBuilder interface {
    82  	ToFirewallGroupCreateMap() (map[string]any, error)
    83  }
    84  
    85  // CreateOpts contains all the values needed to create a new firewall group.
    86  type CreateOpts struct {
    87  	ID                      string   `json:"id,omitempty"`
    88  	TenantID                string   `json:"tenant_id,omitempty"`
    89  	Name                    string   `json:"name,omitempty"`
    90  	Description             string   `json:"description,omitempty"`
    91  	IngressFirewallPolicyID string   `json:"ingress_firewall_policy_id,omitempty"`
    92  	EgressFirewallPolicyID  string   `json:"egress_firewall_policy_id,omitempty"`
    93  	AdminStateUp            *bool    `json:"admin_state_up,omitempty"`
    94  	Ports                   []string `json:"ports,omitempty"`
    95  	Shared                  *bool    `json:"shared,omitempty"`
    96  	ProjectID               string   `json:"project_id,omitempty"`
    97  }
    98  
    99  // ToFirewallGroupCreateMap casts a CreateOpts struct to a map.
   100  func (opts CreateOpts) ToFirewallGroupCreateMap() (map[string]any, error) {
   101  	return gophercloud.BuildRequestBody(opts, "firewall_group")
   102  }
   103  
   104  // Create accepts a CreateOpts struct and uses the values to create a new firewall group
   105  func Create(ctx context.Context, c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   106  	b, err := opts.ToFirewallGroupCreateMap()
   107  	if err != nil {
   108  		r.Err = err
   109  		return
   110  	}
   111  	resp, err := c.Post(ctx, rootURL(c), b, &r.Body, nil)
   112  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   113  	return
   114  }
   115  
   116  // UpdateOptsBuilder is the interface options structs have to satisfy in order
   117  // to be used in the main Update operation in this package. Since many
   118  // extensions decorate or modify the common logic, it is useful for them to
   119  // satisfy a basic interface in order for them to be used.
   120  type UpdateOptsBuilder interface {
   121  	ToFirewallGroupUpdateMap() (map[string]any, error)
   122  }
   123  
   124  // UpdateOpts contains the values used when updating a firewall group.
   125  type UpdateOpts struct {
   126  	Name                    *string   `json:"name,omitempty"`
   127  	Description             *string   `json:"description,omitempty"`
   128  	IngressFirewallPolicyID *string   `json:"ingress_firewall_policy_id,omitempty"`
   129  	EgressFirewallPolicyID  *string   `json:"egress_firewall_policy_id,omitempty"`
   130  	AdminStateUp            *bool     `json:"admin_state_up,omitempty"`
   131  	Ports                   *[]string `json:"ports,omitempty"`
   132  	Shared                  *bool     `json:"shared,omitempty"`
   133  }
   134  
   135  // ToFirewallGroupUpdateMap casts a UpdateOpts struct to a map.
   136  func (opts UpdateOpts) ToFirewallGroupUpdateMap() (map[string]any, error) {
   137  	return gophercloud.BuildRequestBody(opts, "firewall_group")
   138  }
   139  
   140  // Update allows firewall groups to be updated.
   141  func Update(ctx context.Context, c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   142  	b, err := opts.ToFirewallGroupUpdateMap()
   143  	if err != nil {
   144  		r.Err = err
   145  		return
   146  	}
   147  	resp, err := c.Put(ctx, resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   148  		OkCodes: []int{200},
   149  	})
   150  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   151  	return
   152  }
   153  
   154  // Because of fwaas_v2 wait only UUID not string and base updateOpts has omitempty,
   155  // only set nil allows firewall group policies to be unset.
   156  // Two different functions, because can not specify both policy in one function.
   157  // New functions needs new structs without omitempty.
   158  // Separate function for BuildRequestBody is missing due to complication
   159  // of code readability and bulkiness.
   160  
   161  type RemoveIngressPolicyOpts struct {
   162  	IngressFirewallPolicyID *string `json:"ingress_firewall_policy_id"`
   163  }
   164  
   165  func RemoveIngressPolicy(ctx context.Context, c *gophercloud.ServiceClient, id string) (r UpdateResult) {
   166  	b, err := gophercloud.BuildRequestBody(RemoveIngressPolicyOpts{IngressFirewallPolicyID: nil}, "firewall_group")
   167  	if err != nil {
   168  		r.Err = err
   169  		return
   170  	}
   171  	resp, err := c.Put(ctx, resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   172  		OkCodes: []int{200},
   173  	})
   174  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   175  	return
   176  }
   177  
   178  type RemoveEgressPolicyOpts struct {
   179  	EgressFirewallPolicyID *string `json:"egress_firewall_policy_id"`
   180  }
   181  
   182  func RemoveEgressPolicy(ctx context.Context, c *gophercloud.ServiceClient, id string) (r UpdateResult) {
   183  	b, err := gophercloud.BuildRequestBody(RemoveEgressPolicyOpts{EgressFirewallPolicyID: nil}, "firewall_group")
   184  	if err != nil {
   185  		r.Err = err
   186  		return
   187  	}
   188  	resp, err := c.Put(ctx, resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   189  		OkCodes: []int{200},
   190  	})
   191  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   192  	return
   193  }
   194  
   195  // Delete will permanently delete a particular firewall group based on its unique ID.
   196  func Delete(ctx context.Context, c *gophercloud.ServiceClient, id string) (r DeleteResult) {
   197  	resp, err := c.Delete(ctx, resourceURL(c, id), nil)
   198  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   199  	return
   200  }