blob: 47f7fb26fe982be73b81b1dfe864835e4a5f5a18 [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 Hannaford39d4ffb2015-02-10 13:19:44 +01006 "github.com/rackspace/gophercloud/pagination"
Jamie Hannafordfac40db2015-02-09 17:27:08 +01007)
8
Jamie Hannafordfac40db2015-02-09 17:27:08 +01009// DatastoreOpts represents the configuration for how an instance stores data.
10type DatastoreOpts struct {
11 Version string
12 Type string
13}
14
15func (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 Hannafordfac40db2015-02-09 17:27:08 +010022// CreateOpts is the struct responsible for configuring a new database instance.
23type 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 Hannaford9fdda582015-02-10 12:15:43 +010036 // A slice of database information options.
37 Databases os.DatabasesOpts
38
39 // A slice of user information options.
40 Users os.UsersOpts
41
Jamie Hannafordfac40db2015-02-09 17:27:08 +010042 // 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 Hannafordfac40db2015-02-09 17:27:08 +010049 // 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
61func (opts CreateOpts) ToInstanceCreateMap() (map[string]interface{}, error) {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010062 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 Hannafordfac40db2015-02-09 17:27:08 +010072 }
73
Jamie Hannaford9fdda582015-02-10 12:15:43 +010074 instance = instance["instance"].(map[string]interface{})
Jamie Hannafordfac40db2015-02-09 17:27:08 +010075
Jamie Hannafordfac40db2015-02-09 17:27:08 +010076 if opts.ConfigID != "" {
77 instance["configuration"] = opts.ConfigID
78 }
Jamie Hannaford9fdda582015-02-10 12:15:43 +010079
Jamie Hannafordfac40db2015-02-09 17:27:08 +010080 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 Hannaford9fdda582015-02-10 12:15:43 +010087
Jamie Hannafordfac40db2015-02-09 17:27:08 +010088 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 Hannaford9fdda582015-02-10 12:15:43 +010096func Create(client *gophercloud.ServiceClient, opts os.CreateOptsBuilder) CreateResult {
97 return CreateResult{os.Create(client, opts)}
Jamie Hannafordfac40db2015-02-09 17:27:08 +010098}
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010099
100func List(client *gophercloud.ServiceClient) pagination.Pager {
101 return os.List(client)
102}
103
104func Get(client *gophercloud.ServiceClient, id string) GetResult {
105 return GetResult{os.Get(client, id)}
106}
Jamie Hannaford1232e042015-02-10 13:36:32 +0100107
108func Delete(client *gophercloud.ServiceClient, id string) os.DeleteResult {
109 return os.Delete(client, id)
110}
Jamie Hannafordebcac552015-02-10 13:58:56 +0100111
112func EnableRootUser(client *gophercloud.ServiceClient, id string) os.UserRootResult {
113 return os.EnableRootUser(client, id)
114}