blob: 9181e7e9fb498e1f7b19cc148deed20468945621 [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
8type APIVersion struct {
9 Status string `mapstructure:"status" json:"status"`
10 ID string `mapstructure:"id" json:"id"`
11}
12
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020013type APIVersionPage struct {
14 pagination.SinglePageBase
15}
16
17func (r APIVersionPage) IsEmpty() (bool, error) {
18 is, err := ExtractAPIVersions(r)
19 if err != nil {
20 return true, err
21 }
22 return len(is) == 0, nil
23}
24
25func ExtractAPIVersions(page pagination.Page) ([]APIVersion, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020026 var resp struct {
27 Versions []APIVersion `mapstructure:"versions"`
28 }
29
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020030 err := mapstructure.Decode(page.(APIVersionPage).Body, &resp)
Jamie Hannaford4721abc2014-09-16 16:29:04 +020031 if err != nil {
32 return nil, err
33 }
34
35 return resp.Versions, nil
36}
37
38type APIVersionResource struct {
39 Name string `mapstructure:"name" json:"name"`
40 Collection string `mapstructure:"collection" json:"collection"`
41}
42
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020043type APIVersionResourcePage struct {
44 pagination.SinglePageBase
45}
46
47func (r APIVersionResourcePage) IsEmpty() (bool, error) {
48 is, err := ExtractVersionResources(r)
49 if err != nil {
50 return true, err
51 }
52 return len(is) == 0, nil
53}
54
55func ExtractVersionResources(page pagination.Page) ([]APIVersionResource, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020056 var resp struct {
57 APIVersionResources []APIVersionResource `mapstructure:"resources"`
58 }
59
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020060 err := mapstructure.Decode(page.(APIVersionResourcePage).Body, &resp)
Jamie Hannaford4721abc2014-09-16 16:29:04 +020061 if err != nil {
62 return nil, err
63 }
64
65 return resp.APIVersionResources, nil
66}