blob: e8938847c05acf9128cc1ee860bbfe713d33bb42 [file] [log] [blame]
Jamie Hannaford82523522015-02-17 16:53:29 +01001package datastores
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Jamie Hannaford82523522015-02-17 16:53:29 +01006)
7
Jamie Hannafordb0d267b2015-02-19 11:59:53 +01008// Version represents a version API resource. Multiple versions belong to a Datastore.
Jamie Hannaford82523522015-02-17 16:53:29 +01009type Version struct {
10 ID string
11 Links []gophercloud.Link
12 Name string
13}
14
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010015// Datastore represents a Datastore API resource.
Jamie Hannaford82523522015-02-17 16:53:29 +010016type Datastore struct {
Jon Perritt12395212016-02-24 10:41:17 -060017 DefaultVersion string `json:"default_version"`
Jamie Hannaford82523522015-02-17 16:53:29 +010018 ID string
19 Links []gophercloud.Link
20 Name string
21 Versions []Version
22}
23
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010024// DatastorePartial is a meta structure which is used in various API responses.
25// It is a lightweight and truncated version of a full Datastore resource,
26// offering details of the Version, Type and VersionID only.
Jamie Hannaforda50d1352015-02-18 11:38:38 +010027type DatastorePartial struct {
28 Version string
29 Type string
Jon Perritt12395212016-02-24 10:41:17 -060030 VersionID string `json:"version_id"`
Jamie Hannaforda50d1352015-02-18 11:38:38 +010031}
32
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010033// GetResult represents the result of a Get operation.
Jamie Hannaford82523522015-02-17 16:53:29 +010034type GetResult struct {
35 gophercloud.Result
36}
37
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010038// GetVersionResult represents the result of getting a version.
Jamie Hannaford82523522015-02-17 16:53:29 +010039type GetVersionResult struct {
40 gophercloud.Result
41}
42
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010043// DatastorePage represents a page of datastore resources.
Jamie Hannaford82523522015-02-17 16:53:29 +010044type DatastorePage struct {
45 pagination.SinglePageBase
46}
47
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010048// IsEmpty indicates whether a Datastore collection is empty.
Jamie Hannaford82523522015-02-17 16:53:29 +010049func (r DatastorePage) IsEmpty() (bool, error) {
50 is, err := ExtractDatastores(r)
Jon Perritt12395212016-02-24 10:41:17 -060051 return len(is) == 0, err
Jamie Hannaford82523522015-02-17 16:53:29 +010052}
53
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010054// ExtractDatastores retrieves a slice of datastore structs from a paginated
55// collection.
Jamie Hannaford82523522015-02-17 16:53:29 +010056func ExtractDatastores(page pagination.Page) ([]Datastore, error) {
Jon Perritt12395212016-02-24 10:41:17 -060057 r := page.(DatastorePage)
58 var s struct {
59 Datastores []Datastore `json:"datastores"`
Jamie Hannaford82523522015-02-17 16:53:29 +010060 }
Jon Perritt12395212016-02-24 10:41:17 -060061 err := r.ExtractInto(&s)
62 return s.Datastores, err
Jamie Hannaford82523522015-02-17 16:53:29 +010063}
64
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010065// Extract retrieves a single Datastore struct from an operation result.
Jamie Hannaford82523522015-02-17 16:53:29 +010066func (r GetResult) Extract() (*Datastore, error) {
Jon Perritt12395212016-02-24 10:41:17 -060067 var s struct {
68 Datastore *Datastore `json:"datastore"`
Jamie Hannaford82523522015-02-17 16:53:29 +010069 }
Jon Perritt12395212016-02-24 10:41:17 -060070 err := r.ExtractInto(&s)
71 return s.Datastore, err
Jamie Hannaford82523522015-02-17 16:53:29 +010072}
73
Jon Perritt12395212016-02-24 10:41:17 -060074// VersionPage represents a page of version resources.
Jamie Hannaford82523522015-02-17 16:53:29 +010075type VersionPage struct {
76 pagination.SinglePageBase
77}
78
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010079// IsEmpty indicates whether a collection of version resources is empty.
Jamie Hannaford82523522015-02-17 16:53:29 +010080func (r VersionPage) IsEmpty() (bool, error) {
81 is, err := ExtractVersions(r)
Jon Perritt12395212016-02-24 10:41:17 -060082 return len(is) == 0, err
Jamie Hannaford82523522015-02-17 16:53:29 +010083}
84
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010085// ExtractVersions retrieves a slice of versions from a paginated collection.
Jamie Hannaford82523522015-02-17 16:53:29 +010086func ExtractVersions(page pagination.Page) ([]Version, error) {
Jon Perritt12395212016-02-24 10:41:17 -060087 r := page.(VersionPage)
88 var s struct {
89 Versions []Version `json:"versions"`
Jamie Hannaford82523522015-02-17 16:53:29 +010090 }
Jon Perritt12395212016-02-24 10:41:17 -060091 err := r.ExtractInto(&s)
92 return s.Versions, err
Jamie Hannaford82523522015-02-17 16:53:29 +010093}
94
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010095// Extract retrieves a single Version struct from an operation result.
Jamie Hannaford82523522015-02-17 16:53:29 +010096func (r GetVersionResult) Extract() (*Version, error) {
Jon Perritt12395212016-02-24 10:41:17 -060097 var s struct {
98 Version *Version `json:"version"`
Jamie Hannaford82523522015-02-17 16:53:29 +010099 }
Jon Perritt12395212016-02-24 10:41:17 -0600100 err := r.ExtractInto(&s)
101 return s.Version, err
Jamie Hannaford82523522015-02-17 16:53:29 +0100102}