blob: c8551dfcfa54ba030aacec5fe9ed85bd3b934353 [file] [log] [blame]
Jamie Hannafordfac40db2015-02-09 17:27:08 +01001package instances
2
3import (
Jamie Hannafordfac40db2015-02-09 17:27:08 +01004 "github.com/rackspace/gophercloud"
Jamie Hannaford9fdda582015-02-10 12:15:43 +01005 os "github.com/rackspace/gophercloud/openstack/db/v1/instances"
Jamie Hannafordfac40db2015-02-09 17:27:08 +01006)
7
Jamie Hannafordfac40db2015-02-09 17:27:08 +01008// DatastoreOpts represents the configuration for how an instance stores data.
9type DatastoreOpts struct {
10 Version string
11 Type string
12}
13
14func (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 Hannafordfac40db2015-02-09 17:27:08 +010021// CreateOpts is the struct responsible for configuring a new database instance.
22type 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 Hannaford9fdda582015-02-10 12:15:43 +010035 // A slice of database information options.
36 Databases os.DatabasesOpts
37
38 // A slice of user information options.
39 Users os.UsersOpts
40
Jamie Hannafordfac40db2015-02-09 17:27:08 +010041 // 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 Hannafordfac40db2015-02-09 17:27:08 +010048 // 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
60func (opts CreateOpts) ToInstanceCreateMap() (map[string]interface{}, error) {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010061 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 Hannafordfac40db2015-02-09 17:27:08 +010071 }
72
Jamie Hannaford9fdda582015-02-10 12:15:43 +010073 instance = instance["instance"].(map[string]interface{})
Jamie Hannafordfac40db2015-02-09 17:27:08 +010074
Jamie Hannafordfac40db2015-02-09 17:27:08 +010075 if opts.ConfigID != "" {
76 instance["configuration"] = opts.ConfigID
77 }
Jamie Hannaford9fdda582015-02-10 12:15:43 +010078
Jamie Hannafordfac40db2015-02-09 17:27:08 +010079 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 Hannaford9fdda582015-02-10 12:15:43 +010086
Jamie Hannafordfac40db2015-02-09 17:27:08 +010087 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 Hannaford9fdda582015-02-10 12:15:43 +010095func Create(client *gophercloud.ServiceClient, opts os.CreateOptsBuilder) CreateResult {
96 return CreateResult{os.Create(client, opts)}
Jamie Hannafordfac40db2015-02-09 17:27:08 +010097}