blob: 8da90f3120890f0e5ebee5b3fa2543fc45ef2669 [file] [log] [blame]
Ash Wilsonbdfc3302014-09-04 10:16:28 -04001package endpoints
2
Ash Wilson700d13a2014-09-05 14:24:16 -04003import (
Ash Wilson700d13a2014-09-05 14:24:16 -04004 "github.com/mitchellh/mapstructure"
5 "github.com/rackspace/gophercloud"
Ash Wilson3c8cc772014-09-16 11:40:49 -04006 "github.com/rackspace/gophercloud/pagination"
Ash Wilson700d13a2014-09-05 14:24:16 -04007)
Ash Wilsonbdfc3302014-09-04 10:16:28 -04008
9// Endpoint describes the entry point for another service's API.
10type Endpoint struct {
Ash Wilson6269f252014-09-12 14:33:56 -040011 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 Wilsonbdfc3302014-09-04 10:16:28 -040017}
18
Ash Wilson3c8cc772014-09-16 11:40:49 -040019// EndpointPage is a single page of Endpoint results.
20type EndpointPage struct {
21 pagination.LinkedPageBase
22}
23
24// IsEmpty returns true if no Endpoints were returned.
25func (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 Wilson6269f252014-09-12 14:33:56 -040033// ExtractEndpoints extracts an Endpoint slice from a Page.
Ash Wilson3c8cc772014-09-16 11:40:49 -040034func ExtractEndpoints(page pagination.Page) ([]Endpoint, error) {
Ash Wilson6269f252014-09-12 14:33:56 -040035 var response struct {
36 Endpoints []Endpoint `mapstructure:"endpoints"`
Ash Wilson700d13a2014-09-05 14:24:16 -040037 }
38
Ash Wilson3c8cc772014-09-16 11:40:49 -040039 err := mapstructure.Decode(page.(EndpointPage).Body, &response)
Ash Wilson700d13a2014-09-05 14:24:16 -040040 if err != nil {
41 return nil, err
42 }
Ash Wilson6269f252014-09-12 14:33:56 -040043 return response.Endpoints, nil
Ash Wilson0555c642014-09-05 16:57:17 -040044}