Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 1 | package endpoints |
| 2 | |
Ash Wilson | 700d13a | 2014-09-05 14:24:16 -0400 | [diff] [blame] | 3 | import ( |
Ash Wilson | 700d13a | 2014-09-05 14:24:16 -0400 | [diff] [blame] | 4 | "github.com/mitchellh/mapstructure" |
| 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 | 700d13a | 2014-09-05 14:24:16 -0400 | [diff] [blame] | 7 | ) |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 8 | |
| 9 | // Endpoint describes the entry point for another service's API. |
| 10 | type Endpoint struct { |
Ash Wilson | 6269f25 | 2014-09-12 14:33:56 -0400 | [diff] [blame] | 11 | ID string `mapstructure:"id" json:"id"` |
| 12 | Availability gophercloud.Availability `mapstructure:"interface" json:"interface"` |
| 13 | Name string `mapstructure:"name" json:"name"` |
| 14 | Region string `mapstructure:"region" json:"region"` |
| 15 | ServiceID string `mapstructure:"service_id" json:"service_id"` |
| 16 | URL string `mapstructure:"url" json:"url"` |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 17 | } |
| 18 | |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 19 | // EndpointPage is a single page of Endpoint results. |
| 20 | type EndpointPage struct { |
| 21 | pagination.LinkedPageBase |
| 22 | } |
| 23 | |
| 24 | // IsEmpty returns true if no Endpoints were returned. |
| 25 | func (p EndpointPage) IsEmpty() (bool, error) { |
| 26 | es, err := ExtractEndpoints(p) |
| 27 | if err != nil { |
| 28 | return true, err |
| 29 | } |
| 30 | return len(es) == 0, nil |
| 31 | } |
| 32 | |
Ash Wilson | 6269f25 | 2014-09-12 14:33:56 -0400 | [diff] [blame] | 33 | // ExtractEndpoints extracts an Endpoint slice from a Page. |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 34 | func ExtractEndpoints(page pagination.Page) ([]Endpoint, error) { |
Ash Wilson | 6269f25 | 2014-09-12 14:33:56 -0400 | [diff] [blame] | 35 | var response struct { |
| 36 | Endpoints []Endpoint `mapstructure:"endpoints"` |
Ash Wilson | 700d13a | 2014-09-05 14:24:16 -0400 | [diff] [blame] | 37 | } |
| 38 | |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 39 | err := mapstructure.Decode(page.(EndpointPage).Body, &response) |
Ash Wilson | 700d13a | 2014-09-05 14:24:16 -0400 | [diff] [blame] | 40 | if err != nil { |
| 41 | return nil, err |
| 42 | } |
Ash Wilson | 6269f25 | 2014-09-12 14:33:56 -0400 | [diff] [blame] | 43 | return response.Endpoints, nil |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 44 | } |