github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/vpnaas/endpointgroups/requests.go (about)

     1  package endpointgroups
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/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 golangsdk.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 *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    54  	b, err := opts.ToEndpointGroupCreateMap()
    55  	if err != nil {
    56  		r.Err = err
    57  		return
    58  	}
    59  	_, r.Err = c.Post(rootURL(c), b, &r.Body, nil)
    60  	return
    61  }
    62  
    63  // Get retrieves a particular endpoint group based on its unique ID.
    64  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
    65  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
    66  	return
    67  }
    68  
    69  // ListOptsBuilder allows extensions to add additional parameters to the
    70  // List request.
    71  type ListOptsBuilder interface {
    72  	ToEndpointGroupListQuery() (string, error)
    73  }
    74  
    75  // ListOpts allows the filtering of paginated collections through
    76  // the API. Filtering is achieved by passing in struct field values that map to
    77  // the Endpoint group attributes you want to see returned.
    78  type ListOpts struct {
    79  	TenantID    string `q:"tenant_id"`
    80  	ProjectID   string `q:"project_id"`
    81  	Description string `q:"description"`
    82  	Name        string `q:"name"`
    83  	Type        string `q:"type"`
    84  }
    85  
    86  // ToEndpointGroupListQuery formats a ListOpts into a query string.
    87  func (opts ListOpts) ToEndpointGroupListQuery() (string, error) {
    88  	q, err := golangsdk.BuildQueryString(opts)
    89  	return q.String(), err
    90  }
    91  
    92  // List returns a Pager which allows you to iterate over a collection of
    93  // Endpoint groups. It accepts a ListOpts struct, which allows you to filter
    94  // the returned collection for greater efficiency.
    95  func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    96  	url := rootURL(c)
    97  	if opts != nil {
    98  		query, err := opts.ToEndpointGroupListQuery()
    99  		if err != nil {
   100  			return pagination.Pager{Err: err}
   101  		}
   102  		url += query
   103  	}
   104  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   105  		return EndpointGroupPage{pagination.LinkedPageBase{PageResult: r}}
   106  	})
   107  }
   108  
   109  // Delete will permanently delete a particular endpoint group based on its
   110  // unique ID.
   111  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   112  	_, r.Err = c.Delete(resourceURL(c, id), nil)
   113  	return
   114  }
   115  
   116  // UpdateOptsBuilder allows extensions to add additional parameters to the
   117  // Update request.
   118  type UpdateOptsBuilder interface {
   119  	ToEndpointGroupUpdateMap() (map[string]interface{}, error)
   120  }
   121  
   122  // UpdateOpts contains the values used when updating an endpoint group.
   123  type UpdateOpts struct {
   124  	Description *string `json:"description,omitempty"`
   125  	Name        *string `json:"name,omitempty"`
   126  }
   127  
   128  // ToEndpointGroupUpdateMap casts an UpdateOpts struct to a map.
   129  func (opts UpdateOpts) ToEndpointGroupUpdateMap() (map[string]interface{}, error) {
   130  	return golangsdk.BuildRequestBody(opts, "endpoint_group")
   131  }
   132  
   133  // Update allows endpoint groups to be updated.
   134  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   135  	b, err := opts.ToEndpointGroupUpdateMap()
   136  	if err != nil {
   137  		r.Err = err
   138  		return
   139  	}
   140  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   141  		OkCodes: []int{200},
   142  	})
   143  	return
   144  }