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