blob: 7b0df115b507bafb858fd30d39c700f33df5f6d6 [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)
Jon Perrittfd53bba2014-10-03 00:41:22 -050040
Jamie Hannaford6a83e802014-10-08 17:13:50 +020041 return resp.Versions, err
Jon Perrittfd53bba2014-10-03 00:41:22 -050042}
43
Jamie Hannafordd8275bb2014-10-06 16:12:23 +020044// GetResult represents the result of a get operation.
Jon Perrittfd53bba2014-10-03 00:41:22 -050045type GetResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040046 gophercloud.Result
Jon Perrittfd53bba2014-10-03 00:41:22 -050047}
48
Jamie Hannafordd8275bb2014-10-06 16:12:23 +020049// Extract is a function that accepts a result and extracts an API version resource.
Jon Perrittfd53bba2014-10-03 00:41:22 -050050func (r GetResult) Extract() (*APIVersion, error) {
51 var resp struct {
52 Version *APIVersion `mapstructure:"version"`
53 }
54
Ash Wilsond3dc2542014-10-20 10:10:48 -040055 err := mapstructure.Decode(r.Body, &resp)
Jon Perrittfd53bba2014-10-03 00:41:22 -050056
Jamie Hannaford6a83e802014-10-08 17:13:50 +020057 return resp.Version, err
Jon Perrittfd53bba2014-10-03 00:41:22 -050058}