Jon Perritt | fd53bba | 2014-10-03 00:41:22 -0500 | [diff] [blame] | 1 | package apiversions |
| 2 | |
| 3 | import ( |
| 4 | "github.com/rackspace/gophercloud" |
| 5 | "github.com/rackspace/gophercloud/pagination" |
| 6 | |
| 7 | "github.com/mitchellh/mapstructure" |
| 8 | ) |
| 9 | |
| 10 | // APIVersion represents an API version for Cinder. |
| 11 | type APIVersion struct { |
| 12 | ID string `json:"id" mapstructure:"id"` // unique identifier |
| 13 | Status string `json:"status" mapstructure:"status"` // current status |
| 14 | Updated string `json:"updated" mapstructure:"updated"` // date last updated |
| 15 | } |
| 16 | |
| 17 | // APIVersionPage is the page returned by a pager when traversing over a |
| 18 | // collection of API versions. |
| 19 | type APIVersionPage struct { |
| 20 | pagination.SinglePageBase |
| 21 | } |
| 22 | |
| 23 | // IsEmpty checks whether an APIVersionPage struct is empty. |
| 24 | func (r APIVersionPage) IsEmpty() (bool, error) { |
| 25 | is, err := ExtractAPIVersions(r) |
| 26 | if err != nil { |
| 27 | return true, err |
| 28 | } |
| 29 | return len(is) == 0, nil |
| 30 | } |
| 31 | |
| 32 | // ExtractAPIVersions takes a collection page, extracts all of the elements, |
| 33 | // and returns them a slice of APIVersion structs. It is effectively a cast. |
| 34 | func ExtractAPIVersions(page pagination.Page) ([]APIVersion, error) { |
| 35 | var resp struct { |
| 36 | Versions []APIVersion `mapstructure:"versions"` |
| 37 | } |
| 38 | |
| 39 | err := mapstructure.Decode(page.(APIVersionPage).Body, &resp) |
Jon Perritt | fd53bba | 2014-10-03 00:41:22 -0500 | [diff] [blame] | 40 | |
Jamie Hannaford | 6a83e80 | 2014-10-08 17:13:50 +0200 | [diff] [blame] | 41 | return resp.Versions, err |
Jon Perritt | fd53bba | 2014-10-03 00:41:22 -0500 | [diff] [blame] | 42 | } |
| 43 | |
Jamie Hannaford | d8275bb | 2014-10-06 16:12:23 +0200 | [diff] [blame] | 44 | // GetResult represents the result of a get operation. |
Jon Perritt | fd53bba | 2014-10-03 00:41:22 -0500 | [diff] [blame] | 45 | type GetResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 46 | gophercloud.Result |
Jon Perritt | fd53bba | 2014-10-03 00:41:22 -0500 | [diff] [blame] | 47 | } |
| 48 | |
Jamie Hannaford | d8275bb | 2014-10-06 16:12:23 +0200 | [diff] [blame] | 49 | // Extract is a function that accepts a result and extracts an API version resource. |
Jon Perritt | fd53bba | 2014-10-03 00:41:22 -0500 | [diff] [blame] | 50 | func (r GetResult) Extract() (*APIVersion, error) { |
| 51 | var resp struct { |
| 52 | Version *APIVersion `mapstructure:"version"` |
| 53 | } |
| 54 | |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 55 | err := mapstructure.Decode(r.Body, &resp) |
Jon Perritt | fd53bba | 2014-10-03 00:41:22 -0500 | [diff] [blame] | 56 | |
Jamie Hannaford | 6a83e80 | 2014-10-08 17:13:50 +0200 | [diff] [blame] | 57 | return resp.Version, err |
Jon Perritt | fd53bba | 2014-10-03 00:41:22 -0500 | [diff] [blame] | 58 | } |