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 { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 17 | Version string `json:"version"` |
| 18 | Type string `json:"type"` |
Jamie Hannaford | 99eced5 | 2015-03-02 15:24:22 +0100 | [diff] [blame] | 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) |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 22 | func (opts DatastoreOpts) ToMap() (map[string]interface{}, error) { |
| 23 | return gophercloud.BuildRequestBody(opts, "") |
Jamie Hannaford | 99eced5 | 2015-03-02 15:24:22 +0100 | [diff] [blame] | 24 | } |
| 25 | |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 26 | // CreateOpts is the struct responsible for configuring a new database instance. |
| 27 | type CreateOpts struct { |
| 28 | // Either the integer UUID (in string form) of the flavor, or its URI |
| 29 | // reference as specified in the response from the List() call. Required. |
| 30 | FlavorRef string |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 31 | // Specifies the volume size in gigabytes (GB). The value must be between 1 |
| 32 | // and 300. Required. |
| 33 | Size int |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 34 | // Name of the instance to create. The length of the name is limited to |
| 35 | // 255 characters and any characters are permitted. Optional. |
| 36 | Name string |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 37 | // A slice of database information options. |
Jamie Hannaford | 2e695a3 | 2015-11-16 16:36:10 +0100 | [diff] [blame] | 38 | Databases db.CreateOptsBuilder |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 39 | // A slice of user information options. |
Jamie Hannaford | 2e695a3 | 2015-11-16 16:36:10 +0100 | [diff] [blame] | 40 | Users users.CreateOptsBuilder |
Jamie Hannaford | 99eced5 | 2015-03-02 15:24:22 +0100 | [diff] [blame] | 41 | // Options to configure the type of datastore the instance will use. This is |
| 42 | // optional, and if excluded will default to MySQL. |
| 43 | Datastore *DatastoreOpts |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 44 | } |
| 45 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 46 | // ToInstanceCreateMap will render a JSON map. |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 47 | func (opts CreateOpts) ToInstanceCreateMap() (map[string]interface{}, error) { |
| 48 | if opts.Size > 300 || opts.Size < 1 { |
Jon Perritt | 763e592 | 2016-03-07 03:21:18 -0600 | [diff] [blame] | 49 | err := gophercloud.ErrInvalidInput{} |
Jon Perritt | 763e592 | 2016-03-07 03:21:18 -0600 | [diff] [blame] | 50 | err.Argument = "instances.CreateOpts.Size" |
| 51 | err.Value = opts.Size |
| 52 | err.Info = "Size (GB) must be between 1-300" |
| 53 | return nil, err |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 54 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 55 | |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 56 | if opts.FlavorRef == "" { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 57 | return nil, gophercloud.ErrMissingInput{Argument: "instances.CreateOpts.FlavorRef"} |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | instance := map[string]interface{}{ |
| 61 | "volume": map[string]int{"size": opts.Size}, |
| 62 | "flavorRef": opts.FlavorRef, |
| 63 | } |
| 64 | |
| 65 | if opts.Name != "" { |
| 66 | instance["name"] = opts.Name |
| 67 | } |
Jamie Hannaford | 2e695a3 | 2015-11-16 16:36:10 +0100 | [diff] [blame] | 68 | if opts.Databases != nil { |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 69 | dbs, err := opts.Databases.ToDBCreateMap() |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 70 | if err != nil { |
| 71 | return nil, err |
| 72 | } |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 73 | instance["databases"] = dbs["databases"] |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 74 | } |
Jamie Hannaford | 2e695a3 | 2015-11-16 16:36:10 +0100 | [diff] [blame] | 75 | if opts.Users != nil { |
Jamie Hannaford | 2ca55d8 | 2015-02-12 14:21:55 +0100 | [diff] [blame] | 76 | users, err := opts.Users.ToUserCreateMap() |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 77 | if err != nil { |
| 78 | return nil, err |
| 79 | } |
Jamie Hannaford | 2ca55d8 | 2015-02-12 14:21:55 +0100 | [diff] [blame] | 80 | instance["users"] = users["users"] |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 81 | } |
Jon Perritt | 763e592 | 2016-03-07 03:21:18 -0600 | [diff] [blame] | 82 | if opts.Datastore != nil { |
| 83 | datastore, err := opts.Datastore.ToMap() |
| 84 | if err != nil { |
| 85 | return nil, err |
| 86 | } |
| 87 | instance["datastore"] = datastore |
| 88 | } |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 89 | |
| 90 | return map[string]interface{}{"instance": instance}, nil |
| 91 | } |
| 92 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 93 | // Create asynchronously provisions a new database instance. It requires the |
| 94 | // user to specify a flavor and a volume size. The API service then provisions |
| 95 | // the instance with the requested flavor and sets up a volume of the specified |
| 96 | // size, which is the storage for the database instance. |
| 97 | // |
| 98 | // Although this call only allows the creation of 1 instance per request, you |
| 99 | // can create an instance with multiple databases and users. The default |
| 100 | // binding for a MySQL instance is port 3306. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 101 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 102 | b, err := opts.ToInstanceCreateMap() |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 103 | if err != nil { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 104 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 105 | return |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 106 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 107 | _, r.Err = client.Post(baseURL(client), &b, &r.Body, &gophercloud.RequestOpts{OkCodes: []int{200}}) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 108 | return |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 109 | } |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 110 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 111 | // List retrieves the status and information for all database instances. |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 112 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 113 | return pagination.NewPager(client, baseURL(client), func(r pagination.PageResult) pagination.Page { |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 114 | return InstancePage{pagination.LinkedPageBase{PageResult: r}} |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 115 | }) |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 116 | } |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 117 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 118 | // Get retrieves the status and information for a specified database instance. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 119 | func Get(client *gophercloud.ServiceClient, id string) (r GetResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 120 | _, r.Err = client.Get(resourceURL(client, id), &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 121 | return |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 122 | } |
Jamie Hannaford | 5b16b63 | 2015-02-10 13:36:23 +0100 | [diff] [blame] | 123 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 124 | // Delete permanently destroys the database instance. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 125 | func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 126 | _, r.Err = client.Delete(resourceURL(client, id), nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 127 | return |
Jamie Hannaford | 5b16b63 | 2015-02-10 13:36:23 +0100 | [diff] [blame] | 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. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 132 | func EnableRootUser(client *gophercloud.ServiceClient, id string) (r EnableRootUserResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 133 | _, r.Err = client.Post(userRootURL(client, id), nil, &r.Body, &gophercloud.RequestOpts{OkCodes: []int{200}}) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 134 | return |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 135 | } |
Jamie Hannaford | a74d425 | 2015-02-10 15:35:01 +0100 | [diff] [blame] | 136 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 137 | // IsRootEnabled checks an instance to see if root access is enabled. It returns |
| 138 | // True if root user is enabled for the specified database instance or False |
| 139 | // otherwise. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 140 | func IsRootEnabled(client *gophercloud.ServiceClient, id string) (r IsRootEnabledResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 141 | _, r.Err = client.Get(userRootURL(client, id), &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 142 | return |
Jamie Hannaford | a74d425 | 2015-02-10 15:35:01 +0100 | [diff] [blame] | 143 | } |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 144 | |
Jamie Hannaford | 75e8cc4 | 2015-11-16 14:09:25 +0100 | [diff] [blame] | 145 | // Restart will restart only the MySQL Instance. Restarting MySQL will |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 146 | // erase any dynamic configuration settings that you have made within MySQL. |
| 147 | // The MySQL service will be unavailable until the instance restarts. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 148 | func Restart(client *gophercloud.ServiceClient, id string) (r ActionResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 149 | b := map[string]interface{}{"restart": struct{}{}} |
| 150 | _, r.Err = client.Post(actionURL(client, id), &b, nil, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 151 | return |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Jamie Hannaford | 75e8cc4 | 2015-11-16 14:09:25 +0100 | [diff] [blame] | 154 | // Resize changes the memory size of the instance, assuming a valid |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 155 | // flavorRef is provided. It will also restart the MySQL service. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 156 | func Resize(client *gophercloud.ServiceClient, id, flavorRef string) (r ActionResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 157 | b := map[string]interface{}{"resize": map[string]string{"flavorRef": flavorRef}} |
| 158 | _, r.Err = client.Post(actionURL(client, id), &b, nil, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 159 | return |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 162 | // ResizeVolume will resize the attached volume for an instance. It supports |
| 163 | // only increasing the volume size and does not support decreasing the size. |
| 164 | // The volume size is in gigabytes (GB) and must be an integer. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 165 | func ResizeVolume(client *gophercloud.ServiceClient, id string, size int) (r ActionResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 166 | b := map[string]interface{}{"resize": map[string]interface{}{"volume": map[string]int{"size": size}}} |
| 167 | _, r.Err = client.Post(actionURL(client, id), &b, nil, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 168 | return |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 169 | } |