Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 1 | package datastores |
| 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 6 | ) |
| 7 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 8 | // Version represents a version API resource. Multiple versions belong to a Datastore. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 9 | type Version struct { |
| 10 | ID string |
| 11 | Links []gophercloud.Link |
| 12 | Name string |
| 13 | } |
| 14 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 15 | // Datastore represents a Datastore API resource. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 16 | type Datastore struct { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame^] | 17 | DefaultVersion string `json:"default_version"` |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 18 | ID string |
| 19 | Links []gophercloud.Link |
| 20 | Name string |
| 21 | Versions []Version |
| 22 | } |
| 23 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 24 | // 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 Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 27 | type DatastorePartial struct { |
| 28 | Version string |
| 29 | Type string |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame^] | 30 | VersionID string `json:"version_id"` |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 31 | } |
| 32 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 33 | // GetResult represents the result of a Get operation. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 34 | type GetResult struct { |
| 35 | gophercloud.Result |
| 36 | } |
| 37 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 38 | // GetVersionResult represents the result of getting a version. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 39 | type GetVersionResult struct { |
| 40 | gophercloud.Result |
| 41 | } |
| 42 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 43 | // DatastorePage represents a page of datastore resources. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 44 | type DatastorePage struct { |
| 45 | pagination.SinglePageBase |
| 46 | } |
| 47 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 48 | // IsEmpty indicates whether a Datastore collection is empty. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 49 | func (r DatastorePage) IsEmpty() (bool, error) { |
| 50 | is, err := ExtractDatastores(r) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame^] | 51 | return len(is) == 0, err |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 54 | // ExtractDatastores retrieves a slice of datastore structs from a paginated |
| 55 | // collection. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 56 | func ExtractDatastores(page pagination.Page) ([]Datastore, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame^] | 57 | r := page.(DatastorePage) |
| 58 | var s struct { |
| 59 | Datastores []Datastore `json:"datastores"` |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 60 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame^] | 61 | err := r.ExtractInto(&s) |
| 62 | return s.Datastores, err |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 65 | // Extract retrieves a single Datastore struct from an operation result. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 66 | func (r GetResult) Extract() (*Datastore, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame^] | 67 | var s struct { |
| 68 | Datastore *Datastore `json:"datastore"` |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 69 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame^] | 70 | err := r.ExtractInto(&s) |
| 71 | return s.Datastore, err |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame^] | 74 | // VersionPage represents a page of version resources. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 75 | type VersionPage struct { |
| 76 | pagination.SinglePageBase |
| 77 | } |
| 78 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 79 | // IsEmpty indicates whether a collection of version resources is empty. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 80 | func (r VersionPage) IsEmpty() (bool, error) { |
| 81 | is, err := ExtractVersions(r) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame^] | 82 | return len(is) == 0, err |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 85 | // ExtractVersions retrieves a slice of versions from a paginated collection. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 86 | func ExtractVersions(page pagination.Page) ([]Version, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame^] | 87 | r := page.(VersionPage) |
| 88 | var s struct { |
| 89 | Versions []Version `json:"versions"` |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 90 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame^] | 91 | err := r.ExtractInto(&s) |
| 92 | return s.Versions, err |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 95 | // Extract retrieves a single Version struct from an operation result. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 96 | func (r GetVersionResult) Extract() (*Version, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame^] | 97 | var s struct { |
| 98 | Version *Version `json:"version"` |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 99 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame^] | 100 | err := r.ExtractInto(&s) |
| 101 | return s.Version, err |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 102 | } |