github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/vpnaas/endpointgroups/requests.go (about)

     1  package endpointgroups
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  type EndpointType string
     9  
    10  const (
    11  	TypeSubnet  EndpointType = "subnet"
    12  	TypeCIDR    EndpointType = "cidr"
    13  	TypeVLAN    EndpointType = "vlan"
    14  	TypeNetwork EndpointType = "network"
    15  	TypeRouter  EndpointType = "router"
    16  )
    17  
    18  // CreateOptsBuilder allows extensions to add additional parameters to the
    19  // Create request.
    20  type CreateOptsBuilder interface {
    21  	ToEndpointGroupCreateMap() (map[string]interface{}, error)
    22  }
    23  
    24  // CreateOpts contains all the values needed to create a new endpoint group
    25  type CreateOpts struct {
    26  	// TenantID specifies a tenant to own the endpoint group. The caller must have
    27  	// an admin role in order to set this. Otherwise, this field is left unset
    28  	// and the caller will be the owner.
    29  	TenantID string `json:"tenant_id,omitempty"`
    30  
    31  	// Description is the human readable description of the endpoint group.
    32  	Description string `json:"description,omitempty"`
    33  
    34  	// Name is the human readable name of the endpoint group.
    35  	Name string `json:"name,omitempty"`
    36  
    37  	// The type of the endpoints in the group.
    38  	// A valid value is subnet, cidr, network, router, or vlan.
    39  	Type EndpointType `json:"type,omitempty"`
    40  
    41  	// List of endpoints of the same type, for the endpoint group.
    42  	// The values will depend on the type.
    43  	Endpoints []string `json:"endpoints"`
    44  }
    45  
    46  // ToEndpointGroupCreateMap casts a CreateOpts struct to a map.
    47  func (opts CreateOpts) ToEndpointGroupCreateMap() (map[string]interface{}, error) {
    48  	return gophercloud.BuildRequestBody(opts, "endpoint_group")
    49  }
    50  
    51  // Create accepts a CreateOpts struct and uses the values to create a new
    52  // endpoint group.
    53  func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    54  	b, err := opts.ToEndpointGroupCreateMap()
    55  	if err != nil {
    56  		r.Err = err
    57  		return
    58  	}
    59  	resp, err := c.Post(rootURL(c), b, &r.Body, nil)
    60  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    61  	return
    62  }
    63  
    64  // Get retrieves a particular endpoint group based on its unique ID.
    65  func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
    66  	resp, err := c.Get(resourceURL(c, id), &r.Body, nil)
    67  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    68  	return
    69  }
    70  
    71  // ListOptsBuilder allows extensions to add additional parameters to the
    72  // List request.
    73  type ListOptsBuilder interface {
    74  	ToEndpointGroupListQuery() (string, error)
    75  }
    76  
    77  // ListOpts allows the filtering of paginated collections through
    78  // the API. Filtering is achieved by passing in struct field values that map to
    79  // the Endpoint group attributes you want to see returned.
    80  type ListOpts struct {
    81  	TenantID    string `q:"tenant_id"`
    82  	ProjectID   string `q:"project_id"`
    83  	Description string `q:"description"`
    84  	Name        string `q:"name"`
    85  	Type        string `q:"type"`
    86  }
    87  
    88  // ToEndpointGroupListQuery formats a ListOpts into a query string.
    89  func (opts ListOpts) ToEndpointGroupListQuery() (string, error) {
    90  	q, err := gophercloud.BuildQueryString(opts)
    91  	return q.String(), err
    92  }
    93  
    94  // List returns a Pager which allows you to iterate over a collection of
    95  // Endpoint groups. It accepts a ListOpts struct, which allows you to filter
    96  // the returned collection for greater efficiency.
    97  func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    98  	url := rootURL(c)
    99  	if opts != nil {
   100  		query, err := opts.ToEndpointGroupListQuery()
   101  		if err != nil {
   102  			return pagination.Pager{Err: err}
   103  		}
   104  		url += query
   105  	}
   106  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   107  		return EndpointGroupPage{pagination.LinkedPageBase{PageResult: r}}
   108  	})
   109  }
   110  
   111  // Delete will permanently delete a particular endpoint group based on its
   112  // unique ID.
   113  func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) {
   114  	resp, err := c.Delete(resourceURL(c, id), nil)
   115  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   116  	return
   117  }
   118  
   119  // UpdateOptsBuilder allows extensions to add additional parameters to the
   120  // Update request.
   121  type UpdateOptsBuilder interface {
   122  	ToEndpointGroupUpdateMap() (map[string]interface{}, error)
   123  }
   124  
   125  // UpdateOpts contains the values used when updating an endpoint group.
   126  type UpdateOpts struct {
   127  	Description *string `json:"description,omitempty"`
   128  	Name        *string `json:"name,omitempty"`
   129  }
   130  
   131  // ToEndpointGroupUpdateMap casts an UpdateOpts struct to a map.
   132  func (opts UpdateOpts) ToEndpointGroupUpdateMap() (map[string]interface{}, error) {
   133  	return gophercloud.BuildRequestBody(opts, "endpoint_group")
   134  }
   135  
   136  // Update allows endpoint groups to be updated.
   137  func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   138  	b, err := opts.ToEndpointGroupUpdateMap()
   139  	if err != nil {
   140  		r.Err = err
   141  		return
   142  	}
   143  	resp, err := c.Put(resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   144  		OkCodes: []int{200},
   145  	})
   146  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   147  	return
   148  }