Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 1 | package configurations |
| 2 | |
| 3 | import ( |
Jamie Hannaford | 87704ba | 2016-01-14 11:49:56 +0100 | [diff] [blame] | 4 | "fmt" |
| 5 | "reflect" |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 6 | "time" |
| 7 | |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 8 | "github.com/mitchellh/mapstructure" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame^] | 9 | "github.com/gophercloud/gophercloud" |
| 10 | "github.com/gophercloud/gophercloud/pagination" |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 11 | ) |
| 12 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 13 | // Config represents a configuration group API resource. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 14 | type Config struct { |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 15 | Created time.Time `mapstructure:"-"` |
| 16 | Updated time.Time `mapstructure:"-"` |
| 17 | DatastoreName string `mapstructure:"datastore_name"` |
| 18 | DatastoreVersionID string `mapstructure:"datastore_version_id"` |
| 19 | DatastoreVersionName string `mapstructure:"datastore_version_name"` |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 20 | Description string |
| 21 | ID string |
| 22 | Name string |
| 23 | Values map[string]interface{} |
| 24 | } |
| 25 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 26 | // ConfigPage contains a page of Config resources in a paginated collection. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 27 | type ConfigPage struct { |
| 28 | pagination.SinglePageBase |
| 29 | } |
| 30 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 31 | // IsEmpty indicates whether a ConfigPage is empty. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 32 | func (r ConfigPage) IsEmpty() (bool, error) { |
| 33 | is, err := ExtractConfigs(r) |
| 34 | if err != nil { |
| 35 | return true, err |
| 36 | } |
| 37 | return len(is) == 0, nil |
| 38 | } |
| 39 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 40 | // ExtractConfigs will retrieve a slice of Config structs from a page. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 41 | func ExtractConfigs(page pagination.Page) ([]Config, error) { |
| 42 | casted := page.(ConfigPage).Body |
| 43 | |
| 44 | var resp struct { |
| 45 | Configs []Config `mapstructure:"configurations" json:"configurations"` |
| 46 | } |
| 47 | |
Jamie Hannaford | 87704ba | 2016-01-14 11:49:56 +0100 | [diff] [blame] | 48 | if err := mapstructure.Decode(casted, &resp); err != nil { |
| 49 | return nil, err |
| 50 | } |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 51 | |
| 52 | var vals []interface{} |
Jamie Hannaford | 87704ba | 2016-01-14 11:49:56 +0100 | [diff] [blame] | 53 | switch casted.(type) { |
| 54 | case map[string]interface{}: |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 55 | vals = casted.(map[string]interface{})["configurations"].([]interface{}) |
Jamie Hannaford | 87704ba | 2016-01-14 11:49:56 +0100 | [diff] [blame] | 56 | case map[string][]interface{}: |
| 57 | vals = casted.(map[string][]interface{})["configurations"] |
| 58 | default: |
| 59 | return resp.Configs, fmt.Errorf("Unknown type: %v", reflect.TypeOf(casted)) |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | for i, v := range vals { |
| 63 | val := v.(map[string]interface{}) |
| 64 | |
| 65 | if t, ok := val["created"].(string); ok && t != "" { |
| 66 | creationTime, err := time.Parse(time.RFC3339, t) |
| 67 | if err != nil { |
| 68 | return resp.Configs, err |
| 69 | } |
| 70 | resp.Configs[i].Created = creationTime |
| 71 | } |
| 72 | |
| 73 | if t, ok := val["updated"].(string); ok && t != "" { |
| 74 | updatedTime, err := time.Parse(time.RFC3339, t) |
| 75 | if err != nil { |
| 76 | return resp.Configs, err |
| 77 | } |
| 78 | resp.Configs[i].Updated = updatedTime |
| 79 | } |
| 80 | } |
| 81 | |
Jamie Hannaford | 87704ba | 2016-01-14 11:49:56 +0100 | [diff] [blame] | 82 | return resp.Configs, nil |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | type commonResult struct { |
| 86 | gophercloud.Result |
| 87 | } |
| 88 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 89 | // Extract will retrieve a Config resource from an operation result. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 90 | func (r commonResult) Extract() (*Config, error) { |
| 91 | if r.Err != nil { |
| 92 | return nil, r.Err |
| 93 | } |
| 94 | |
| 95 | var response struct { |
| 96 | Config Config `mapstructure:"configuration"` |
| 97 | } |
| 98 | |
| 99 | err := mapstructure.Decode(r.Body, &response) |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 100 | val := r.Body.(map[string]interface{})["configuration"].(map[string]interface{}) |
| 101 | |
| 102 | if t, ok := val["created"].(string); ok && t != "" { |
| 103 | creationTime, err := time.Parse(time.RFC3339, t) |
| 104 | if err != nil { |
| 105 | return &response.Config, err |
| 106 | } |
| 107 | response.Config.Created = creationTime |
| 108 | } |
| 109 | |
| 110 | if t, ok := val["updated"].(string); ok && t != "" { |
| 111 | updatedTime, err := time.Parse(time.RFC3339, t) |
| 112 | if err != nil { |
| 113 | return &response.Config, err |
| 114 | } |
| 115 | response.Config.Updated = updatedTime |
| 116 | } |
| 117 | |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 118 | return &response.Config, err |
| 119 | } |
| 120 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 121 | // GetResult represents the result of a Get operation. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 122 | type GetResult struct { |
| 123 | commonResult |
| 124 | } |
| 125 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 126 | // CreateResult represents the result of a Create operation. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 127 | type CreateResult struct { |
| 128 | commonResult |
| 129 | } |
| 130 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 131 | // UpdateResult represents the result of an Update operation. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 132 | type UpdateResult struct { |
| 133 | gophercloud.ErrResult |
| 134 | } |
| 135 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 136 | // ReplaceResult represents the result of a Replace operation. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 137 | type ReplaceResult struct { |
| 138 | gophercloud.ErrResult |
| 139 | } |
| 140 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 141 | // DeleteResult represents the result of a Delete operation. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 142 | type DeleteResult struct { |
| 143 | gophercloud.ErrResult |
| 144 | } |
Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 145 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 146 | // Param represents a configuration parameter API resource. |
Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 147 | type Param struct { |
| 148 | Max int |
| 149 | Min int |
| 150 | Name string |
| 151 | RestartRequired bool `mapstructure:"restart_required" json:"restart_required"` |
| 152 | Type string |
| 153 | } |
| 154 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 155 | // ParamPage contains a page of Param resources in a paginated collection. |
Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 156 | type ParamPage struct { |
| 157 | pagination.SinglePageBase |
| 158 | } |
| 159 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 160 | // IsEmpty indicates whether a ParamPage is empty. |
Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 161 | func (r ParamPage) IsEmpty() (bool, error) { |
| 162 | is, err := ExtractParams(r) |
| 163 | if err != nil { |
| 164 | return true, err |
| 165 | } |
| 166 | return len(is) == 0, nil |
| 167 | } |
| 168 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 169 | // ExtractParams will retrieve a slice of Param structs from a page. |
Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 170 | func ExtractParams(page pagination.Page) ([]Param, error) { |
| 171 | casted := page.(ParamPage).Body |
| 172 | |
| 173 | var resp struct { |
| 174 | Params []Param `mapstructure:"configuration-parameters" json:"configuration-parameters"` |
| 175 | } |
| 176 | |
| 177 | err := mapstructure.Decode(casted, &resp) |
| 178 | return resp.Params, err |
| 179 | } |
| 180 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 181 | // ParamResult represents the result of an operation which retrieves details |
| 182 | // about a particular configuration param. |
Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 183 | type ParamResult struct { |
| 184 | gophercloud.Result |
| 185 | } |
| 186 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 187 | // Extract will retrieve a param from an operation result. |
Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 188 | func (r ParamResult) Extract() (*Param, error) { |
| 189 | if r.Err != nil { |
| 190 | return nil, r.Err |
| 191 | } |
| 192 | |
| 193 | var param Param |
| 194 | |
| 195 | err := mapstructure.Decode(r.Body, ¶m) |
| 196 | return ¶m, err |
| 197 | } |