Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 1 | package datastores |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud" |
| 6 | "github.com/rackspace/gophercloud/pagination" |
| 7 | ) |
| 8 | |
| 9 | type Version struct { |
| 10 | ID string |
| 11 | Links []gophercloud.Link |
| 12 | Name string |
| 13 | } |
| 14 | |
| 15 | type Datastore struct { |
| 16 | DefaultVersion string `json:"default_version" mapstructure:"default_version"` |
| 17 | ID string |
| 18 | Links []gophercloud.Link |
| 19 | Name string |
| 20 | Versions []Version |
| 21 | } |
| 22 | |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 23 | type DatastorePartial struct { |
| 24 | Version string |
| 25 | Type string |
| 26 | VersionID string `json:"version_id" mapstructure:"version_id"` |
| 27 | } |
| 28 | |
Jamie Hannaford | 8252352 | 2015-02-17 16:53:29 +0100 | [diff] [blame] | 29 | type GetResult struct { |
| 30 | gophercloud.Result |
| 31 | } |
| 32 | |
| 33 | type GetVersionResult struct { |
| 34 | gophercloud.Result |
| 35 | } |
| 36 | |
| 37 | type DatastorePage struct { |
| 38 | pagination.SinglePageBase |
| 39 | } |
| 40 | |
| 41 | func (r DatastorePage) IsEmpty() (bool, error) { |
| 42 | is, err := ExtractDatastores(r) |
| 43 | if err != nil { |
| 44 | return true, err |
| 45 | } |
| 46 | return len(is) == 0, nil |
| 47 | } |
| 48 | |
| 49 | func ExtractDatastores(page pagination.Page) ([]Datastore, error) { |
| 50 | casted := page.(DatastorePage).Body |
| 51 | |
| 52 | var resp struct { |
| 53 | Datastores []Datastore `mapstructure:"datastores" json:"datastores"` |
| 54 | } |
| 55 | |
| 56 | err := mapstructure.Decode(casted, &resp) |
| 57 | return resp.Datastores, err |
| 58 | } |
| 59 | |
| 60 | func (r GetResult) Extract() (*Datastore, error) { |
| 61 | if r.Err != nil { |
| 62 | return nil, r.Err |
| 63 | } |
| 64 | |
| 65 | var response struct { |
| 66 | Datastore Datastore `mapstructure:"datastore"` |
| 67 | } |
| 68 | |
| 69 | err := mapstructure.Decode(r.Body, &response) |
| 70 | return &response.Datastore, err |
| 71 | } |
| 72 | |
| 73 | type VersionPage struct { |
| 74 | pagination.SinglePageBase |
| 75 | } |
| 76 | |
| 77 | func (r VersionPage) IsEmpty() (bool, error) { |
| 78 | is, err := ExtractVersions(r) |
| 79 | if err != nil { |
| 80 | return true, err |
| 81 | } |
| 82 | return len(is) == 0, nil |
| 83 | } |
| 84 | |
| 85 | func ExtractVersions(page pagination.Page) ([]Version, error) { |
| 86 | casted := page.(VersionPage).Body |
| 87 | |
| 88 | var resp struct { |
| 89 | Versions []Version `mapstructure:"versions" json:"versions"` |
| 90 | } |
| 91 | |
| 92 | err := mapstructure.Decode(casted, &resp) |
| 93 | return resp.Versions, err |
| 94 | } |
| 95 | |
| 96 | func (r GetVersionResult) Extract() (*Version, error) { |
| 97 | if r.Err != nil { |
| 98 | return nil, r.Err |
| 99 | } |
| 100 | |
| 101 | var response struct { |
| 102 | Version Version `mapstructure:"version"` |
| 103 | } |
| 104 | |
| 105 | err := mapstructure.Decode(r.Body, &response) |
| 106 | return &response.Version, err |
| 107 | } |