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