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" |
| 5 | "github.com/gophercloud/gophercloud/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 | |
| 27 | // createErr quickly wraps an error in a CreateResult. |
| 28 | func createErr(err error) CreateResult { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 29 | return CreateResult{commonResult{gophercloud.Result{Err: err}}} |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | // UpdateResult is the deferred result of an Update call. |
| 33 | type UpdateResult struct { |
| 34 | commonResult |
| 35 | } |
| 36 | |
Jamie Hannaford | 3c08674 | 2014-10-27 11:32:16 +0100 | [diff] [blame] | 37 | // DeleteResult is the deferred result of an Delete call. |
| 38 | type DeleteResult struct { |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 39 | gophercloud.ErrResult |
Jamie Hannaford | 3c08674 | 2014-10-27 11:32:16 +0100 | [diff] [blame] | 40 | } |
| 41 | |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 42 | // Endpoint describes the entry point for another service's API. |
| 43 | type Endpoint struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 44 | 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 Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 50 | } |
| 51 | |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 52 | // EndpointPage is a single page of Endpoint results. |
| 53 | type EndpointPage struct { |
| 54 | pagination.LinkedPageBase |
| 55 | } |
| 56 | |
| 57 | // IsEmpty returns true if no Endpoints were returned. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 58 | func (r EndpointPage) IsEmpty() (bool, error) { |
| 59 | es, err := ExtractEndpoints(r) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 60 | return len(es) == 0, err |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 61 | } |
| 62 | |
Ash Wilson | 6269f25 | 2014-09-12 14:33:56 -0400 | [diff] [blame] | 63 | // ExtractEndpoints extracts an Endpoint slice from a Page. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 64 | func ExtractEndpoints(r pagination.Page) ([]Endpoint, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 65 | var s struct { |
| 66 | Endpoints []Endpoint `json:"endpoints"` |
Ash Wilson | 700d13a | 2014-09-05 14:24:16 -0400 | [diff] [blame] | 67 | } |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 68 | err := (r.(EndpointPage)).ExtractInto(&s) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 69 | return s.Endpoints, err |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 70 | } |