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