blob: e13998da9a3b9e5f5e129377ba883a52839d7d07 [file] [log] [blame]
Jamie Hannaford4721abc2014-09-16 16:29:04 +02001package apiversions
2
3import (
4 "github.com/mitchellh/mapstructure"
Jamie Hannafordf0c615b2014-09-17 10:56:52 +02005 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford4721abc2014-09-16 16:29:04 +02006)
7
Jamie Hannaforda311f182014-09-19 11:19:10 +02008// APIVersion represents an API version for Neutron. It contains the status of
9// the API, and its unique ID.
Jamie Hannaford4721abc2014-09-16 16:29:04 +020010type APIVersion struct {
11 Status string `mapstructure:"status" json:"status"`
12 ID string `mapstructure:"id" json:"id"`
13}
14
Jamie Hannaforda311f182014-09-19 11:19:10 +020015// APIVersionPage is the page returned by a pager when traversing over a
16// collection of API versions.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020017type APIVersionPage struct {
18 pagination.SinglePageBase
19}
20
Jamie Hannafordc65e1922014-09-22 13:20:58 +020021// IsEmpty checks whether an APIVersionPage struct is empty.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020022func (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 Hannafordc65e1922014-09-22 13:20:58 +020030// ExtractAPIVersions takes a collection page, extracts all of the elements,
Jamie Hannaforda311f182014-09-19 11:19:10 +020031// and returns them a slice of APIVersion structs. It is effectively a cast.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020032func ExtractAPIVersions(page pagination.Page) ([]APIVersion, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020033 var resp struct {
34 Versions []APIVersion `mapstructure:"versions"`
35 }
36
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020037 err := mapstructure.Decode(page.(APIVersionPage).Body, &resp)
Jamie Hannaford4721abc2014-09-16 16:29:04 +020038 if err != nil {
39 return nil, err
40 }
41
42 return resp.Versions, nil
43}
44
Jamie Hannaforda311f182014-09-19 11:19:10 +020045// APIVersionResource represents a generic API resource. It contains the name
46// of the resource and its plural collection name.
Jamie Hannaford4721abc2014-09-16 16:29:04 +020047type APIVersionResource struct {
48 Name string `mapstructure:"name" json:"name"`
49 Collection string `mapstructure:"collection" json:"collection"`
50}
51
Jamie Hannafordc65e1922014-09-22 13:20:58 +020052// APIVersionResourcePage is a concrete type which embeds the common
53// SinglePageBase struct, and is used when traversing API versions collections.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020054type APIVersionResourcePage struct {
55 pagination.SinglePageBase
56}
57
Jamie Hannafordc65e1922014-09-22 13:20:58 +020058// IsEmpty is a concrete function which indicates whether an
59// APIVersionResourcePage is empty or not.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020060func (r APIVersionResourcePage) IsEmpty() (bool, error) {
61 is, err := ExtractVersionResources(r)
62 if err != nil {
63 return true, err
64 }
65 return len(is) == 0, nil
66}
67
Jamie Hannafordc65e1922014-09-22 13:20:58 +020068// ExtractVersionResources accepts a Page struct, specifically a
69// APIVersionResourcePage struct, and extracts the elements into a slice of
70// APIVersionResource structs. In other words, the collection is mapped into
71// a relevant slice.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020072func ExtractVersionResources(page pagination.Page) ([]APIVersionResource, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020073 var resp struct {
74 APIVersionResources []APIVersionResource `mapstructure:"resources"`
75 }
76
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020077 err := mapstructure.Decode(page.(APIVersionResourcePage).Body, &resp)
Jamie Hannaford4721abc2014-09-16 16:29:04 +020078 if err != nil {
79 return nil, err
80 }
81
82 return resp.APIVersionResources, nil
83}