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