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 { |
| 12 | ID string `json:"id"` |
| 13 | Interface Interface `json:"interface"` |
| 14 | Name string `json:"name"` |
| 15 | Region string `json:"region"` |
| 16 | ServiceID string `json:"service_id"` |
| 17 | URL string `json:"url"` |
| 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 | |
| 33 | // Service returns the ServiceClient used to acquire this list. |
| 34 | func (list EndpointList) Service() *gophercloud.ServiceClient { |
| 35 | return list.client |
| 36 | } |
| 37 | |
| 38 | // Links accesses pagination information for the current page. |
| 39 | func (list EndpointList) Links() gophercloud.PaginationLinks { |
| 40 | return list.PaginationLinks |
| 41 | } |
| 42 | |
| 43 | // Interpret parses a follow-on JSON response as an additional page. |
| 44 | func (list EndpointList) Interpret(json interface{}) (gophercloud.LinkCollection, error) { |
| 45 | mapped, ok := json.(map[string]interface{}) |
| 46 | if !ok { |
| 47 | return nil, fmt.Errorf("Unexpected JSON response: %#v", json) |
| 48 | } |
| 49 | |
| 50 | var result EndpointList |
| 51 | err := mapstructure.Decode(mapped, &result) |
| 52 | if err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | return result, nil |
| 56 | } |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 57 | |
| 58 | // AsEndpoints extracts an Endpoint slice from a Collection. |
| 59 | // Panics if `list` was not returned from a List call. |
| 60 | func AsEndpoints(list gophercloud.Collection) []Endpoint { |
| 61 | return list.(*EndpointList).Endpoints |
| 62 | } |