github.com/gophercloud/gophercloud@v1.14.1/openstack/networking/v2/extensions/security/groups/requests.go (about)

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