blob: d3068dfa392ed9974720da880394c05a66e27592 [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 {
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.
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
33// Service returns the ServiceClient used to acquire this list.
34func (list EndpointList) Service() *gophercloud.ServiceClient {
35 return list.client
36}
37
38// Links accesses pagination information for the current page.
39func (list EndpointList) Links() gophercloud.PaginationLinks {
40 return list.PaginationLinks
41}
42
43// Interpret parses a follow-on JSON response as an additional page.
44func (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 Wilson0555c642014-09-05 16:57:17 -040057
58// AsEndpoints extracts an Endpoint slice from a Collection.
59// Panics if `list` was not returned from a List call.
60func AsEndpoints(list gophercloud.Collection) []Endpoint {
61 return list.(*EndpointList).Endpoints
62}