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