Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 1 | package instances |
| 2 | |
| 3 | import ( |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 4 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 2e81732 | 2015-02-16 15:29:17 +0100 | [diff] [blame] | 5 | osDBs "github.com/rackspace/gophercloud/openstack/db/v1/databases" |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 6 | os "github.com/rackspace/gophercloud/openstack/db/v1/instances" |
Jamie Hannaford | 2e81732 | 2015-02-16 15:29:17 +0100 | [diff] [blame] | 7 | osUsers "github.com/rackspace/gophercloud/openstack/db/v1/users" |
Jamie Hannaford | 39d4ffb | 2015-02-10 13:19:44 +0100 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 9 | ) |
| 10 | |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 11 | // DatastoreOpts represents the configuration for how an instance stores data. |
| 12 | type DatastoreOpts struct { |
| 13 | Version string |
| 14 | Type string |
| 15 | } |
| 16 | |
| 17 | func (opts DatastoreOpts) ToMap() (map[string]string, error) { |
| 18 | return map[string]string{ |
| 19 | "version": opts.Version, |
| 20 | "type": opts.Type, |
| 21 | }, nil |
| 22 | } |
| 23 | |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 24 | // CreateOpts is the struct responsible for configuring a new database instance. |
| 25 | type CreateOpts struct { |
| 26 | // Either the integer UUID (in string form) of the flavor, or its URI |
| 27 | // reference as specified in the response from the List() call. Required. |
| 28 | FlavorRef string |
| 29 | |
| 30 | // Specifies the volume size in gigabytes (GB). The value must be between 1 |
| 31 | // and 300. Required. |
| 32 | Size int |
| 33 | |
| 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 |
| 37 | |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 38 | // A slice of database information options. |
Jamie Hannaford | 2e81732 | 2015-02-16 15:29:17 +0100 | [diff] [blame] | 39 | Databases osDBs.BatchCreateOpts |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 40 | |
| 41 | // A slice of user information options. |
Jamie Hannaford | 2e81732 | 2015-02-16 15:29:17 +0100 | [diff] [blame] | 42 | Users osUsers.BatchCreateOpts |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 43 | |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 44 | // ID of the configuration group to associate with the instance. Optional. |
| 45 | ConfigID string |
| 46 | |
| 47 | // Options to configure the type of datastore the instance will use. This is |
| 48 | // optional, and if excluded will default to MySQL. |
| 49 | Datastore *DatastoreOpts |
| 50 | |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 51 | // Specifies the backup ID from which to restore the database instance. There |
| 52 | // are some things to be aware of before using this field. When you execute |
| 53 | // the Restore Backup operation, a new database instance is created to store |
| 54 | // the backup whose ID is specified by the restorePoint attribute. This will |
| 55 | // mean that: |
| 56 | // - All users, passwords and access that were on the instance at the time of |
| 57 | // the backup will be restored along with the databases. |
| 58 | // - You can create new users or databases if you want, but they cannot be |
| 59 | // the same as the ones from the instance that was backed up. |
| 60 | RestorePoint string |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 61 | |
| 62 | ReplicaOf string |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | func (opts CreateOpts) ToInstanceCreateMap() (map[string]interface{}, error) { |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 66 | instance, err := os.CreateOpts{ |
| 67 | FlavorRef: opts.FlavorRef, |
| 68 | Size: opts.Size, |
| 69 | Name: opts.Name, |
| 70 | Databases: opts.Databases, |
| 71 | Users: opts.Users, |
| 72 | }.ToInstanceCreateMap() |
| 73 | |
| 74 | if err != nil { |
| 75 | return nil, err |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 78 | instance = instance["instance"].(map[string]interface{}) |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 79 | |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 80 | if opts.ConfigID != "" { |
| 81 | instance["configuration"] = opts.ConfigID |
| 82 | } |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 83 | |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 84 | if opts.Datastore != nil { |
| 85 | ds, err := opts.Datastore.ToMap() |
| 86 | if err != nil { |
| 87 | return nil, err |
| 88 | } |
| 89 | instance["datastore"] = ds |
| 90 | } |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 91 | |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 92 | if opts.RestorePoint != "" { |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 93 | instance["restorePoint"] = map[string]string{"backupRef": opts.RestorePoint} |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 96 | if opts.ReplicaOf != "" { |
| 97 | instance["replica_of"] = opts.ReplicaOf |
| 98 | } |
| 99 | |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 100 | return map[string]interface{}{"instance": instance}, nil |
| 101 | } |
| 102 | |
Jamie Hannaford | cc21147 | 2015-02-10 17:01:21 +0100 | [diff] [blame] | 103 | // Create asynchronously provisions a new database instance. It requires the |
| 104 | // user to specify a flavor and a volume size. The API service then provisions |
| 105 | // the instance with the requested flavor and sets up a volume of the specified |
| 106 | // size, which is the storage for the database instance. |
| 107 | // |
| 108 | // Although this call only allows the creation of 1 instance per request, you |
| 109 | // can create an instance with multiple databases and users. The default |
| 110 | // binding for a MySQL instance is port 3306. |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 111 | func Create(client *gophercloud.ServiceClient, opts os.CreateOptsBuilder) CreateResult { |
| 112 | return CreateResult{os.Create(client, opts)} |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 113 | } |
Jamie Hannaford | 39d4ffb | 2015-02-10 13:19:44 +0100 | [diff] [blame] | 114 | |
Jamie Hannaford | cc21147 | 2015-02-10 17:01:21 +0100 | [diff] [blame] | 115 | // List retrieves the status and information for all database instances. |
Jamie Hannaford | 39d4ffb | 2015-02-10 13:19:44 +0100 | [diff] [blame] | 116 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 117 | return os.List(client) |
| 118 | } |
| 119 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 120 | // Get retrieves the status and information for a specified database instance. |
Jamie Hannaford | 39d4ffb | 2015-02-10 13:19:44 +0100 | [diff] [blame] | 121 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
| 122 | return GetResult{os.Get(client, id)} |
| 123 | } |
Jamie Hannaford | 1232e04 | 2015-02-10 13:36:32 +0100 | [diff] [blame] | 124 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 125 | // Delete permanently destroys the database instance. |
Jamie Hannaford | 1232e04 | 2015-02-10 13:36:32 +0100 | [diff] [blame] | 126 | func Delete(client *gophercloud.ServiceClient, id string) os.DeleteResult { |
| 127 | return os.Delete(client, id) |
| 128 | } |
Jamie Hannaford | ebcac55 | 2015-02-10 13:58:56 +0100 | [diff] [blame] | 129 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +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 | ebcac55 | 2015-02-10 13:58:56 +0100 | [diff] [blame] | 132 | func EnableRootUser(client *gophercloud.ServiceClient, id string) os.UserRootResult { |
| 133 | return os.EnableRootUser(client, id) |
| 134 | } |
Jamie Hannaford | e6390d4 | 2015-02-10 15:59:28 +0100 | [diff] [blame] | 135 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 136 | // IsRootEnabled checks an instance to see if root access is enabled. It returns |
| 137 | // True if root user is enabled for the specified database instance or False |
| 138 | // otherwise. |
Jamie Hannaford | e6390d4 | 2015-02-10 15:59:28 +0100 | [diff] [blame] | 139 | func IsRootEnabled(client *gophercloud.ServiceClient, id string) (bool, error) { |
| 140 | return os.IsRootEnabled(client, id) |
| 141 | } |
| 142 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 143 | // RestartService will restart only the MySQL Instance. Restarting MySQL will |
| 144 | // erase any dynamic configuration settings that you have made within MySQL. |
| 145 | // The MySQL service will be unavailable until the instance restarts. |
Jamie Hannaford | e6390d4 | 2015-02-10 15:59:28 +0100 | [diff] [blame] | 146 | func RestartService(client *gophercloud.ServiceClient, id string) os.ActionResult { |
| 147 | return os.RestartService(client, id) |
| 148 | } |
| 149 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 150 | // ResizeInstance changes the memory size of the instance, assuming a valid |
| 151 | // flavorRef is provided. It will also restart the MySQL service. |
Jamie Hannaford | e6390d4 | 2015-02-10 15:59:28 +0100 | [diff] [blame] | 152 | func ResizeInstance(client *gophercloud.ServiceClient, id, flavorRef string) os.ActionResult { |
| 153 | return os.ResizeInstance(client, id, flavorRef) |
| 154 | } |
| 155 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 156 | // ResizeVolume will resize the attached volume for an instance. It supports |
| 157 | // only increasing the volume size and does not support decreasing the size. |
| 158 | // The volume size is in gigabytes (GB) and must be an integer. |
Jamie Hannaford | e6390d4 | 2015-02-10 15:59:28 +0100 | [diff] [blame] | 159 | func ResizeVolume(client *gophercloud.ServiceClient, id string, size int) os.ActionResult { |
| 160 | return os.ResizeVolume(client, id, size) |
| 161 | } |