blob: f072d84431a4e8bab3432d5f7e215a13180f82fa [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
Jamie Hannaford27957b22015-02-12 12:50:55 +0100112// Get retrieves the status and information for a specified database instance.
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +0100113func Get(client *gophercloud.ServiceClient, id string) GetResult {
114 return GetResult{os.Get(client, id)}
115}
Jamie Hannaford1232e042015-02-10 13:36:32 +0100116
Jamie Hannaford27957b22015-02-12 12:50:55 +0100117// Delete permanently destroys the database instance.
Jamie Hannaford1232e042015-02-10 13:36:32 +0100118func Delete(client *gophercloud.ServiceClient, id string) os.DeleteResult {
119 return os.Delete(client, id)
120}
Jamie Hannafordebcac552015-02-10 13:58:56 +0100121
Jamie Hannaford27957b22015-02-12 12:50:55 +0100122// EnableRootUser enables the login from any host for the root user and
123// provides the user with a generated root password.
Jamie Hannafordebcac552015-02-10 13:58:56 +0100124func EnableRootUser(client *gophercloud.ServiceClient, id string) os.UserRootResult {
125 return os.EnableRootUser(client, id)
126}
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100127
Jamie Hannaford27957b22015-02-12 12:50:55 +0100128// IsRootEnabled checks an instance to see if root access is enabled. It returns
129// True if root user is enabled for the specified database instance or False
130// otherwise.
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100131func IsRootEnabled(client *gophercloud.ServiceClient, id string) (bool, error) {
132 return os.IsRootEnabled(client, id)
133}
134
Jamie Hannaford27957b22015-02-12 12:50:55 +0100135// RestartService will restart only the MySQL Instance. Restarting MySQL will
136// erase any dynamic configuration settings that you have made within MySQL.
137// The MySQL service will be unavailable until the instance restarts.
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100138func RestartService(client *gophercloud.ServiceClient, id string) os.ActionResult {
139 return os.RestartService(client, id)
140}
141
Jamie Hannaford27957b22015-02-12 12:50:55 +0100142// ResizeInstance changes the memory size of the instance, assuming a valid
143// flavorRef is provided. It will also restart the MySQL service.
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100144func ResizeInstance(client *gophercloud.ServiceClient, id, flavorRef string) os.ActionResult {
145 return os.ResizeInstance(client, id, flavorRef)
146}
147
Jamie Hannaford27957b22015-02-12 12:50:55 +0100148// ResizeVolume will resize the attached volume for an instance. It supports
149// only increasing the volume size and does not support decreasing the size.
150// The volume size is in gigabytes (GB) and must be an integer.
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100151func ResizeVolume(client *gophercloud.ServiceClient, id string, size int) os.ActionResult {
152 return os.ResizeVolume(client, id, size)
153}