Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 1 | package instances |
| 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | db "github.com/gophercloud/gophercloud/openstack/db/v1/databases" |
| 6 | "github.com/gophercloud/gophercloud/openstack/db/v1/users" |
| 7 | "github.com/gophercloud/gophercloud/pagination" |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 8 | ) |
| 9 | |
| 10 | // CreateOptsBuilder is the top-level interface for create options. |
| 11 | type CreateOptsBuilder interface { |
| 12 | ToInstanceCreateMap() (map[string]interface{}, error) |
| 13 | } |
| 14 | |
Jamie Hannaford | 99eced5 | 2015-03-02 15:24:22 +0100 | [diff] [blame] | 15 | // DatastoreOpts represents the configuration for how an instance stores data. |
| 16 | type DatastoreOpts struct { |
| 17 | Version string |
| 18 | Type string |
| 19 | } |
| 20 | |
Jon Perritt | 763e592 | 2016-03-07 03:21:18 -0600 | [diff] [blame^] | 21 | // ToMap converts a DatastoreOpts to a map[string]string (for a request body) |
Jamie Hannaford | 99eced5 | 2015-03-02 15:24:22 +0100 | [diff] [blame] | 22 | func (opts DatastoreOpts) ToMap() (map[string]string, error) { |
| 23 | return map[string]string{ |
| 24 | "version": opts.Version, |
| 25 | "type": opts.Type, |
| 26 | }, nil |
| 27 | } |
| 28 | |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 29 | // CreateOpts is the struct responsible for configuring a new database instance. |
| 30 | type CreateOpts struct { |
| 31 | // Either the integer UUID (in string form) of the flavor, or its URI |
| 32 | // reference as specified in the response from the List() call. Required. |
| 33 | FlavorRef string |
| 34 | |
| 35 | // Specifies the volume size in gigabytes (GB). The value must be between 1 |
| 36 | // and 300. Required. |
| 37 | Size int |
| 38 | |
| 39 | // Name of the instance to create. The length of the name is limited to |
| 40 | // 255 characters and any characters are permitted. Optional. |
| 41 | Name string |
| 42 | |
| 43 | // A slice of database information options. |
Jamie Hannaford | 2e695a3 | 2015-11-16 16:36:10 +0100 | [diff] [blame] | 44 | Databases db.CreateOptsBuilder |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 45 | |
| 46 | // A slice of user information options. |
Jamie Hannaford | 2e695a3 | 2015-11-16 16:36:10 +0100 | [diff] [blame] | 47 | Users users.CreateOptsBuilder |
Jamie Hannaford | 99eced5 | 2015-03-02 15:24:22 +0100 | [diff] [blame] | 48 | |
| 49 | // Options to configure the type of datastore the instance will use. This is |
| 50 | // optional, and if excluded will default to MySQL. |
| 51 | Datastore *DatastoreOpts |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 54 | // ToInstanceCreateMap will render a JSON map. |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 55 | func (opts CreateOpts) ToInstanceCreateMap() (map[string]interface{}, error) { |
| 56 | if opts.Size > 300 || opts.Size < 1 { |
Jon Perritt | 763e592 | 2016-03-07 03:21:18 -0600 | [diff] [blame^] | 57 | err := gophercloud.ErrInvalidInput{} |
| 58 | err.Function = "instances.ToInstanceCreateMap" |
| 59 | err.Argument = "instances.CreateOpts.Size" |
| 60 | err.Value = opts.Size |
| 61 | err.Info = "Size (GB) must be between 1-300" |
| 62 | return nil, err |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 63 | } |
| 64 | if opts.FlavorRef == "" { |
Jon Perritt | 763e592 | 2016-03-07 03:21:18 -0600 | [diff] [blame^] | 65 | err := gophercloud.ErrMissingInput{} |
| 66 | err.Function = "instances.ToInstanceCreateMap" |
| 67 | err.Argument = "instances.CreateOpts.FlavorRef" |
| 68 | return nil, err |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | instance := map[string]interface{}{ |
| 72 | "volume": map[string]int{"size": opts.Size}, |
| 73 | "flavorRef": opts.FlavorRef, |
| 74 | } |
| 75 | |
| 76 | if opts.Name != "" { |
| 77 | instance["name"] = opts.Name |
| 78 | } |
Jamie Hannaford | 2e695a3 | 2015-11-16 16:36:10 +0100 | [diff] [blame] | 79 | if opts.Databases != nil { |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 80 | dbs, err := opts.Databases.ToDBCreateMap() |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 81 | if err != nil { |
| 82 | return nil, err |
| 83 | } |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 84 | instance["databases"] = dbs["databases"] |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 85 | } |
Jamie Hannaford | 2e695a3 | 2015-11-16 16:36:10 +0100 | [diff] [blame] | 86 | if opts.Users != nil { |
Jamie Hannaford | 2ca55d8 | 2015-02-12 14:21:55 +0100 | [diff] [blame] | 87 | users, err := opts.Users.ToUserCreateMap() |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 88 | if err != nil { |
| 89 | return nil, err |
| 90 | } |
Jamie Hannaford | 2ca55d8 | 2015-02-12 14:21:55 +0100 | [diff] [blame] | 91 | instance["users"] = users["users"] |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 92 | } |
Jon Perritt | 763e592 | 2016-03-07 03:21:18 -0600 | [diff] [blame^] | 93 | if opts.Datastore != nil { |
| 94 | datastore, err := opts.Datastore.ToMap() |
| 95 | if err != nil { |
| 96 | return nil, err |
| 97 | } |
| 98 | instance["datastore"] = datastore |
| 99 | } |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 100 | |
| 101 | return map[string]interface{}{"instance": instance}, nil |
| 102 | } |
| 103 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 104 | // Create asynchronously provisions a new database instance. It requires the |
| 105 | // user to specify a flavor and a volume size. The API service then provisions |
| 106 | // the instance with the requested flavor and sets up a volume of the specified |
| 107 | // size, which is the storage for the database instance. |
| 108 | // |
| 109 | // Although this call only allows the creation of 1 instance per request, you |
| 110 | // can create an instance with multiple databases and users. The default |
| 111 | // binding for a MySQL instance is port 3306. |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 112 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
| 113 | var res CreateResult |
| 114 | |
| 115 | reqBody, err := opts.ToInstanceCreateMap() |
| 116 | if err != nil { |
| 117 | res.Err = err |
| 118 | return res |
| 119 | } |
| 120 | |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 121 | _, res.Err = client.Request("POST", baseURL(client), &gophercloud.RequestOpts{ |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 122 | JSONBody: &reqBody, |
| 123 | JSONResponse: &res.Body, |
| 124 | OkCodes: []int{200}, |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 125 | }) |
| 126 | |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 127 | return res |
| 128 | } |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 129 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 130 | // List retrieves the status and information for all database instances. |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 131 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 132 | createPageFn := func(r pagination.PageResult) pagination.Page { |
| 133 | return InstancePage{pagination.LinkedPageBase{PageResult: r}} |
| 134 | } |
| 135 | |
| 136 | return pagination.NewPager(client, baseURL(client), createPageFn) |
| 137 | } |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 138 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 139 | // Get retrieves the status and information for a specified database instance. |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 140 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
| 141 | var res GetResult |
| 142 | |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 143 | _, res.Err = client.Request("GET", resourceURL(client, id), &gophercloud.RequestOpts{ |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 144 | JSONResponse: &res.Body, |
| 145 | OkCodes: []int{200}, |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 146 | }) |
| 147 | |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 148 | return res |
| 149 | } |
Jamie Hannaford | 5b16b63 | 2015-02-10 13:36:23 +0100 | [diff] [blame] | 150 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 151 | // Delete permanently destroys the database instance. |
Jamie Hannaford | 5b16b63 | 2015-02-10 13:36:23 +0100 | [diff] [blame] | 152 | func Delete(client *gophercloud.ServiceClient, id string) DeleteResult { |
| 153 | var res DeleteResult |
| 154 | |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 155 | _, res.Err = client.Request("DELETE", resourceURL(client, id), &gophercloud.RequestOpts{ |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 156 | OkCodes: []int{202}, |
Jamie Hannaford | 5b16b63 | 2015-02-10 13:36:23 +0100 | [diff] [blame] | 157 | }) |
| 158 | |
Jamie Hannaford | 5b16b63 | 2015-02-10 13:36:23 +0100 | [diff] [blame] | 159 | return res |
| 160 | } |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 161 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 162 | // EnableRootUser enables the login from any host for the root user and |
| 163 | // provides the user with a generated root password. |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 164 | func EnableRootUser(client *gophercloud.ServiceClient, id string) UserRootResult { |
| 165 | var res UserRootResult |
| 166 | |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 167 | _, res.Err = client.Request("POST", userRootURL(client, id), &gophercloud.RequestOpts{ |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 168 | JSONResponse: &res.Body, |
| 169 | OkCodes: []int{200}, |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 170 | }) |
| 171 | |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 172 | return res |
| 173 | } |
Jamie Hannaford | a74d425 | 2015-02-10 15:35:01 +0100 | [diff] [blame] | 174 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 175 | // IsRootEnabled checks an instance to see if root access is enabled. It returns |
| 176 | // True if root user is enabled for the specified database instance or False |
| 177 | // otherwise. |
Jamie Hannaford | a74d425 | 2015-02-10 15:35:01 +0100 | [diff] [blame] | 178 | func IsRootEnabled(client *gophercloud.ServiceClient, id string) (bool, error) { |
| 179 | var res gophercloud.Result |
| 180 | |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 181 | _, err := client.Request("GET", userRootURL(client, id), &gophercloud.RequestOpts{ |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 182 | JSONResponse: &res.Body, |
| 183 | OkCodes: []int{200}, |
Jamie Hannaford | a74d425 | 2015-02-10 15:35:01 +0100 | [diff] [blame] | 184 | }) |
| 185 | |
| 186 | return res.Body.(map[string]interface{})["rootEnabled"] == true, err |
| 187 | } |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 188 | |
Jamie Hannaford | 75e8cc4 | 2015-11-16 14:09:25 +0100 | [diff] [blame] | 189 | // Restart will restart only the MySQL Instance. Restarting MySQL will |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 190 | // erase any dynamic configuration settings that you have made within MySQL. |
| 191 | // The MySQL service will be unavailable until the instance restarts. |
Jamie Hannaford | 75e8cc4 | 2015-11-16 14:09:25 +0100 | [diff] [blame] | 192 | func Restart(client *gophercloud.ServiceClient, id string) ActionResult { |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 193 | var res ActionResult |
| 194 | |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 195 | _, res.Err = client.Request("POST", actionURL(client, id), &gophercloud.RequestOpts{ |
Jamie Hannaford | d2b755f | 2015-10-07 14:01:57 +0200 | [diff] [blame] | 196 | JSONBody: map[string]interface{}{"restart": struct{}{}}, |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 197 | OkCodes: []int{202}, |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 198 | }) |
| 199 | |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 200 | return res |
| 201 | } |
| 202 | |
Jamie Hannaford | 75e8cc4 | 2015-11-16 14:09:25 +0100 | [diff] [blame] | 203 | // Resize changes the memory size of the instance, assuming a valid |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 204 | // flavorRef is provided. It will also restart the MySQL service. |
Jamie Hannaford | 75e8cc4 | 2015-11-16 14:09:25 +0100 | [diff] [blame] | 205 | func Resize(client *gophercloud.ServiceClient, id, flavorRef string) ActionResult { |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 206 | var res ActionResult |
| 207 | |
Jamie Hannaford | 7d51cf1 | 2015-02-25 14:50:06 +0100 | [diff] [blame] | 208 | type resize struct { |
| 209 | FlavorRef string `json:"flavorRef"` |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 210 | } |
| 211 | |
Jamie Hannaford | 7d51cf1 | 2015-02-25 14:50:06 +0100 | [diff] [blame] | 212 | type req struct { |
| 213 | Resize resize `json:"resize"` |
| 214 | } |
| 215 | |
| 216 | reqBody := req{Resize: resize{FlavorRef: flavorRef}} |
| 217 | |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 218 | _, res.Err = client.Request("POST", actionURL(client, id), &gophercloud.RequestOpts{ |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 219 | JSONBody: reqBody, |
| 220 | OkCodes: []int{202}, |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 221 | }) |
| 222 | |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 223 | return res |
| 224 | } |
| 225 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 226 | // ResizeVolume will resize the attached volume for an instance. It supports |
| 227 | // only increasing the volume size and does not support decreasing the size. |
| 228 | // The volume size is in gigabytes (GB) and must be an integer. |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 229 | func ResizeVolume(client *gophercloud.ServiceClient, id string, size int) ActionResult { |
| 230 | var res ActionResult |
| 231 | |
Jamie Hannaford | 7d51cf1 | 2015-02-25 14:50:06 +0100 | [diff] [blame] | 232 | type volume struct { |
| 233 | Size int `json:"size"` |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Jamie Hannaford | 7d51cf1 | 2015-02-25 14:50:06 +0100 | [diff] [blame] | 236 | type resize struct { |
| 237 | Volume volume `json:"volume"` |
| 238 | } |
| 239 | |
| 240 | type req struct { |
| 241 | Resize resize `json:"resize"` |
| 242 | } |
| 243 | |
| 244 | reqBody := req{Resize: resize{Volume: volume{Size: size}}} |
| 245 | |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 246 | _, res.Err = client.Request("POST", actionURL(client, id), &gophercloud.RequestOpts{ |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 247 | JSONBody: reqBody, |
| 248 | OkCodes: []int{202}, |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 249 | }) |
| 250 | |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 251 | return res |
| 252 | } |