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 | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 6 | ) |
| 7 | |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 8 | // DatastoreOpts represents the configuration for how an instance stores data. |
| 9 | type DatastoreOpts struct { |
| 10 | Version string |
| 11 | Type string |
| 12 | } |
| 13 | |
| 14 | func (opts DatastoreOpts) ToMap() (map[string]string, error) { |
| 15 | return map[string]string{ |
| 16 | "version": opts.Version, |
| 17 | "type": opts.Type, |
| 18 | }, nil |
| 19 | } |
| 20 | |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 21 | // CreateOpts is the struct responsible for configuring a new database instance. |
| 22 | type CreateOpts struct { |
| 23 | // Either the integer UUID (in string form) of the flavor, or its URI |
| 24 | // reference as specified in the response from the List() call. Required. |
| 25 | FlavorRef string |
| 26 | |
| 27 | // Specifies the volume size in gigabytes (GB). The value must be between 1 |
| 28 | // and 300. Required. |
| 29 | Size int |
| 30 | |
| 31 | // Name of the instance to create. The length of the name is limited to |
| 32 | // 255 characters and any characters are permitted. Optional. |
| 33 | Name string |
| 34 | |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 35 | // A slice of database information options. |
| 36 | Databases os.DatabasesOpts |
| 37 | |
| 38 | // A slice of user information options. |
| 39 | Users os.UsersOpts |
| 40 | |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 41 | // ID of the configuration group to associate with the instance. Optional. |
| 42 | ConfigID string |
| 43 | |
| 44 | // Options to configure the type of datastore the instance will use. This is |
| 45 | // optional, and if excluded will default to MySQL. |
| 46 | Datastore *DatastoreOpts |
| 47 | |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 48 | // Specifies the backup ID from which to restore the database instance. There |
| 49 | // are some things to be aware of before using this field. When you execute |
| 50 | // the Restore Backup operation, a new database instance is created to store |
| 51 | // the backup whose ID is specified by the restorePoint attribute. This will |
| 52 | // mean that: |
| 53 | // - All users, passwords and access that were on the instance at the time of |
| 54 | // the backup will be restored along with the databases. |
| 55 | // - You can create new users or databases if you want, but they cannot be |
| 56 | // the same as the ones from the instance that was backed up. |
| 57 | RestorePoint string |
| 58 | } |
| 59 | |
| 60 | func (opts CreateOpts) ToInstanceCreateMap() (map[string]interface{}, error) { |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 61 | instance, err := os.CreateOpts{ |
| 62 | FlavorRef: opts.FlavorRef, |
| 63 | Size: opts.Size, |
| 64 | Name: opts.Name, |
| 65 | Databases: opts.Databases, |
| 66 | Users: opts.Users, |
| 67 | }.ToInstanceCreateMap() |
| 68 | |
| 69 | if err != nil { |
| 70 | return nil, err |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 73 | instance = instance["instance"].(map[string]interface{}) |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 74 | |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 75 | if opts.ConfigID != "" { |
| 76 | instance["configuration"] = opts.ConfigID |
| 77 | } |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 78 | |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 79 | if opts.Datastore != nil { |
| 80 | ds, err := opts.Datastore.ToMap() |
| 81 | if err != nil { |
| 82 | return nil, err |
| 83 | } |
| 84 | instance["datastore"] = ds |
| 85 | } |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 86 | |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 87 | if opts.RestorePoint != "" { |
| 88 | instance["restorePoint"] = opts.RestorePoint |
| 89 | } |
| 90 | |
| 91 | return map[string]interface{}{"instance": instance}, nil |
| 92 | } |
| 93 | |
| 94 | // Create will provision a new Database instance. |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 95 | func Create(client *gophercloud.ServiceClient, opts os.CreateOptsBuilder) CreateResult { |
| 96 | return CreateResult{os.Create(client, opts)} |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 97 | } |