github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/extensions/security/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  // ListOpts allows the filtering and sorting of paginated collections through
    11  // the API. Filtering is achieved by passing in struct field values that map to
    12  // the group attributes you want to see returned. SortKey allows you to
    13  // sort by a particular network attribute. SortDir sets the direction, and is
    14  // either `asc' or `desc'. Marker and Limit are used for pagination.
    15  type ListOpts struct {
    16  	ID          string `q:"id"`
    17  	Name        string `q:"name"`
    18  	Description string `q:"description"`
    19  	Stateful    *bool  `q:"stateful"`
    20  	TenantID    string `q:"tenant_id"`
    21  	ProjectID   string `q:"project_id"`
    22  	Limit       int    `q:"limit"`
    23  	Marker      string `q:"marker"`
    24  	SortKey     string `q:"sort_key"`
    25  	SortDir     string `q:"sort_dir"`
    26  	Tags        string `q:"tags"`
    27  	TagsAny     string `q:"tags-any"`
    28  	NotTags     string `q:"not-tags"`
    29  	NotTagsAny  string `q:"not-tags-any"`
    30  }
    31  
    32  // List returns a Pager which allows you to iterate over a collection of
    33  // security groups. It accepts a ListOpts struct, which allows you to filter
    34  // and sort the returned collection for greater efficiency.
    35  func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
    36  	q, err := gophercloud.BuildQueryString(&opts)
    37  	if err != nil {
    38  		return pagination.Pager{Err: err}
    39  	}
    40  	u := rootURL(c) + q.String()
    41  	return pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
    42  		return SecGroupPage{pagination.LinkedPageBase{PageResult: r}}
    43  	})
    44  }
    45  
    46  // CreateOptsBuilder allows extensions to add additional parameters to the
    47  // Create request.
    48  type CreateOptsBuilder interface {
    49  	ToSecGroupCreateMap() (map[string]any, error)
    50  }
    51  
    52  // CreateOpts contains all the values needed to create a new security group.
    53  type CreateOpts struct {
    54  	// Human-readable name for the Security Group. Does not have to be unique.
    55  	Name string `json:"name" required:"true"`
    56  
    57  	// TenantID is the UUID of the project who owns the Group.
    58  	// Only administrative users can specify a tenant UUID other than their own.
    59  	TenantID string `json:"tenant_id,omitempty"`
    60  
    61  	// ProjectID is the UUID of the project who owns the Group.
    62  	// Only administrative users can specify a tenant UUID other than their own.
    63  	ProjectID string `json:"project_id,omitempty"`
    64  
    65  	// Describes the security group.
    66  	Description string `json:"description,omitempty"`
    67  
    68  	// Stateful indicates if the security group is stateful or stateless.
    69  	Stateful *bool `json:"stateful,omitempty"`
    70  }
    71  
    72  // ToSecGroupCreateMap builds a request body from CreateOpts.
    73  func (opts CreateOpts) ToSecGroupCreateMap() (map[string]any, error) {
    74  	return gophercloud.BuildRequestBody(opts, "security_group")
    75  }
    76  
    77  // Create is an operation which provisions a new security group with default
    78  // security group rules for the IPv4 and IPv6 ether types.
    79  func Create(ctx context.Context, c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    80  	b, err := opts.ToSecGroupCreateMap()
    81  	if err != nil {
    82  		r.Err = err
    83  		return
    84  	}
    85  	resp, err := c.Post(ctx, rootURL(c), b, &r.Body, nil)
    86  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    87  	return
    88  }
    89  
    90  // UpdateOptsBuilder allows extensions to add additional parameters to the
    91  // Update request.
    92  type UpdateOptsBuilder interface {
    93  	ToSecGroupUpdateMap() (map[string]any, error)
    94  }
    95  
    96  // UpdateOpts contains all the values needed to update an existing security
    97  // group.
    98  type UpdateOpts struct {
    99  	// Human-readable name for the Security Group. Does not have to be unique.
   100  	Name string `json:"name,omitempty"`
   101  
   102  	// Describes the security group.
   103  	Description *string `json:"description,omitempty"`
   104  
   105  	// Stateful indicates if the security group is stateful or stateless.
   106  	Stateful *bool `json:"stateful,omitempty"`
   107  }
   108  
   109  // ToSecGroupUpdateMap builds a request body from UpdateOpts.
   110  func (opts UpdateOpts) ToSecGroupUpdateMap() (map[string]any, error) {
   111  	return gophercloud.BuildRequestBody(opts, "security_group")
   112  }
   113  
   114  // Update is an operation which updates an existing security group.
   115  func Update(ctx context.Context, c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   116  	b, err := opts.ToSecGroupUpdateMap()
   117  	if err != nil {
   118  		r.Err = err
   119  		return
   120  	}
   121  
   122  	resp, err := c.Put(ctx, resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   123  		OkCodes: []int{200},
   124  	})
   125  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   126  	return
   127  }
   128  
   129  // Get retrieves a particular security group based on its unique ID.
   130  func Get(ctx context.Context, c *gophercloud.ServiceClient, id string) (r GetResult) {
   131  	resp, err := c.Get(ctx, resourceURL(c, id), &r.Body, nil)
   132  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   133  	return
   134  }
   135  
   136  // Delete will permanently delete a particular security group based on its
   137  // unique ID.
   138  func Delete(ctx context.Context, c *gophercloud.ServiceClient, id string) (r DeleteResult) {
   139  	resp, err := c.Delete(ctx, resourceURL(c, id), nil)
   140  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   141  	return
   142  }