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