blob: a6e27d274549ec1eb477a1366503015627392e95 [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.
Jon Perritt31b66462016-02-25 22:25:30 -060056func ExtractDatastores(r pagination.Page) ([]Datastore, error) {
Jon Perritt12395212016-02-24 10:41:17 -060057 var s struct {
58 Datastores []Datastore `json:"datastores"`
Jamie Hannaford82523522015-02-17 16:53:29 +010059 }
Jon Perritt31b66462016-02-25 22:25:30 -060060 err := (r.(DatastorePage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -060061 return s.Datastores, err
Jamie Hannaford82523522015-02-17 16:53:29 +010062}
63
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010064// Extract retrieves a single Datastore struct from an operation result.
Jamie Hannaford82523522015-02-17 16:53:29 +010065func (r GetResult) Extract() (*Datastore, error) {
Jon Perritt12395212016-02-24 10:41:17 -060066 var s struct {
67 Datastore *Datastore `json:"datastore"`
Jamie Hannaford82523522015-02-17 16:53:29 +010068 }
Jon Perritt12395212016-02-24 10:41:17 -060069 err := r.ExtractInto(&s)
70 return s.Datastore, err
Jamie Hannaford82523522015-02-17 16:53:29 +010071}
72
Jon Perritt12395212016-02-24 10:41:17 -060073// VersionPage represents a page of version resources.
Jamie Hannaford82523522015-02-17 16:53:29 +010074type VersionPage struct {
75 pagination.SinglePageBase
76}
77
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010078// IsEmpty indicates whether a collection of version resources is empty.
Jamie Hannaford82523522015-02-17 16:53:29 +010079func (r VersionPage) IsEmpty() (bool, error) {
80 is, err := ExtractVersions(r)
Jon Perritt12395212016-02-24 10:41:17 -060081 return len(is) == 0, err
Jamie Hannaford82523522015-02-17 16:53:29 +010082}
83
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010084// ExtractVersions retrieves a slice of versions from a paginated collection.
Jon Perritt31b66462016-02-25 22:25:30 -060085func ExtractVersions(r pagination.Page) ([]Version, error) {
Jon Perritt12395212016-02-24 10:41:17 -060086 var s struct {
87 Versions []Version `json:"versions"`
Jamie Hannaford82523522015-02-17 16:53:29 +010088 }
Jon Perritt31b66462016-02-25 22:25:30 -060089 err := (r.(VersionPage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -060090 return s.Versions, err
Jamie Hannaford82523522015-02-17 16:53:29 +010091}
92
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010093// Extract retrieves a single Version struct from an operation result.
Jamie Hannaford82523522015-02-17 16:53:29 +010094func (r GetVersionResult) Extract() (*Version, error) {
Jon Perritt12395212016-02-24 10:41:17 -060095 var s struct {
96 Version *Version `json:"version"`
Jamie Hannaford82523522015-02-17 16:53:29 +010097 }
Jon Perritt12395212016-02-24 10:41:17 -060098 err := r.ExtractInto(&s)
99 return s.Version, err
Jamie Hannaford82523522015-02-17 16:53:29 +0100100}