blob: 31fd838043cd3e81d973ed9144cf15ec9afc239c [file] [log] [blame]
Jamie Hannaford82523522015-02-17 16:53:29 +01001package datastores
2
3import (
4 "github.com/mitchellh/mapstructure"
5 "github.com/rackspace/gophercloud"
6 "github.com/rackspace/gophercloud/pagination"
7)
8
9type Version struct {
10 ID string
11 Links []gophercloud.Link
12 Name string
13}
14
15type Datastore struct {
16 DefaultVersion string `json:"default_version" mapstructure:"default_version"`
17 ID string
18 Links []gophercloud.Link
19 Name string
20 Versions []Version
21}
22
Jamie Hannaforda50d1352015-02-18 11:38:38 +010023type DatastorePartial struct {
24 Version string
25 Type string
26 VersionID string `json:"version_id" mapstructure:"version_id"`
27}
28
Jamie Hannaford82523522015-02-17 16:53:29 +010029type GetResult struct {
30 gophercloud.Result
31}
32
33type GetVersionResult struct {
34 gophercloud.Result
35}
36
37type DatastorePage struct {
38 pagination.SinglePageBase
39}
40
41func (r DatastorePage) IsEmpty() (bool, error) {
42 is, err := ExtractDatastores(r)
43 if err != nil {
44 return true, err
45 }
46 return len(is) == 0, nil
47}
48
49func ExtractDatastores(page pagination.Page) ([]Datastore, error) {
50 casted := page.(DatastorePage).Body
51
52 var resp struct {
53 Datastores []Datastore `mapstructure:"datastores" json:"datastores"`
54 }
55
56 err := mapstructure.Decode(casted, &resp)
57 return resp.Datastores, err
58}
59
60func (r GetResult) Extract() (*Datastore, error) {
61 if r.Err != nil {
62 return nil, r.Err
63 }
64
65 var response struct {
66 Datastore Datastore `mapstructure:"datastore"`
67 }
68
69 err := mapstructure.Decode(r.Body, &response)
70 return &response.Datastore, err
71}
72
73type VersionPage struct {
74 pagination.SinglePageBase
75}
76
77func (r VersionPage) IsEmpty() (bool, error) {
78 is, err := ExtractVersions(r)
79 if err != nil {
80 return true, err
81 }
82 return len(is) == 0, nil
83}
84
85func ExtractVersions(page pagination.Page) ([]Version, error) {
86 casted := page.(VersionPage).Body
87
88 var resp struct {
89 Versions []Version `mapstructure:"versions" json:"versions"`
90 }
91
92 err := mapstructure.Decode(casted, &resp)
93 return resp.Versions, err
94}
95
96func (r GetVersionResult) Extract() (*Version, error) {
97 if r.Err != nil {
98 return nil, r.Err
99 }
100
101 var response struct {
102 Version Version `mapstructure:"version"`
103 }
104
105 err := mapstructure.Decode(r.Body, &response)
106 return &response.Version, err
107}