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