Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 1 | package endpoints |
| 2 | |
Ash Wilson | 700d13a | 2014-09-05 14:24:16 -0400 | [diff] [blame] | 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
Krzysztof Szukiełojć | 24a29ce | 2017-05-07 14:24:02 +0200 | [diff] [blame^] | 5 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination" |
Ash Wilson | 700d13a | 2014-09-05 14:24:16 -0400 | [diff] [blame] | 6 | ) |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 7 | |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 8 | type commonResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 9 | gophercloud.Result |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 10 | } |
| 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. |
| 14 | func (r commonResult) Extract() (*Endpoint, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 15 | var s struct { |
| 16 | Endpoint *Endpoint `json:"endpoint"` |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 17 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 18 | err := r.ExtractInto(&s) |
| 19 | return s.Endpoint, err |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | // CreateResult is the deferred result of a Create call. |
| 23 | type CreateResult struct { |
| 24 | commonResult |
| 25 | } |
| 26 | |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 27 | // UpdateResult is the deferred result of an Update call. |
| 28 | type UpdateResult struct { |
| 29 | commonResult |
| 30 | } |
| 31 | |
Jamie Hannaford | 3c08674 | 2014-10-27 11:32:16 +0100 | [diff] [blame] | 32 | // DeleteResult is the deferred result of an Delete call. |
| 33 | type DeleteResult struct { |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 34 | gophercloud.ErrResult |
Jamie Hannaford | 3c08674 | 2014-10-27 11:32:16 +0100 | [diff] [blame] | 35 | } |
| 36 | |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 37 | // Endpoint describes the entry point for another service's API. |
| 38 | type Endpoint struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 39 | 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 Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 45 | } |
| 46 | |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 47 | // EndpointPage is a single page of Endpoint results. |
| 48 | type EndpointPage struct { |
| 49 | pagination.LinkedPageBase |
| 50 | } |
| 51 | |
| 52 | // IsEmpty returns true if no Endpoints were returned. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 53 | func (r EndpointPage) IsEmpty() (bool, error) { |
| 54 | es, err := ExtractEndpoints(r) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 55 | return len(es) == 0, err |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 56 | } |
| 57 | |
Ash Wilson | 6269f25 | 2014-09-12 14:33:56 -0400 | [diff] [blame] | 58 | // ExtractEndpoints extracts an Endpoint slice from a Page. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 59 | func ExtractEndpoints(r pagination.Page) ([]Endpoint, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 60 | var s struct { |
| 61 | Endpoints []Endpoint `json:"endpoints"` |
Ash Wilson | 700d13a | 2014-09-05 14:24:16 -0400 | [diff] [blame] | 62 | } |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 63 | err := (r.(EndpointPage)).ExtractInto(&s) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 64 | return s.Endpoints, err |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 65 | } |