blob: a7250032f7ec7831ce9d2ed3621387e4f8fe99ca [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"
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02005 "gerrit.mcp.mirantis.net/debian/gophercloud.git/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
Ash Wilson74e2bb82014-09-30 17:08:48 -040027// UpdateResult is the deferred result of an Update call.
28type UpdateResult struct {
29 commonResult
30}
31
Jamie Hannaford3c086742014-10-27 11:32:16 +010032// DeleteResult is the deferred result of an Delete call.
33type DeleteResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050034 gophercloud.ErrResult
Jamie Hannaford3c086742014-10-27 11:32:16 +010035}
36
Ash Wilsonbdfc3302014-09-04 10:16:28 -040037// Endpoint describes the entry point for another service's API.
38type Endpoint struct {
Jon Perritt3c166472016-02-25 03:07:41 -060039 ID string `json:"id"`
40 Availability gophercloud.Availability `json:"interface"`
41 Name string `json:"name"`
42 Region string `json:"region"`
43 ServiceID string `json:"service_id"`
44 URL string `json:"url"`
Ash Wilsonbdfc3302014-09-04 10:16:28 -040045}
46
Ash Wilson3c8cc772014-09-16 11:40:49 -040047// EndpointPage is a single page of Endpoint results.
48type EndpointPage struct {
49 pagination.LinkedPageBase
50}
51
52// IsEmpty returns true if no Endpoints were returned.
Jon Perritt31b66462016-02-25 22:25:30 -060053func (r EndpointPage) IsEmpty() (bool, error) {
54 es, err := ExtractEndpoints(r)
Jon Perritt3c166472016-02-25 03:07:41 -060055 return len(es) == 0, err
Ash Wilson3c8cc772014-09-16 11:40:49 -040056}
57
Ash Wilson6269f252014-09-12 14:33:56 -040058// ExtractEndpoints extracts an Endpoint slice from a Page.
Jon Perritt31b66462016-02-25 22:25:30 -060059func ExtractEndpoints(r pagination.Page) ([]Endpoint, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060060 var s struct {
61 Endpoints []Endpoint `json:"endpoints"`
Ash Wilson700d13a2014-09-05 14:24:16 -040062 }
Jon Perritt31b66462016-02-25 22:25:30 -060063 err := (r.(EndpointPage)).ExtractInto(&s)
Jon Perritt3c166472016-02-25 03:07:41 -060064 return s.Endpoints, err
Ash Wilson0555c642014-09-05 16:57:17 -040065}