Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 1 | package datastores |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 5 | "github.com/gophercloud/gophercloud" |
| 6 | "github.com/gophercloud/gophercloud/pagination" |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 7 | ) |
| 8 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 9 | // Version represents a version API resource. Multiple versions belong to a Datastore. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 10 | type Version struct { |
| 11 | ID string |
| 12 | Links []gophercloud.Link |
| 13 | Name string |
| 14 | } |
| 15 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 16 | // Datastore represents a Datastore API resource. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 17 | type 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 Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 25 | // 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 Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 28 | type DatastorePartial struct { |
| 29 | Version string |
| 30 | Type string |
| 31 | VersionID string `json:"version_id" mapstructure:"version_id"` |
| 32 | } |
| 33 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 34 | // GetResult represents the result of a Get operation. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 35 | type GetResult struct { |
| 36 | gophercloud.Result |
| 37 | } |
| 38 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 39 | // GetVersionResult represents the result of getting a version. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 40 | type GetVersionResult struct { |
| 41 | gophercloud.Result |
| 42 | } |
| 43 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 44 | // DatastorePage represents a page of datastore resources. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 45 | type DatastorePage struct { |
| 46 | pagination.SinglePageBase |
| 47 | } |
| 48 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 49 | // IsEmpty indicates whether a Datastore collection is empty. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 50 | func (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 Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 58 | // ExtractDatastores retrieves a slice of datastore structs from a paginated |
| 59 | // collection. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 60 | func 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 Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 71 | // Extract retrieves a single Datastore struct from an operation result. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 72 | func (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 Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 85 | // DatastorePage represents a page of version resources. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 86 | type VersionPage struct { |
| 87 | pagination.SinglePageBase |
| 88 | } |
| 89 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 90 | // IsEmpty indicates whether a collection of version resources is empty. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 91 | func (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 Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 99 | // ExtractVersions retrieves a slice of versions from a paginated collection. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 100 | func 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 Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 111 | // Extract retrieves a single Version struct from an operation result. |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 112 | func (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 | } |