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 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 9 | // Config represents a configuration group API resource. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 10 | type Config struct { |
| 11 | Created string |
| 12 | Updated string |
| 13 | DatastoreName string `mapstructure:"datastore_name"` |
| 14 | DatastoreVersionID string `mapstructure:"datastore_version_id"` |
| 15 | DatastoreVersionName string `mapstructure:"datastore_version_name"` |
| 16 | Description string |
| 17 | ID string |
| 18 | Name string |
| 19 | Values map[string]interface{} |
| 20 | } |
| 21 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 22 | // ConfigPage contains a page of Config resources in a paginated collection. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 23 | type ConfigPage struct { |
| 24 | pagination.SinglePageBase |
| 25 | } |
| 26 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 27 | // IsEmpty indicates whether a ConfigPage is empty. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 28 | func (r ConfigPage) IsEmpty() (bool, error) { |
| 29 | is, err := ExtractConfigs(r) |
| 30 | if err != nil { |
| 31 | return true, err |
| 32 | } |
| 33 | return len(is) == 0, nil |
| 34 | } |
| 35 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 36 | // ExtractConfigs will retrieve a slice of Config structs from a page. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 37 | func ExtractConfigs(page pagination.Page) ([]Config, error) { |
| 38 | casted := page.(ConfigPage).Body |
| 39 | |
| 40 | var resp struct { |
| 41 | Configs []Config `mapstructure:"configurations" json:"configurations"` |
| 42 | } |
| 43 | |
| 44 | err := mapstructure.Decode(casted, &resp) |
| 45 | return resp.Configs, err |
| 46 | } |
| 47 | |
| 48 | type commonResult struct { |
| 49 | gophercloud.Result |
| 50 | } |
| 51 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 52 | // Extract will retrieve a Config resource from an operation result. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 53 | func (r commonResult) Extract() (*Config, error) { |
| 54 | if r.Err != nil { |
| 55 | return nil, r.Err |
| 56 | } |
| 57 | |
| 58 | var response struct { |
| 59 | Config Config `mapstructure:"configuration"` |
| 60 | } |
| 61 | |
| 62 | err := mapstructure.Decode(r.Body, &response) |
| 63 | return &response.Config, err |
| 64 | } |
| 65 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 66 | // GetResult represents the result of a Get operation. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 67 | type GetResult struct { |
| 68 | commonResult |
| 69 | } |
| 70 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 71 | // CreateResult represents the result of a Create operation. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 72 | type CreateResult struct { |
| 73 | commonResult |
| 74 | } |
| 75 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 76 | // UpdateResult represents the result of an Update operation. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 77 | type UpdateResult struct { |
| 78 | gophercloud.ErrResult |
| 79 | } |
| 80 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 81 | // ReplaceResult represents the result of a Replace operation. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 82 | type ReplaceResult struct { |
| 83 | gophercloud.ErrResult |
| 84 | } |
| 85 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 86 | // DeleteResult represents the result of a Delete operation. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 87 | type DeleteResult struct { |
| 88 | gophercloud.ErrResult |
| 89 | } |
Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 90 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 91 | // Param represents a configuration parameter API resource. |
Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 92 | type Param struct { |
| 93 | Max int |
| 94 | Min int |
| 95 | Name string |
| 96 | RestartRequired bool `mapstructure:"restart_required" json:"restart_required"` |
| 97 | Type string |
| 98 | } |
| 99 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 100 | // ParamPage contains a page of Param resources in a paginated collection. |
Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 101 | type ParamPage struct { |
| 102 | pagination.SinglePageBase |
| 103 | } |
| 104 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 105 | // IsEmpty indicates whether a ParamPage is empty. |
Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 106 | func (r ParamPage) IsEmpty() (bool, error) { |
| 107 | is, err := ExtractParams(r) |
| 108 | if err != nil { |
| 109 | return true, err |
| 110 | } |
| 111 | return len(is) == 0, nil |
| 112 | } |
| 113 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 114 | // ExtractParams will retrieve a slice of Param structs from a page. |
Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 115 | func ExtractParams(page pagination.Page) ([]Param, error) { |
| 116 | casted := page.(ParamPage).Body |
| 117 | |
| 118 | var resp struct { |
| 119 | Params []Param `mapstructure:"configuration-parameters" json:"configuration-parameters"` |
| 120 | } |
| 121 | |
| 122 | err := mapstructure.Decode(casted, &resp) |
| 123 | return resp.Params, err |
| 124 | } |
| 125 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 126 | // ParamResult represents the result of an operation which retrieves details |
| 127 | // about a particular configuration param. |
Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 128 | type ParamResult struct { |
| 129 | gophercloud.Result |
| 130 | } |
| 131 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 132 | // Extract will retrieve a param from an operation result. |
Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 133 | func (r ParamResult) Extract() (*Param, error) { |
| 134 | if r.Err != nil { |
| 135 | return nil, r.Err |
| 136 | } |
| 137 | |
| 138 | var param Param |
| 139 | |
| 140 | err := mapstructure.Decode(r.Body, ¶m) |
| 141 | return ¶m, err |
| 142 | } |