Ash Wilson | 0a997f8 | 2014-09-03 15:50:52 -0400 | [diff] [blame] | 1 | package endpoints |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
Krzysztof Szukiełojć | 24a29ce | 2017-05-07 14:24:02 +0200 | [diff] [blame^] | 5 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination" |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 6 | ) |
| 7 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 8 | type CreateOptsBuilder interface { |
| 9 | ToEndpointCreateMap() (map[string]interface{}, error) |
| 10 | } |
| 11 | |
| 12 | // CreateOpts contains the subset of Endpoint attributes that should be used to create an Endpoint. |
| 13 | type CreateOpts struct { |
| 14 | Availability gophercloud.Availability `json:"interface" required:"true"` |
| 15 | Name string `json:"name" required:"true"` |
| 16 | Region string `json:"region,omitempty"` |
| 17 | URL string `json:"url" required:"true"` |
| 18 | ServiceID string `json:"service_id" required:"true"` |
| 19 | } |
| 20 | |
| 21 | func (opts CreateOpts) ToEndpointCreateMap() (map[string]interface{}, error) { |
| 22 | return gophercloud.BuildRequestBody(opts, "endpoint") |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | // Create inserts a new Endpoint into the service catalog. |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 26 | // Within EndpointOpts, Region may be omitted by being left as "", but all other fields are required. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 27 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 28 | b, err := opts.ToEndpointCreateMap() |
| 29 | if err != nil { |
| 30 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 31 | return |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 32 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 33 | _, r.Err = client.Post(listURL(client), &b, &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 34 | return |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 35 | } |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 36 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 37 | type ListOptsBuilder interface { |
| 38 | ToEndpointListParams() (string, error) |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 39 | } |
| 40 | |
Alex Gaynor | a6d5f9f | 2014-10-27 10:52:32 -0700 | [diff] [blame] | 41 | // ListOpts allows finer control over the endpoints returned by a List call. |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 42 | // All fields are optional. |
| 43 | type ListOpts struct { |
Jon Perritt | 1980344 | 2014-10-28 12:11:10 -0500 | [diff] [blame] | 44 | Availability gophercloud.Availability `q:"interface"` |
| 45 | ServiceID string `q:"service_id"` |
| 46 | Page int `q:"page"` |
| 47 | PerPage int `q:"per_page"` |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 48 | } |
| 49 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 50 | func (opts ListOpts) ToEndpointListParams() (string, error) { |
Jon Perritt | 1980344 | 2014-10-28 12:11:10 -0500 | [diff] [blame] | 51 | q, err := gophercloud.BuildQueryString(opts) |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 52 | return q.String(), err |
| 53 | } |
Ash Wilson | ab6be61 | 2014-09-15 15:51:22 -0400 | [diff] [blame] | 54 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 55 | // List enumerates endpoints in a paginated collection, optionally filtered by ListOpts criteria. |
| 56 | func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
| 57 | u := listURL(client) |
| 58 | if opts != nil { |
| 59 | q, err := gophercloud.BuildQueryString(opts) |
| 60 | if err != nil { |
| 61 | return pagination.Pager{Err: err} |
| 62 | } |
| 63 | u += q.String() |
| 64 | } |
| 65 | return pagination.NewPager(client, u, func(r pagination.PageResult) pagination.Page { |
| 66 | return EndpointPage{pagination.LinkedPageBase{PageResult: r}} |
| 67 | }) |
| 68 | } |
| 69 | |
| 70 | type UpdateOptsBuilder interface { |
| 71 | ToEndpointUpdateMap() (map[string]interface{}, error) |
| 72 | } |
| 73 | |
| 74 | // UpdateOpts contains the subset of Endpoint attributes that should be used to update an Endpoint. |
| 75 | type UpdateOpts struct { |
| 76 | Availability gophercloud.Availability `json:"interface,omitempty"` |
| 77 | Name string `json:"name,omitempty"` |
| 78 | Region string `json:"region,omitempty"` |
| 79 | URL string `json:"url,omitempty"` |
| 80 | ServiceID string `json:"service_id,omitempty"` |
| 81 | } |
| 82 | |
| 83 | func (opts UpdateOpts) ToEndpointUpdateMap() (map[string]interface{}, error) { |
| 84 | return gophercloud.BuildRequestBody(opts, "endpoint") |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // Update changes an existing endpoint with new data. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 88 | func Update(client *gophercloud.ServiceClient, endpointID string, opts UpdateOptsBuilder) (r UpdateResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 89 | b, err := opts.ToEndpointUpdateMap() |
| 90 | if err != nil { |
| 91 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 92 | return |
Ash Wilson | f04a74c | 2014-09-04 11:16:20 -0400 | [diff] [blame] | 93 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 94 | _, r.Err = client.Patch(endpointURL(client, endpointID), &b, &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 95 | return |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Delete removes an endpoint from the service catalog. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 99 | func Delete(client *gophercloud.ServiceClient, endpointID string) (r DeleteResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 100 | _, r.Err = client.Delete(endpointURL(client, endpointID), nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 101 | return |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 102 | } |