blob: eeff13297a806bf3983faedf444a6ab2d5b9c01e [file] [log] [blame]
Jon Perrittfd53bba2014-10-03 00:41:22 -05001package apiversions
2
3import (
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.
11type 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.
19type APIVersionPage struct {
20 pagination.SinglePageBase
21}
22
23// IsEmpty checks whether an APIVersionPage struct is empty.
24func (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.
34func 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)
40 if err != nil {
41 return nil, err
42 }
43
44 return resp.Versions, nil
45}
46
47type GetResult struct {
48 gophercloud.CommonResult
49}
50
51func (r GetResult) Extract() (*APIVersion, error) {
52 var resp struct {
53 Version *APIVersion `mapstructure:"version"`
54 }
55
56 err := mapstructure.Decode(r.Resp, &resp)
57 if err != nil {
58 return nil, err
59 }
60
61 return resp.Version, nil
62}