blob: ca585e268d822b7f9f19e20989cad29072dffb20 [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
Jamie Hannafordcc211472015-02-10 17:01:21 +010095// 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 Hannaford9fdda582015-02-10 12:15:43 +0100103func Create(client *gophercloud.ServiceClient, opts os.CreateOptsBuilder) CreateResult {
104 return CreateResult{os.Create(client, opts)}
Jamie Hannafordfac40db2015-02-09 17:27:08 +0100105}
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +0100106
Jamie Hannafordcc211472015-02-10 17:01:21 +0100107// List retrieves the status and information for all database instances.
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +0100108func List(client *gophercloud.ServiceClient) pagination.Pager {
109 return os.List(client)
110}
111
112func Get(client *gophercloud.ServiceClient, id string) GetResult {
113 return GetResult{os.Get(client, id)}
114}
Jamie Hannaford1232e042015-02-10 13:36:32 +0100115
116func Delete(client *gophercloud.ServiceClient, id string) os.DeleteResult {
117 return os.Delete(client, id)
118}
Jamie Hannafordebcac552015-02-10 13:58:56 +0100119
120func EnableRootUser(client *gophercloud.ServiceClient, id string) os.UserRootResult {
121 return os.EnableRootUser(client, id)
122}
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100123
124func IsRootEnabled(client *gophercloud.ServiceClient, id string) (bool, error) {
125 return os.IsRootEnabled(client, id)
126}
127
128func RestartService(client *gophercloud.ServiceClient, id string) os.ActionResult {
129 return os.RestartService(client, id)
130}
131
132func ResizeInstance(client *gophercloud.ServiceClient, id, flavorRef string) os.ActionResult {
133 return os.ResizeInstance(client, id, flavorRef)
134}
135
136func ResizeVolume(client *gophercloud.ServiceClient, id string, size int) os.ActionResult {
137 return os.ResizeVolume(client, id, size)
138}