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 | |
| 95 | // Create will provision a new Database instance. |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 96 | func Create(client *gophercloud.ServiceClient, opts os.CreateOptsBuilder) CreateResult { |
| 97 | return CreateResult{os.Create(client, opts)} |
Jamie Hannaford | fac40db | 2015-02-09 17:27:08 +0100 | [diff] [blame] | 98 | } |
Jamie Hannaford | 39d4ffb | 2015-02-10 13:19:44 +0100 | [diff] [blame^] | 99 | |
| 100 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 101 | return os.List(client) |
| 102 | } |
| 103 | |
| 104 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
| 105 | return GetResult{os.Get(client, id)} |
| 106 | } |