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