blob: d2e3c25c282320a1f186dfecdca1521fe7f4aa0f [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 Hannaford2e817322015-02-16 15:29:17 +01005 osDBs "github.com/rackspace/gophercloud/openstack/db/v1/databases"
Jamie Hannaford9fdda582015-02-10 12:15:43 +01006 os "github.com/rackspace/gophercloud/openstack/db/v1/instances"
Jamie Hannaford2e817322015-02-16 15:29:17 +01007 osUsers "github.com/rackspace/gophercloud/openstack/db/v1/users"
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +01008 "github.com/rackspace/gophercloud/pagination"
Jamie Hannafordfac40db2015-02-09 17:27:08 +01009)
10
Jamie Hannafordfac40db2015-02-09 17:27:08 +010011// DatastoreOpts represents the configuration for how an instance stores data.
12type DatastoreOpts struct {
13 Version string
14 Type string
15}
16
17func (opts DatastoreOpts) ToMap() (map[string]string, error) {
18 return map[string]string{
19 "version": opts.Version,
20 "type": opts.Type,
21 }, nil
22}
23
Jamie Hannafordfac40db2015-02-09 17:27:08 +010024// CreateOpts is the struct responsible for configuring a new database instance.
25type CreateOpts struct {
26 // Either the integer UUID (in string form) of the flavor, or its URI
27 // reference as specified in the response from the List() call. Required.
28 FlavorRef string
29
30 // Specifies the volume size in gigabytes (GB). The value must be between 1
31 // and 300. Required.
32 Size int
33
34 // Name of the instance to create. The length of the name is limited to
35 // 255 characters and any characters are permitted. Optional.
36 Name string
37
Jamie Hannaford9fdda582015-02-10 12:15:43 +010038 // A slice of database information options.
Jamie Hannaford2e817322015-02-16 15:29:17 +010039 Databases osDBs.BatchCreateOpts
Jamie Hannaford9fdda582015-02-10 12:15:43 +010040
41 // A slice of user information options.
Jamie Hannaford2e817322015-02-16 15:29:17 +010042 Users osUsers.BatchCreateOpts
Jamie Hannaford9fdda582015-02-10 12:15:43 +010043
Jamie Hannafordfac40db2015-02-09 17:27:08 +010044 // ID of the configuration group to associate with the instance. Optional.
45 ConfigID string
46
47 // Options to configure the type of datastore the instance will use. This is
48 // optional, and if excluded will default to MySQL.
49 Datastore *DatastoreOpts
50
Jamie Hannafordfac40db2015-02-09 17:27:08 +010051 // Specifies the backup ID from which to restore the database instance. There
52 // are some things to be aware of before using this field. When you execute
53 // the Restore Backup operation, a new database instance is created to store
54 // the backup whose ID is specified by the restorePoint attribute. This will
55 // mean that:
56 // - All users, passwords and access that were on the instance at the time of
57 // the backup will be restored along with the databases.
58 // - You can create new users or databases if you want, but they cannot be
59 // the same as the ones from the instance that was backed up.
60 RestorePoint string
61}
62
63func (opts CreateOpts) ToInstanceCreateMap() (map[string]interface{}, error) {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010064 instance, err := os.CreateOpts{
65 FlavorRef: opts.FlavorRef,
66 Size: opts.Size,
67 Name: opts.Name,
68 Databases: opts.Databases,
69 Users: opts.Users,
70 }.ToInstanceCreateMap()
71
72 if err != nil {
73 return nil, err
Jamie Hannafordfac40db2015-02-09 17:27:08 +010074 }
75
Jamie Hannaford9fdda582015-02-10 12:15:43 +010076 instance = instance["instance"].(map[string]interface{})
Jamie Hannafordfac40db2015-02-09 17:27:08 +010077
Jamie Hannafordfac40db2015-02-09 17:27:08 +010078 if opts.ConfigID != "" {
79 instance["configuration"] = opts.ConfigID
80 }
Jamie Hannaford9fdda582015-02-10 12:15:43 +010081
Jamie Hannafordfac40db2015-02-09 17:27:08 +010082 if opts.Datastore != nil {
83 ds, err := opts.Datastore.ToMap()
84 if err != nil {
85 return nil, err
86 }
87 instance["datastore"] = ds
88 }
Jamie Hannaford9fdda582015-02-10 12:15:43 +010089
Jamie Hannafordfac40db2015-02-09 17:27:08 +010090 if opts.RestorePoint != "" {
Jamie Hannaford302c0b62015-02-16 14:12:34 +010091 instance["restorePoint"] = map[string]string{"backupRef": opts.RestorePoint}
Jamie Hannafordfac40db2015-02-09 17:27:08 +010092 }
93
94 return map[string]interface{}{"instance": instance}, nil
95}
96
Jamie Hannafordcc211472015-02-10 17:01:21 +010097// Create asynchronously provisions a new database instance. It requires the
98// user to specify a flavor and a volume size. The API service then provisions
99// the instance with the requested flavor and sets up a volume of the specified
100// size, which is the storage for the database instance.
101//
102// Although this call only allows the creation of 1 instance per request, you
103// can create an instance with multiple databases and users. The default
104// binding for a MySQL instance is port 3306.
Jamie Hannaford9fdda582015-02-10 12:15:43 +0100105func Create(client *gophercloud.ServiceClient, opts os.CreateOptsBuilder) CreateResult {
106 return CreateResult{os.Create(client, opts)}
Jamie Hannafordfac40db2015-02-09 17:27:08 +0100107}
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +0100108
Jamie Hannafordcc211472015-02-10 17:01:21 +0100109// List retrieves the status and information for all database instances.
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +0100110func List(client *gophercloud.ServiceClient) pagination.Pager {
111 return os.List(client)
112}
113
Jamie Hannaford27957b22015-02-12 12:50:55 +0100114// Get retrieves the status and information for a specified database instance.
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +0100115func Get(client *gophercloud.ServiceClient, id string) GetResult {
116 return GetResult{os.Get(client, id)}
117}
Jamie Hannaford1232e042015-02-10 13:36:32 +0100118
Jamie Hannaford27957b22015-02-12 12:50:55 +0100119// Delete permanently destroys the database instance.
Jamie Hannaford1232e042015-02-10 13:36:32 +0100120func Delete(client *gophercloud.ServiceClient, id string) os.DeleteResult {
121 return os.Delete(client, id)
122}
Jamie Hannafordebcac552015-02-10 13:58:56 +0100123
Jamie Hannaford27957b22015-02-12 12:50:55 +0100124// EnableRootUser enables the login from any host for the root user and
125// provides the user with a generated root password.
Jamie Hannafordebcac552015-02-10 13:58:56 +0100126func EnableRootUser(client *gophercloud.ServiceClient, id string) os.UserRootResult {
127 return os.EnableRootUser(client, id)
128}
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100129
Jamie Hannaford27957b22015-02-12 12:50:55 +0100130// IsRootEnabled checks an instance to see if root access is enabled. It returns
131// True if root user is enabled for the specified database instance or False
132// otherwise.
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100133func IsRootEnabled(client *gophercloud.ServiceClient, id string) (bool, error) {
134 return os.IsRootEnabled(client, id)
135}
136
Jamie Hannaford27957b22015-02-12 12:50:55 +0100137// RestartService will restart only the MySQL Instance. Restarting MySQL will
138// erase any dynamic configuration settings that you have made within MySQL.
139// The MySQL service will be unavailable until the instance restarts.
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100140func RestartService(client *gophercloud.ServiceClient, id string) os.ActionResult {
141 return os.RestartService(client, id)
142}
143
Jamie Hannaford27957b22015-02-12 12:50:55 +0100144// ResizeInstance changes the memory size of the instance, assuming a valid
145// flavorRef is provided. It will also restart the MySQL service.
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100146func ResizeInstance(client *gophercloud.ServiceClient, id, flavorRef string) os.ActionResult {
147 return os.ResizeInstance(client, id, flavorRef)
148}
149
Jamie Hannaford27957b22015-02-12 12:50:55 +0100150// ResizeVolume will resize the attached volume for an instance. It supports
151// only increasing the volume size and does not support decreasing the size.
152// The volume size is in gigabytes (GB) and must be an integer.
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100153func ResizeVolume(client *gophercloud.ServiceClient, id string, size int) os.ActionResult {
154 return os.ResizeVolume(client, id, size)
155}