blob: a0b3ee867911def48733ba3595c65b6424412cb5 [file] [log] [blame]
Jamie Hannaford82523522015-02-17 16:53:29 +01001package datastores
2
3import (
4 "github.com/mitchellh/mapstructure"
Jon Perritt27249f42016-02-18 10:35:59 -06005 "github.com/gophercloud/gophercloud"
6 "github.com/gophercloud/gophercloud/pagination"
Jamie Hannaford82523522015-02-17 16:53:29 +01007)
8
Jamie Hannafordb0d267b2015-02-19 11:59:53 +01009// Version represents a version API resource. Multiple versions belong to a Datastore.
Jamie Hannaford82523522015-02-17 16:53:29 +010010type Version struct {
11 ID string
12 Links []gophercloud.Link
13 Name string
14}
15
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010016// Datastore represents a Datastore API resource.
Jamie Hannaford82523522015-02-17 16:53:29 +010017type Datastore struct {
18 DefaultVersion string `json:"default_version" mapstructure:"default_version"`
19 ID string
20 Links []gophercloud.Link
21 Name string
22 Versions []Version
23}
24
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010025// DatastorePartial is a meta structure which is used in various API responses.
26// It is a lightweight and truncated version of a full Datastore resource,
27// offering details of the Version, Type and VersionID only.
Jamie Hannaforda50d1352015-02-18 11:38:38 +010028type DatastorePartial struct {
29 Version string
30 Type string
31 VersionID string `json:"version_id" mapstructure:"version_id"`
32}
33
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010034// GetResult represents the result of a Get operation.
Jamie Hannaford82523522015-02-17 16:53:29 +010035type GetResult struct {
36 gophercloud.Result
37}
38
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010039// GetVersionResult represents the result of getting a version.
Jamie Hannaford82523522015-02-17 16:53:29 +010040type GetVersionResult struct {
41 gophercloud.Result
42}
43
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010044// DatastorePage represents a page of datastore resources.
Jamie Hannaford82523522015-02-17 16:53:29 +010045type DatastorePage struct {
46 pagination.SinglePageBase
47}
48
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010049// IsEmpty indicates whether a Datastore collection is empty.
Jamie Hannaford82523522015-02-17 16:53:29 +010050func (r DatastorePage) IsEmpty() (bool, error) {
51 is, err := ExtractDatastores(r)
52 if err != nil {
53 return true, err
54 }
55 return len(is) == 0, nil
56}
57
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010058// ExtractDatastores retrieves a slice of datastore structs from a paginated
59// collection.
Jamie Hannaford82523522015-02-17 16:53:29 +010060func ExtractDatastores(page pagination.Page) ([]Datastore, error) {
61 casted := page.(DatastorePage).Body
62
63 var resp struct {
64 Datastores []Datastore `mapstructure:"datastores" json:"datastores"`
65 }
66
67 err := mapstructure.Decode(casted, &resp)
68 return resp.Datastores, err
69}
70
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010071// Extract retrieves a single Datastore struct from an operation result.
Jamie Hannaford82523522015-02-17 16:53:29 +010072func (r GetResult) Extract() (*Datastore, error) {
73 if r.Err != nil {
74 return nil, r.Err
75 }
76
77 var response struct {
78 Datastore Datastore `mapstructure:"datastore"`
79 }
80
81 err := mapstructure.Decode(r.Body, &response)
82 return &response.Datastore, err
83}
84
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010085// DatastorePage represents a page of version resources.
Jamie Hannaford82523522015-02-17 16:53:29 +010086type VersionPage struct {
87 pagination.SinglePageBase
88}
89
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010090// IsEmpty indicates whether a collection of version resources is empty.
Jamie Hannaford82523522015-02-17 16:53:29 +010091func (r VersionPage) IsEmpty() (bool, error) {
92 is, err := ExtractVersions(r)
93 if err != nil {
94 return true, err
95 }
96 return len(is) == 0, nil
97}
98
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010099// ExtractVersions retrieves a slice of versions from a paginated collection.
Jamie Hannaford82523522015-02-17 16:53:29 +0100100func ExtractVersions(page pagination.Page) ([]Version, error) {
101 casted := page.(VersionPage).Body
102
103 var resp struct {
104 Versions []Version `mapstructure:"versions" json:"versions"`
105 }
106
107 err := mapstructure.Decode(casted, &resp)
108 return resp.Versions, err
109}
110
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100111// Extract retrieves a single Version struct from an operation result.
Jamie Hannaford82523522015-02-17 16:53:29 +0100112func (r GetVersionResult) Extract() (*Version, error) {
113 if r.Err != nil {
114 return nil, r.Err
115 }
116
117 var response struct {
118 Version Version `mapstructure:"version"`
119 }
120
121 err := mapstructure.Decode(r.Body, &response)
122 return &response.Version, err
123}