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