blob: bd7a01330fefcc359aa495b8d0c47eaed6b96b1d [file] [log] [blame]
Ash Wilsonbdfc3302014-09-04 10:16:28 -04001package endpoints
2
Ash Wilson700d13a2014-09-05 14:24:16 -04003import (
4 "fmt"
5
6 "github.com/mitchellh/mapstructure"
7 "github.com/rackspace/gophercloud"
8)
Ash Wilsonbdfc3302014-09-04 10:16:28 -04009
10// Endpoint describes the entry point for another service's API.
11type Endpoint struct {
Ash Wilsonefac18b2014-09-10 14:44:42 -040012 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 Wilsonbdfc3302014-09-04 10:16:28 -040018}
19
20// EndpointList contains a page of Endpoint results.
21type EndpointList struct {
Ash Wilson8df23c82014-09-05 14:18:20 -040022 gophercloud.PaginationLinks `json:"links"`
23
Ash Wilson700d13a2014-09-05 14:24:16 -040024 client *gophercloud.ServiceClient
Ash Wilson8df23c82014-09-05 14:18:20 -040025 Endpoints []Endpoint `json:"endpoints"`
Ash Wilsonbdfc3302014-09-04 10:16:28 -040026}
Ash Wilson700d13a2014-09-05 14:24:16 -040027
28// Pager marks EndpointList as paged by links.
29func (list EndpointList) Pager() gophercloud.Pager {
30 return gophercloud.NewLinkPager(list)
31}
32
Ash Wilsonb110fc92014-09-08 13:54:59 -040033// Concat adds the contents of another Collection to this one.
34func (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 Wilson700d13a2014-09-05 14:24:16 -040041// Service returns the ServiceClient used to acquire this list.
42func (list EndpointList) Service() *gophercloud.ServiceClient {
43 return list.client
44}
45
46// Links accesses pagination information for the current page.
47func (list EndpointList) Links() gophercloud.PaginationLinks {
48 return list.PaginationLinks
49}
50
51// Interpret parses a follow-on JSON response as an additional page.
52func (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 Wilson0555c642014-09-05 16:57:17 -040065
66// AsEndpoints extracts an Endpoint slice from a Collection.
67// Panics if `list` was not returned from a List call.
68func AsEndpoints(list gophercloud.Collection) []Endpoint {
69 return list.(*EndpointList).Endpoints
70}