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 | } |
Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame^] | 80 | |
| 81 | type Param struct { |
| 82 | Max int |
| 83 | Min int |
| 84 | Name string |
| 85 | RestartRequired bool `mapstructure:"restart_required" json:"restart_required"` |
| 86 | Type string |
| 87 | } |
| 88 | |
| 89 | type ParamPage struct { |
| 90 | pagination.SinglePageBase |
| 91 | } |
| 92 | |
| 93 | func (r ParamPage) IsEmpty() (bool, error) { |
| 94 | is, err := ExtractParams(r) |
| 95 | if err != nil { |
| 96 | return true, err |
| 97 | } |
| 98 | return len(is) == 0, nil |
| 99 | } |
| 100 | |
| 101 | func ExtractParams(page pagination.Page) ([]Param, error) { |
| 102 | casted := page.(ParamPage).Body |
| 103 | |
| 104 | var resp struct { |
| 105 | Params []Param `mapstructure:"configuration-parameters" json:"configuration-parameters"` |
| 106 | } |
| 107 | |
| 108 | err := mapstructure.Decode(casted, &resp) |
| 109 | return resp.Params, err |
| 110 | } |
| 111 | |
| 112 | type ParamResult struct { |
| 113 | gophercloud.Result |
| 114 | } |
| 115 | |
| 116 | func (r ParamResult) Extract() (*Param, error) { |
| 117 | if r.Err != nil { |
| 118 | return nil, r.Err |
| 119 | } |
| 120 | |
| 121 | var param Param |
| 122 | |
| 123 | err := mapstructure.Decode(r.Body, ¶m) |
| 124 | return ¶m, err |
| 125 | } |