blob: 40b26aea06278947bdc5c0d7c494c3f4efec486f [file] [log] [blame]
Jon Perrittdf38cca2014-12-04 10:59:04 -07001package apiversions
2
3import (
4 "github.com/mitchellh/mapstructure"
5 "github.com/rackspace/gophercloud/pagination"
6)
7
8// Link represents the base URL for a particular version.
9type Link struct {
10 HRef string `mapstructure:"href"`
11 Rel string `mapstructure:"rel"`
12}
13
14// APIVersion represents an API version for Neutron. It contains the status of
15// the API, and its unique ID.
16type APIVersion struct {
17 Status string `mapstructure:"status"`
18 ID string `mapstructure:"id"`
19 Links []Link `mapstructure:"links"`
20}
21
22// APIVersionPage is the page returned by a pager when traversing over a
23// collection of API versions.
24type APIVersionPage struct {
25 pagination.SinglePageBase
26}
27
28// IsEmpty checks whether an APIVersionPage struct is empty.
29func (r APIVersionPage) IsEmpty() (bool, error) {
30 is, err := ExtractAPIVersions(r)
31 if err != nil {
32 return true, err
33 }
34 return len(is) == 0, nil
35}
36
37// ExtractAPIVersions takes a collection page, extracts all of the elements,
38// and returns them a slice of APIVersion structs. It is effectively a cast.
39func ExtractAPIVersions(page pagination.Page) ([]APIVersion, error) {
40 var resp struct {
41 Versions []APIVersion `mapstructure:"versions"`
42 }
43
44 err := mapstructure.Decode(page.(APIVersionPage).Body, &resp)
45
46 return resp.Versions, err
47}