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