Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame^] | 1 | package configurations |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud" |
| 6 | "github.com/rackspace/gophercloud/pagination" |
| 7 | ) |
| 8 | |
| 9 | type Config struct { |
| 10 | Created string |
| 11 | Updated string |
| 12 | DatastoreName string `mapstructure:"datastore_name"` |
| 13 | DatastoreVersionID string `mapstructure:"datastore_version_id"` |
| 14 | DatastoreVersionName string `mapstructure:"datastore_version_name"` |
| 15 | Description string |
| 16 | ID string |
| 17 | Name string |
| 18 | Values map[string]interface{} |
| 19 | } |
| 20 | |
| 21 | type ConfigPage struct { |
| 22 | pagination.SinglePageBase |
| 23 | } |
| 24 | |
| 25 | func (r ConfigPage) IsEmpty() (bool, error) { |
| 26 | is, err := ExtractConfigs(r) |
| 27 | if err != nil { |
| 28 | return true, err |
| 29 | } |
| 30 | return len(is) == 0, nil |
| 31 | } |
| 32 | |
| 33 | func ExtractConfigs(page pagination.Page) ([]Config, error) { |
| 34 | casted := page.(ConfigPage).Body |
| 35 | |
| 36 | var resp struct { |
| 37 | Configs []Config `mapstructure:"configurations" json:"configurations"` |
| 38 | } |
| 39 | |
| 40 | err := mapstructure.Decode(casted, &resp) |
| 41 | return resp.Configs, err |
| 42 | } |
| 43 | |
| 44 | type commonResult struct { |
| 45 | gophercloud.Result |
| 46 | } |
| 47 | |
| 48 | func (r commonResult) Extract() (*Config, error) { |
| 49 | if r.Err != nil { |
| 50 | return nil, r.Err |
| 51 | } |
| 52 | |
| 53 | var response struct { |
| 54 | Config Config `mapstructure:"configuration"` |
| 55 | } |
| 56 | |
| 57 | err := mapstructure.Decode(r.Body, &response) |
| 58 | return &response.Config, err |
| 59 | } |
| 60 | |
| 61 | type GetResult struct { |
| 62 | commonResult |
| 63 | } |
| 64 | |
| 65 | type CreateResult struct { |
| 66 | commonResult |
| 67 | } |
| 68 | |
| 69 | type UpdateResult struct { |
| 70 | gophercloud.ErrResult |
| 71 | } |
| 72 | |
| 73 | type ReplaceResult struct { |
| 74 | gophercloud.ErrResult |
| 75 | } |
| 76 | |
| 77 | type DeleteResult struct { |
| 78 | gophercloud.ErrResult |
| 79 | } |