blob: d1c2472e8eb4075e4fdc0aa7893281a05d99e84b [file] [log] [blame]
Jamie Hannaford8c072a32014-10-16 14:33:32 +02001package endpoints
2
3import (
4 "github.com/mitchellh/mapstructure"
5 "github.com/rackspace/gophercloud"
6 "github.com/rackspace/gophercloud/pagination"
7)
8
9type commonResult struct {
10 gophercloud.CommonResult
11}
12
13// Extract interprets a GetResult, CreateResult or UpdateResult as a concrete Endpoint.
14// An error is returned if the original call or the extraction failed.
15func (r commonResult) Extract() (*Endpoint, error) {
16 if r.Err != nil {
17 return nil, r.Err
18 }
19
20 var res struct {
21 Endpoint `json:"endpoint"`
22 }
23
24 err := mapstructure.Decode(r.Resp, &res)
25
26 return &res.Endpoint, err
27}
28
29// CreateResult is the deferred result of a Create call.
30type CreateResult struct {
31 commonResult
32}
33
34// createErr quickly wraps an error in a CreateResult.
35func createErr(err error) CreateResult {
36 return CreateResult{commonResult{gophercloud.CommonResult{Err: err}}}
37}
38
39// UpdateResult is the deferred result of an Update call.
40type UpdateResult struct {
41 commonResult
42}
43
44// Endpoint describes the entry point for another service's API.
45type Endpoint struct {
46 ID string `mapstructure:"id" json:"id"`
47 Availability gophercloud.Availability `mapstructure:"interface" json:"interface"`
48 Name string `mapstructure:"name" json:"name"`
49 Region string `mapstructure:"region" json:"region"`
50 ServiceID string `mapstructure:"service_id" json:"service_id"`
51 URL string `mapstructure:"url" json:"url"`
52}
53
54// EndpointPage is a single page of Endpoint results.
55type EndpointPage struct {
56 pagination.LinkedPageBase
57}
58
59// IsEmpty returns true if no Endpoints were returned.
60func (p EndpointPage) IsEmpty() (bool, error) {
61 es, err := ExtractEndpoints(p)
62 if err != nil {
63 return true, err
64 }
65 return len(es) == 0, nil
66}
67
68// ExtractEndpoints extracts an Endpoint slice from a Page.
69func ExtractEndpoints(page pagination.Page) ([]Endpoint, error) {
70 var response struct {
71 Endpoints []Endpoint `mapstructure:"endpoints"`
72 }
73
74 err := mapstructure.Decode(page.(EndpointPage).Body, &response)
75
76 return response.Endpoints, err
77}