blob: 128112295a22c14e2c1abcc8da188a3034e844b9 [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
Ash Wilson74e2bb82014-09-30 17:08:48 -04009type commonResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040010 gophercloud.Result
Ash Wilson74e2bb82014-09-30 17:08:48 -040011}
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
Ash Wilsond3dc2542014-10-20 10:10:48 -040024 err := mapstructure.Decode(r.Body, &res)
Ash Wilson74e2bb82014-09-30 17:08:48 -040025
Jamie Hannaforda253adf2014-10-08 17:14:24 +020026 return &res.Endpoint, err
Ash Wilson74e2bb82014-09-30 17:08:48 -040027}
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 {
Ash Wilsonf548aad2014-10-20 08:35:34 -040036 return CreateResult{commonResult{gophercloud.Result{Err: err}}}
Ash Wilson74e2bb82014-09-30 17:08:48 -040037}
38
39// UpdateResult is the deferred result of an Update call.
40type UpdateResult struct {
41 commonResult
42}
43
Jamie Hannaford3c086742014-10-27 11:32:16 +010044// DeleteResult is the deferred result of an Delete call.
45type DeleteResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050046 gophercloud.ErrResult
Jamie Hannaford3c086742014-10-27 11:32:16 +010047}
48
Ash Wilsonbdfc3302014-09-04 10:16:28 -040049// Endpoint describes the entry point for another service's API.
50type Endpoint struct {
Ash Wilson6269f252014-09-12 14:33:56 -040051 ID string `mapstructure:"id" json:"id"`
52 Availability gophercloud.Availability `mapstructure:"interface" json:"interface"`
53 Name string `mapstructure:"name" json:"name"`
54 Region string `mapstructure:"region" json:"region"`
55 ServiceID string `mapstructure:"service_id" json:"service_id"`
56 URL string `mapstructure:"url" json:"url"`
Ash Wilsonbdfc3302014-09-04 10:16:28 -040057}
58
Ash Wilson3c8cc772014-09-16 11:40:49 -040059// EndpointPage is a single page of Endpoint results.
60type EndpointPage struct {
61 pagination.LinkedPageBase
62}
63
64// IsEmpty returns true if no Endpoints were returned.
65func (p EndpointPage) IsEmpty() (bool, error) {
66 es, err := ExtractEndpoints(p)
67 if err != nil {
68 return true, err
69 }
70 return len(es) == 0, nil
71}
72
Ash Wilson6269f252014-09-12 14:33:56 -040073// ExtractEndpoints extracts an Endpoint slice from a Page.
Ash Wilson3c8cc772014-09-16 11:40:49 -040074func ExtractEndpoints(page pagination.Page) ([]Endpoint, error) {
Ash Wilson6269f252014-09-12 14:33:56 -040075 var response struct {
76 Endpoints []Endpoint `mapstructure:"endpoints"`
Ash Wilson700d13a2014-09-05 14:24:16 -040077 }
78
Ash Wilson3c8cc772014-09-16 11:40:49 -040079 err := mapstructure.Decode(page.(EndpointPage).Body, &response)
Jamie Hannaforda253adf2014-10-08 17:14:24 +020080
81 return response.Endpoints, err
Ash Wilson0555c642014-09-05 16:57:17 -040082}