Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 1 | package apiversions |
| 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud/pagination" |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 5 | ) |
| 6 | |
Jamie Hannaford | a311f18 | 2014-09-19 11:19:10 +0200 | [diff] [blame] | 7 | // APIVersion represents an API version for Neutron. It contains the status of |
| 8 | // the API, and its unique ID. |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 9 | type APIVersion struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 10 | Status string `son:"status"` |
| 11 | ID string `json:"id"` |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 12 | } |
| 13 | |
Jamie Hannaford | a311f18 | 2014-09-19 11:19:10 +0200 | [diff] [blame] | 14 | // APIVersionPage is the page returned by a pager when traversing over a |
| 15 | // collection of API versions. |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 16 | type APIVersionPage struct { |
| 17 | pagination.SinglePageBase |
| 18 | } |
| 19 | |
Jamie Hannaford | c65e192 | 2014-09-22 13:20:58 +0200 | [diff] [blame] | 20 | // IsEmpty checks whether an APIVersionPage struct is empty. |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 21 | func (r APIVersionPage) IsEmpty() (bool, error) { |
| 22 | is, err := ExtractAPIVersions(r) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 23 | return len(is) == 0, err |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 24 | } |
| 25 | |
Jamie Hannaford | c65e192 | 2014-09-22 13:20:58 +0200 | [diff] [blame] | 26 | // ExtractAPIVersions takes a collection page, extracts all of the elements, |
Jamie Hannaford | a311f18 | 2014-09-19 11:19:10 +0200 | [diff] [blame] | 27 | // and returns them a slice of APIVersion structs. It is effectively a cast. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 28 | func ExtractAPIVersions(r pagination.Page) ([]APIVersion, error) { |
| 29 | var s struct { |
| 30 | Versions []APIVersion `json:"versions"` |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 31 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 32 | err := (r.(APIVersionPage)).ExtractInto(&s) |
| 33 | return s.Versions, err |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 34 | } |
| 35 | |
Jamie Hannaford | a311f18 | 2014-09-19 11:19:10 +0200 | [diff] [blame] | 36 | // APIVersionResource represents a generic API resource. It contains the name |
| 37 | // of the resource and its plural collection name. |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 38 | type APIVersionResource struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 39 | Name string `json:"name"` |
| 40 | Collection string `json:"collection"` |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 41 | } |
| 42 | |
Jamie Hannaford | c65e192 | 2014-09-22 13:20:58 +0200 | [diff] [blame] | 43 | // APIVersionResourcePage is a concrete type which embeds the common |
| 44 | // SinglePageBase struct, and is used when traversing API versions collections. |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 45 | type APIVersionResourcePage struct { |
| 46 | pagination.SinglePageBase |
| 47 | } |
| 48 | |
Jamie Hannaford | c65e192 | 2014-09-22 13:20:58 +0200 | [diff] [blame] | 49 | // IsEmpty is a concrete function which indicates whether an |
| 50 | // APIVersionResourcePage is empty or not. |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 51 | func (r APIVersionResourcePage) IsEmpty() (bool, error) { |
| 52 | is, err := ExtractVersionResources(r) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 53 | return len(is) == 0, err |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Jamie Hannaford | c65e192 | 2014-09-22 13:20:58 +0200 | [diff] [blame] | 56 | // ExtractVersionResources accepts a Page struct, specifically a |
| 57 | // APIVersionResourcePage struct, and extracts the elements into a slice of |
| 58 | // APIVersionResource structs. In other words, the collection is mapped into |
| 59 | // a relevant slice. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 60 | func ExtractVersionResources(r pagination.Page) ([]APIVersionResource, error) { |
| 61 | var s struct { |
| 62 | APIVersionResources []APIVersionResource `json:"resources"` |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 63 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 64 | err := (r.(APIVersionResourcePage)).ExtractInto(&s) |
| 65 | return s.APIVersionResources, err |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 66 | } |