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