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 ( |
| 4 | "fmt" |
| 5 | |
| 6 | "github.com/mitchellh/mapstructure" |
| 7 | "github.com/rackspace/gophercloud" |
| 8 | ) |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 9 | |
| 10 | // Endpoint describes the entry point for another service's API. |
| 11 | type Endpoint struct { |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 12 | ID string `json:"id"` |
| 13 | Availability gophercloud.Availability `json:"interface"` |
| 14 | Name string `json:"name"` |
| 15 | Region string `json:"region"` |
| 16 | ServiceID string `json:"service_id"` |
| 17 | URL string `json:"url"` |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | // EndpointList contains a page of Endpoint results. |
| 21 | type EndpointList struct { |
Ash Wilson | 8df23c8 | 2014-09-05 14:18:20 -0400 | [diff] [blame] | 22 | gophercloud.PaginationLinks `json:"links"` |
| 23 | |
Ash Wilson | 700d13a | 2014-09-05 14:24:16 -0400 | [diff] [blame] | 24 | client *gophercloud.ServiceClient |
Ash Wilson | 8df23c8 | 2014-09-05 14:18:20 -0400 | [diff] [blame] | 25 | Endpoints []Endpoint `json:"endpoints"` |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 26 | } |
Ash Wilson | 700d13a | 2014-09-05 14:24:16 -0400 | [diff] [blame] | 27 | |
| 28 | // Pager marks EndpointList as paged by links. |
| 29 | func (list EndpointList) Pager() gophercloud.Pager { |
| 30 | return gophercloud.NewLinkPager(list) |
| 31 | } |
| 32 | |
Ash Wilson | b110fc9 | 2014-09-08 13:54:59 -0400 | [diff] [blame] | 33 | // Concat adds the contents of another Collection to this one. |
| 34 | func (list EndpointList) Concat(other gophercloud.Collection) gophercloud.Collection { |
| 35 | return EndpointList{ |
| 36 | client: list.client, |
| 37 | Endpoints: append(list.Endpoints, AsEndpoints(other)...), |
| 38 | } |
| 39 | } |
| 40 | |
Ash Wilson | 700d13a | 2014-09-05 14:24:16 -0400 | [diff] [blame] | 41 | // Service returns the ServiceClient used to acquire this list. |
| 42 | func (list EndpointList) Service() *gophercloud.ServiceClient { |
| 43 | return list.client |
| 44 | } |
| 45 | |
| 46 | // Links accesses pagination information for the current page. |
| 47 | func (list EndpointList) Links() gophercloud.PaginationLinks { |
| 48 | return list.PaginationLinks |
| 49 | } |
| 50 | |
| 51 | // Interpret parses a follow-on JSON response as an additional page. |
| 52 | func (list EndpointList) Interpret(json interface{}) (gophercloud.LinkCollection, error) { |
| 53 | mapped, ok := json.(map[string]interface{}) |
| 54 | if !ok { |
| 55 | return nil, fmt.Errorf("Unexpected JSON response: %#v", json) |
| 56 | } |
| 57 | |
| 58 | var result EndpointList |
| 59 | err := mapstructure.Decode(mapped, &result) |
| 60 | if err != nil { |
| 61 | return nil, err |
| 62 | } |
| 63 | return result, nil |
| 64 | } |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 65 | |
| 66 | // AsEndpoints extracts an Endpoint slice from a Collection. |
| 67 | // Panics if `list` was not returned from a List call. |
| 68 | func AsEndpoints(list gophercloud.Collection) []Endpoint { |
| 69 | return list.(*EndpointList).Endpoints |
| 70 | } |