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