Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 1 | package configurations |
| 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/openstack/db/v1/instances" |
| 6 | "github.com/gophercloud/gophercloud/pagination" |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 7 | ) |
| 8 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 9 | // List will list all of the available configurations. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 10 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 11 | pageFn := func(r pagination.PageResult) pagination.Page { |
| 12 | return ConfigPage{pagination.SinglePageBase(r)} |
| 13 | } |
| 14 | |
| 15 | return pagination.NewPager(client, baseURL(client), pageFn) |
| 16 | } |
| 17 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 18 | // CreateOptsBuilder is a top-level interface which renders a JSON map. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 19 | type CreateOptsBuilder interface { |
| 20 | ToConfigCreateMap() (map[string]interface{}, error) |
| 21 | } |
| 22 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 23 | // DatastoreOpts is the primary options struct for creating and modifying |
| 24 | // how configuration resources are associated with datastores. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 25 | type DatastoreOpts struct { |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 26 | // [OPTIONAL] The type of datastore. Defaults to "MySQL". |
| 27 | Type string |
| 28 | |
| 29 | // [OPTIONAL] The specific version of a datastore. Defaults to "5.6". |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 30 | Version string |
| 31 | } |
| 32 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 33 | // ToMap renders a JSON map for a datastore setting. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 34 | func (opts DatastoreOpts) ToMap() (map[string]string, error) { |
| 35 | datastore := map[string]string{} |
| 36 | |
| 37 | if opts.Type != "" { |
| 38 | datastore["type"] = opts.Type |
| 39 | } |
| 40 | |
| 41 | if opts.Version != "" { |
| 42 | datastore["version"] = opts.Version |
| 43 | } |
| 44 | |
| 45 | return datastore, nil |
| 46 | } |
| 47 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 48 | // CreateOpts is the struct responsible for configuring new configurations. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 49 | type CreateOpts struct { |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 50 | // [REQUIRED] The configuration group name |
| 51 | Name string |
| 52 | |
| 53 | // [REQUIRED] A map of user-defined configuration settings that will define |
| 54 | // how each associated datastore works. Each key/value pair is specific to a |
| 55 | // datastore type. |
| 56 | Values map[string]interface{} |
| 57 | |
| 58 | // [OPTIONAL] Associates the configuration group with a particular datastore. |
| 59 | Datastore *DatastoreOpts |
| 60 | |
| 61 | // [OPTIONAL] A human-readable explanation for the group. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 62 | Description string |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 65 | // ToConfigCreateMap casts a CreateOpts struct into a JSON map. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 66 | func (opts CreateOpts) ToConfigCreateMap() (map[string]interface{}, error) { |
| 67 | if opts.Name == "" { |
Jon Perritt | 763e592 | 2016-03-07 03:21:18 -0600 | [diff] [blame] | 68 | err := gophercloud.ErrMissingInput{} |
| 69 | err.Function = "configurations.ToConfigCreateMap" |
| 70 | err.Argument = "configurations.CreateOpts.Name" |
| 71 | return nil, err |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 72 | } |
| 73 | if len(opts.Values) == 0 { |
Jon Perritt | 763e592 | 2016-03-07 03:21:18 -0600 | [diff] [blame] | 74 | err := gophercloud.ErrMissingInput{} |
| 75 | err.Function = "configurations.ToConfigCreateMap" |
| 76 | err.Argument = "configurations.CreateOpts.Values" |
| 77 | return nil, err |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | config := map[string]interface{}{ |
| 81 | "name": opts.Name, |
| 82 | "values": opts.Values, |
| 83 | } |
| 84 | |
| 85 | if opts.Datastore != nil { |
| 86 | ds, err := opts.Datastore.ToMap() |
| 87 | if err != nil { |
| 88 | return config, err |
| 89 | } |
| 90 | config["datastore"] = ds |
| 91 | } |
| 92 | |
| 93 | if opts.Description != "" { |
| 94 | config["description"] = opts.Description |
| 95 | } |
| 96 | |
| 97 | return map[string]interface{}{"configuration": config}, nil |
| 98 | } |
| 99 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 100 | // Create will create a new configuration group. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 101 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
| 102 | var res CreateResult |
| 103 | |
| 104 | reqBody, err := opts.ToConfigCreateMap() |
| 105 | if err != nil { |
| 106 | res.Err = err |
| 107 | return res |
| 108 | } |
| 109 | |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 110 | _, res.Err = client.Request("POST", baseURL(client), &gophercloud.RequestOpts{ |
Jamie Hannaford | 99eced5 | 2015-03-02 15:24:22 +0100 | [diff] [blame] | 111 | OkCodes: []int{200}, |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 112 | JSONBody: &reqBody, |
| 113 | JSONResponse: &res.Body, |
| 114 | }) |
| 115 | |
| 116 | return res |
| 117 | } |
| 118 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 119 | // Get will retrieve the details for a specified configuration group. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 120 | func Get(client *gophercloud.ServiceClient, configID string) GetResult { |
| 121 | var res GetResult |
| 122 | |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 123 | _, res.Err = client.Request("GET", resourceURL(client, configID), &gophercloud.RequestOpts{ |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 124 | OkCodes: []int{200}, |
| 125 | JSONResponse: &res.Body, |
| 126 | }) |
| 127 | |
| 128 | return res |
| 129 | } |
| 130 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 131 | // UpdateOptsBuilder is the top-level interface for casting update options into |
| 132 | // JSON maps. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 133 | type UpdateOptsBuilder interface { |
| 134 | ToConfigUpdateMap() (map[string]interface{}, error) |
| 135 | } |
| 136 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 137 | // UpdateOpts is the struct responsible for modifying existing configurations. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 138 | type UpdateOpts struct { |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 139 | // [OPTIONAL] The configuration group name |
| 140 | Name string |
| 141 | |
| 142 | // [OPTIONAL] A map of user-defined configuration settings that will define |
| 143 | // how each associated datastore works. Each key/value pair is specific to a |
| 144 | // datastore type. |
| 145 | Values map[string]interface{} |
| 146 | |
| 147 | // [OPTIONAL] Associates the configuration group with a particular datastore. |
| 148 | Datastore *DatastoreOpts |
| 149 | |
| 150 | // [OPTIONAL] A human-readable explanation for the group. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 151 | Description string |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 154 | // ToConfigUpdateMap will cast an UpdateOpts struct into a JSON map. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 155 | func (opts UpdateOpts) ToConfigUpdateMap() (map[string]interface{}, error) { |
| 156 | config := map[string]interface{}{} |
| 157 | |
| 158 | if opts.Name != "" { |
| 159 | config["name"] = opts.Name |
| 160 | } |
| 161 | |
| 162 | if opts.Description != "" { |
| 163 | config["description"] = opts.Description |
| 164 | } |
| 165 | |
| 166 | if opts.Datastore != nil { |
| 167 | ds, err := opts.Datastore.ToMap() |
| 168 | if err != nil { |
| 169 | return config, err |
| 170 | } |
| 171 | config["datastore"] = ds |
| 172 | } |
| 173 | |
| 174 | if len(opts.Values) > 0 { |
| 175 | config["values"] = opts.Values |
| 176 | } |
| 177 | |
| 178 | return map[string]interface{}{"configuration": config}, nil |
| 179 | } |
| 180 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 181 | // Update will modify an existing configuration group by performing a merge |
| 182 | // between new and existing values. If the key already exists, the new value |
| 183 | // will overwrite. All other keys will remain unaffected. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 184 | func Update(client *gophercloud.ServiceClient, configID string, opts UpdateOptsBuilder) UpdateResult { |
| 185 | var res UpdateResult |
| 186 | |
| 187 | reqBody, err := opts.ToConfigUpdateMap() |
| 188 | if err != nil { |
| 189 | res.Err = err |
| 190 | return res |
| 191 | } |
| 192 | |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 193 | _, res.Err = client.Request("PATCH", resourceURL(client, configID), &gophercloud.RequestOpts{ |
Jamie Hannaford | 52dbcee | 2015-10-06 16:09:56 +0200 | [diff] [blame] | 194 | OkCodes: []int{200}, |
| 195 | JSONBody: &reqBody, |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 196 | }) |
| 197 | |
| 198 | return res |
| 199 | } |
| 200 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 201 | // Replace will modify an existing configuration group by overwriting the |
| 202 | // entire parameter group with the new values provided. Any existing keys not |
| 203 | // included in UpdateOptsBuilder will be deleted. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 204 | func Replace(client *gophercloud.ServiceClient, configID string, opts UpdateOptsBuilder) ReplaceResult { |
| 205 | var res ReplaceResult |
| 206 | |
| 207 | reqBody, err := opts.ToConfigUpdateMap() |
| 208 | if err != nil { |
| 209 | res.Err = err |
| 210 | return res |
| 211 | } |
| 212 | |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 213 | _, res.Err = client.Request("PUT", resourceURL(client, configID), &gophercloud.RequestOpts{ |
Jamie Hannaford | 52dbcee | 2015-10-06 16:09:56 +0200 | [diff] [blame] | 214 | OkCodes: []int{202}, |
| 215 | JSONBody: &reqBody, |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 216 | }) |
| 217 | |
| 218 | return res |
| 219 | } |
| 220 | |
Jamie Hannaford | 99eced5 | 2015-03-02 15:24:22 +0100 | [diff] [blame] | 221 | // Delete will permanently delete a configuration group. Please note that |
| 222 | // config groups cannot be deleted whilst still attached to running instances - |
| 223 | // you must detach and then delete them. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 224 | func Delete(client *gophercloud.ServiceClient, configID string) DeleteResult { |
| 225 | var res DeleteResult |
| 226 | |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 227 | _, res.Err = client.Request("DELETE", resourceURL(client, configID), &gophercloud.RequestOpts{ |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 228 | OkCodes: []int{202}, |
| 229 | }) |
| 230 | |
| 231 | return res |
| 232 | } |
| 233 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 234 | // ListInstances will list all the instances associated with a particular |
| 235 | // configuration group. |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 236 | func ListInstances(client *gophercloud.ServiceClient, configID string) pagination.Pager { |
| 237 | pageFn := func(r pagination.PageResult) pagination.Page { |
| 238 | return instances.InstancePage{pagination.LinkedPageBase{PageResult: r}} |
| 239 | } |
Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 240 | return pagination.NewPager(client, instancesURL(client, configID), pageFn) |
| 241 | } |
Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 242 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 243 | // ListDatastoreParams will list all the available and supported parameters |
| 244 | // that can be used for a particular datastore ID and a particular version. |
| 245 | // For example, if you are wondering how you can configure a MySQL 5.6 instance, |
| 246 | // you can use this operation (you will need to retrieve the MySQL datastore ID |
| 247 | // by using the datastores API). |
Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 248 | func ListDatastoreParams(client *gophercloud.ServiceClient, datastoreID, versionID string) pagination.Pager { |
| 249 | pageFn := func(r pagination.PageResult) pagination.Page { |
| 250 | return ParamPage{pagination.SinglePageBase(r)} |
| 251 | } |
| 252 | return pagination.NewPager(client, listDSParamsURL(client, datastoreID, versionID), pageFn) |
| 253 | } |
| 254 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 255 | // GetDatastoreParam will retrieve information about a specific configuration |
| 256 | // parameter. For example, you can use this operation to understand more about |
| 257 | // "innodb_file_per_table" configuration param for MySQL datastores. You will |
| 258 | // need the param's ID first, which can be attained by using the ListDatastoreParams |
| 259 | // operation. |
Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 260 | func GetDatastoreParam(client *gophercloud.ServiceClient, datastoreID, versionID, paramID string) ParamResult { |
| 261 | var res ParamResult |
| 262 | |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 263 | _, res.Err = client.Request("GET", getDSParamURL(client, datastoreID, versionID, paramID), &gophercloud.RequestOpts{ |
Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 264 | OkCodes: []int{200}, |
| 265 | JSONResponse: &res.Body, |
| 266 | }) |
| 267 | |
| 268 | return res |
| 269 | } |
| 270 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 271 | // ListGlobalParams is similar to ListDatastoreParams but does not require a |
| 272 | // DatastoreID. |
Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 273 | func ListGlobalParams(client *gophercloud.ServiceClient, versionID string) pagination.Pager { |
| 274 | pageFn := func(r pagination.PageResult) pagination.Page { |
| 275 | return ParamPage{pagination.SinglePageBase(r)} |
| 276 | } |
| 277 | return pagination.NewPager(client, listGlobalParamsURL(client, versionID), pageFn) |
| 278 | } |
| 279 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 280 | // GetGlobalParam is similar to GetDatastoreParam but does not require a |
| 281 | // DatastoreID. |
Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 282 | func GetGlobalParam(client *gophercloud.ServiceClient, versionID, paramID string) ParamResult { |
| 283 | var res ParamResult |
| 284 | |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 285 | _, res.Err = client.Request("GET", getGlobalParamURL(client, versionID, paramID), &gophercloud.RequestOpts{ |
Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 286 | OkCodes: []int{200}, |
| 287 | JSONResponse: &res.Body, |
| 288 | }) |
| 289 | |
| 290 | return res |
| 291 | } |