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 ( |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 4 | "github.com/rackspace/gophercloud" |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 5 | "github.com/rackspace/gophercloud/pagination" |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 6 | ) |
| 7 | |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 8 | // EndpointOpts contains the subset of Endpoint attributes that should be used to create or update an Endpoint. |
| 9 | type EndpointOpts struct { |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 10 | Availability gophercloud.Availability |
| 11 | Name string |
| 12 | Region string |
| 13 | URL string |
| 14 | ServiceID string |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | // Create inserts a new Endpoint into the service catalog. |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 18 | // Within EndpointOpts, Region may be omitted by being left as "", but all other fields are required. |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 19 | func Create(client *gophercloud.ServiceClient, opts EndpointOpts) CreateResult { |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 20 | // Redefined so that Region can be re-typed as a *string, which can be omitted from the JSON output. |
| 21 | type endpoint struct { |
| 22 | Interface string `json:"interface"` |
| 23 | Name string `json:"name"` |
| 24 | Region *string `json:"region,omitempty"` |
| 25 | URL string `json:"url"` |
| 26 | ServiceID string `json:"service_id"` |
| 27 | } |
| 28 | |
| 29 | type request struct { |
| 30 | Endpoint endpoint `json:"endpoint"` |
| 31 | } |
| 32 | |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 33 | // Ensure that EndpointOpts is fully populated. |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 34 | if opts.Availability == "" { |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 35 | return createErr(ErrAvailabilityRequired) |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 36 | } |
| 37 | if opts.Name == "" { |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 38 | return createErr(ErrNameRequired) |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 39 | } |
| 40 | if opts.URL == "" { |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 41 | return createErr(ErrURLRequired) |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 42 | } |
| 43 | if opts.ServiceID == "" { |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 44 | return createErr(ErrServiceIDRequired) |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | // Populate the request body. |
| 48 | reqBody := request{ |
| 49 | Endpoint: endpoint{ |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 50 | Interface: string(opts.Availability), |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 51 | Name: opts.Name, |
| 52 | URL: opts.URL, |
| 53 | ServiceID: opts.ServiceID, |
| 54 | }, |
| 55 | } |
Ash Wilson | b18fc10 | 2014-09-30 15:26:01 -0400 | [diff] [blame] | 56 | reqBody.Endpoint.Region = gophercloud.MaybeString(opts.Region) |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 57 | |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 58 | var result CreateResult |
Ash Wilson | 4bf41a3 | 2015-02-12 15:52:44 -0500 | [diff] [blame] | 59 | _, result.Err = client.Request("POST", listURL(client), gophercloud.RequestOpts{ |
| 60 | JSONBody: &reqBody, |
| 61 | JSONResponse: &result.Body, |
| 62 | OkCodes: []int{201}, |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 63 | }) |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 64 | return result |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 65 | } |
| 66 | |
Alex Gaynor | a6d5f9f | 2014-10-27 10:52:32 -0700 | [diff] [blame] | 67 | // ListOpts allows finer control over the endpoints returned by a List call. |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 68 | // All fields are optional. |
| 69 | type ListOpts struct { |
Jon Perritt | 1980344 | 2014-10-28 12:11:10 -0500 | [diff] [blame] | 70 | Availability gophercloud.Availability `q:"interface"` |
| 71 | ServiceID string `q:"service_id"` |
| 72 | Page int `q:"page"` |
| 73 | PerPage int `q:"per_page"` |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | // List enumerates endpoints in a paginated collection, optionally filtered by ListOpts criteria. |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 77 | func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager { |
Jon Perritt | 1980344 | 2014-10-28 12:11:10 -0500 | [diff] [blame] | 78 | u := listURL(client) |
| 79 | q, err := gophercloud.BuildQueryString(opts) |
| 80 | if err != nil { |
| 81 | return pagination.Pager{Err: err} |
Ash Wilson | 32c0e8d | 2014-09-04 10:53:08 -0400 | [diff] [blame] | 82 | } |
Jon Perritt | 1980344 | 2014-10-28 12:11:10 -0500 | [diff] [blame] | 83 | u += q.String() |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 84 | createPage := func(r pagination.PageResult) pagination.Page { |
| 85 | return EndpointPage{pagination.LinkedPageBase{PageResult: r}} |
Ash Wilson | ab6be61 | 2014-09-15 15:51:22 -0400 | [diff] [blame] | 86 | } |
| 87 | |
Ash Wilson | cd95a0c | 2014-09-16 13:07:31 -0400 | [diff] [blame] | 88 | return pagination.NewPager(client, u, createPage) |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // Update changes an existing endpoint with new data. |
Ash Wilson | f04a74c | 2014-09-04 11:16:20 -0400 | [diff] [blame] | 92 | // All fields are optional in the provided EndpointOpts. |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 93 | func Update(client *gophercloud.ServiceClient, endpointID string, opts EndpointOpts) UpdateResult { |
Ash Wilson | f04a74c | 2014-09-04 11:16:20 -0400 | [diff] [blame] | 94 | type endpoint struct { |
| 95 | Interface *string `json:"interface,omitempty"` |
| 96 | Name *string `json:"name,omitempty"` |
| 97 | Region *string `json:"region,omitempty"` |
| 98 | URL *string `json:"url,omitempty"` |
| 99 | ServiceID *string `json:"service_id,omitempty"` |
| 100 | } |
| 101 | |
| 102 | type request struct { |
| 103 | Endpoint endpoint `json:"endpoint"` |
| 104 | } |
| 105 | |
Ash Wilson | f04a74c | 2014-09-04 11:16:20 -0400 | [diff] [blame] | 106 | reqBody := request{Endpoint: endpoint{}} |
Ash Wilson | b18fc10 | 2014-09-30 15:26:01 -0400 | [diff] [blame] | 107 | reqBody.Endpoint.Interface = gophercloud.MaybeString(string(opts.Availability)) |
| 108 | reqBody.Endpoint.Name = gophercloud.MaybeString(opts.Name) |
| 109 | reqBody.Endpoint.Region = gophercloud.MaybeString(opts.Region) |
| 110 | reqBody.Endpoint.URL = gophercloud.MaybeString(opts.URL) |
| 111 | reqBody.Endpoint.ServiceID = gophercloud.MaybeString(opts.ServiceID) |
Ash Wilson | f04a74c | 2014-09-04 11:16:20 -0400 | [diff] [blame] | 112 | |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 113 | var result UpdateResult |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 114 | _, result.Err = client.Request("PATCH", endpointURL(client, endpointID), gophercloud.RequestOpts{ |
| 115 | JSONBody: &reqBody, |
| 116 | JSONResponse: &result.Body, |
| 117 | OkCodes: []int{200}, |
Ash Wilson | f04a74c | 2014-09-04 11:16:20 -0400 | [diff] [blame] | 118 | }) |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 119 | return result |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // Delete removes an endpoint from the service catalog. |
Jamie Hannaford | 3c08674 | 2014-10-27 11:32:16 +0100 | [diff] [blame] | 123 | func Delete(client *gophercloud.ServiceClient, endpointID string) DeleteResult { |
| 124 | var res DeleteResult |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 125 | _, res.Err = client.Request("DELETE", endpointURL(client, endpointID), gophercloud.RequestOpts{ |
| 126 | OkCodes: []int{204}, |
Ash Wilson | 70db2ab | 2014-09-04 11:18:32 -0400 | [diff] [blame] | 127 | }) |
Jamie Hannaford | 3c08674 | 2014-10-27 11:32:16 +0100 | [diff] [blame] | 128 | return res |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 129 | } |