blob: 2788f16540b2878473d538aa54da05a861a207f1 [file] [log] [blame]
Ash Wilsonbdfc3302014-09-04 10:16:28 -04001package endpoints
2
Ash Wilson700d13a2014-09-05 14:24:16 -04003import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Ash Wilson700d13a2014-09-05 14:24:16 -04006)
Ash Wilsonbdfc3302014-09-04 10:16:28 -04007
Ash Wilson74e2bb82014-09-30 17:08:48 -04008type commonResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -04009 gophercloud.Result
Ash Wilson74e2bb82014-09-30 17:08:48 -040010}
11
12// Extract interprets a GetResult, CreateResult or UpdateResult as a concrete Endpoint.
13// An error is returned if the original call or the extraction failed.
14func (r commonResult) Extract() (*Endpoint, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060015 var s struct {
16 Endpoint *Endpoint `json:"endpoint"`
Ash Wilson74e2bb82014-09-30 17:08:48 -040017 }
Jon Perritt3c166472016-02-25 03:07:41 -060018 err := r.ExtractInto(&s)
19 return s.Endpoint, err
Ash Wilson74e2bb82014-09-30 17:08:48 -040020}
21
22// CreateResult is the deferred result of a Create call.
23type CreateResult struct {
24 commonResult
25}
26
27// createErr quickly wraps an error in a CreateResult.
28func createErr(err error) CreateResult {
Ash Wilsonf548aad2014-10-20 08:35:34 -040029 return CreateResult{commonResult{gophercloud.Result{Err: err}}}
Ash Wilson74e2bb82014-09-30 17:08:48 -040030}
31
32// UpdateResult is the deferred result of an Update call.
33type UpdateResult struct {
34 commonResult
35}
36
Jamie Hannaford3c086742014-10-27 11:32:16 +010037// DeleteResult is the deferred result of an Delete call.
38type DeleteResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050039 gophercloud.ErrResult
Jamie Hannaford3c086742014-10-27 11:32:16 +010040}
41
Ash Wilsonbdfc3302014-09-04 10:16:28 -040042// Endpoint describes the entry point for another service's API.
43type Endpoint struct {
Jon Perritt3c166472016-02-25 03:07:41 -060044 ID string `json:"id"`
45 Availability gophercloud.Availability `json:"interface"`
46 Name string `json:"name"`
47 Region string `json:"region"`
48 ServiceID string `json:"service_id"`
49 URL string `json:"url"`
Ash Wilsonbdfc3302014-09-04 10:16:28 -040050}
51
Ash Wilson3c8cc772014-09-16 11:40:49 -040052// EndpointPage is a single page of Endpoint results.
53type EndpointPage struct {
54 pagination.LinkedPageBase
55}
56
57// IsEmpty returns true if no Endpoints were returned.
Jon Perritt31b66462016-02-25 22:25:30 -060058func (r EndpointPage) IsEmpty() (bool, error) {
59 es, err := ExtractEndpoints(r)
Jon Perritt3c166472016-02-25 03:07:41 -060060 return len(es) == 0, err
Ash Wilson3c8cc772014-09-16 11:40:49 -040061}
62
Ash Wilson6269f252014-09-12 14:33:56 -040063// ExtractEndpoints extracts an Endpoint slice from a Page.
Jon Perritt31b66462016-02-25 22:25:30 -060064func ExtractEndpoints(r pagination.Page) ([]Endpoint, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060065 var s struct {
66 Endpoints []Endpoint `json:"endpoints"`
Ash Wilson700d13a2014-09-05 14:24:16 -040067 }
Jon Perritt31b66462016-02-25 22:25:30 -060068 err := (r.(EndpointPage)).ExtractInto(&s)
Jon Perritt3c166472016-02-25 03:07:41 -060069 return s.Endpoints, err
Ash Wilson0555c642014-09-05 16:57:17 -040070}