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