blob: 53d9289002838349311a598572051de9cfe3f7f9 [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// CreateOpts is the struct responsible for configuring a new database instance.
12type CreateOpts struct {
13 // Either the integer UUID (in string form) of the flavor, or its URI
14 // reference as specified in the response from the List() call. Required.
15 FlavorRef string
16
17 // Specifies the volume size in gigabytes (GB). The value must be between 1
18 // and 300. Required.
19 Size int
20
21 // Name of the instance to create. The length of the name is limited to
22 // 255 characters and any characters are permitted. Optional.
23 Name string
24
Jamie Hannaford9fdda582015-02-10 12:15:43 +010025 // A slice of database information options.
Jamie Hannaford2e817322015-02-16 15:29:17 +010026 Databases osDBs.BatchCreateOpts
Jamie Hannaford9fdda582015-02-10 12:15:43 +010027
28 // A slice of user information options.
Jamie Hannaford2e817322015-02-16 15:29:17 +010029 Users osUsers.BatchCreateOpts
Jamie Hannaford9fdda582015-02-10 12:15:43 +010030
Jamie Hannafordfac40db2015-02-09 17:27:08 +010031 // ID of the configuration group to associate with the instance. Optional.
32 ConfigID string
33
34 // Options to configure the type of datastore the instance will use. This is
35 // optional, and if excluded will default to MySQL.
Jamie Hannaford99eced52015-03-02 15:24:22 +010036 Datastore *os.DatastoreOpts
Jamie Hannafordfac40db2015-02-09 17:27:08 +010037
Jamie Hannafordfac40db2015-02-09 17:27:08 +010038 // Specifies the backup ID from which to restore the database instance. There
39 // are some things to be aware of before using this field. When you execute
40 // the Restore Backup operation, a new database instance is created to store
41 // the backup whose ID is specified by the restorePoint attribute. This will
42 // mean that:
43 // - All users, passwords and access that were on the instance at the time of
44 // the backup will be restored along with the databases.
45 // - You can create new users or databases if you want, but they cannot be
46 // the same as the ones from the instance that was backed up.
47 RestorePoint string
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +010048
49 ReplicaOf string
Jamie Hannafordfac40db2015-02-09 17:27:08 +010050}
51
52func (opts CreateOpts) ToInstanceCreateMap() (map[string]interface{}, error) {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010053 instance, err := os.CreateOpts{
54 FlavorRef: opts.FlavorRef,
55 Size: opts.Size,
56 Name: opts.Name,
57 Databases: opts.Databases,
58 Users: opts.Users,
59 }.ToInstanceCreateMap()
60
61 if err != nil {
62 return nil, err
Jamie Hannafordfac40db2015-02-09 17:27:08 +010063 }
64
Jamie Hannaford9fdda582015-02-10 12:15:43 +010065 instance = instance["instance"].(map[string]interface{})
Jamie Hannafordfac40db2015-02-09 17:27:08 +010066
Jamie Hannafordfac40db2015-02-09 17:27:08 +010067 if opts.ConfigID != "" {
68 instance["configuration"] = opts.ConfigID
69 }
Jamie Hannaford9fdda582015-02-10 12:15:43 +010070
Jamie Hannafordfac40db2015-02-09 17:27:08 +010071 if opts.Datastore != nil {
72 ds, err := opts.Datastore.ToMap()
73 if err != nil {
74 return nil, err
75 }
76 instance["datastore"] = ds
77 }
Jamie Hannaford9fdda582015-02-10 12:15:43 +010078
Jamie Hannafordfac40db2015-02-09 17:27:08 +010079 if opts.RestorePoint != "" {
Jamie Hannaford302c0b62015-02-16 14:12:34 +010080 instance["restorePoint"] = map[string]string{"backupRef": opts.RestorePoint}
Jamie Hannafordfac40db2015-02-09 17:27:08 +010081 }
82
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +010083 if opts.ReplicaOf != "" {
84 instance["replica_of"] = opts.ReplicaOf
85 }
86
Jamie Hannafordfac40db2015-02-09 17:27:08 +010087 return map[string]interface{}{"instance": instance}, nil
88}
89
Jamie Hannafordcc211472015-02-10 17:01:21 +010090// Create asynchronously provisions a new database instance. It requires the
91// user to specify a flavor and a volume size. The API service then provisions
92// the instance with the requested flavor and sets up a volume of the specified
93// size, which is the storage for the database instance.
94//
95// Although this call only allows the creation of 1 instance per request, you
96// can create an instance with multiple databases and users. The default
97// binding for a MySQL instance is port 3306.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010098func Create(client *gophercloud.ServiceClient, opts os.CreateOptsBuilder) CreateResult {
99 return CreateResult{os.Create(client, opts)}
Jamie Hannafordfac40db2015-02-09 17:27:08 +0100100}
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +0100101
Jamie Hannafordcc211472015-02-10 17:01:21 +0100102// List retrieves the status and information for all database instances.
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +0100103func List(client *gophercloud.ServiceClient) pagination.Pager {
104 return os.List(client)
105}
106
Jamie Hannaford27957b22015-02-12 12:50:55 +0100107// Get retrieves the status and information for a specified database instance.
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +0100108func Get(client *gophercloud.ServiceClient, id string) GetResult {
109 return GetResult{os.Get(client, id)}
110}
Jamie Hannaford1232e042015-02-10 13:36:32 +0100111
Jamie Hannaford27957b22015-02-12 12:50:55 +0100112// Delete permanently destroys the database instance.
Jamie Hannaford1232e042015-02-10 13:36:32 +0100113func Delete(client *gophercloud.ServiceClient, id string) os.DeleteResult {
114 return os.Delete(client, id)
115}
Jamie Hannafordebcac552015-02-10 13:58:56 +0100116
Jamie Hannaford27957b22015-02-12 12:50:55 +0100117// EnableRootUser enables the login from any host for the root user and
118// provides the user with a generated root password.
Jamie Hannafordebcac552015-02-10 13:58:56 +0100119func EnableRootUser(client *gophercloud.ServiceClient, id string) os.UserRootResult {
120 return os.EnableRootUser(client, id)
121}
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100122
Jamie Hannaford27957b22015-02-12 12:50:55 +0100123// IsRootEnabled checks an instance to see if root access is enabled. It returns
124// True if root user is enabled for the specified database instance or False
125// otherwise.
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100126func IsRootEnabled(client *gophercloud.ServiceClient, id string) (bool, error) {
127 return os.IsRootEnabled(client, id)
128}
129
Jamie Hannaford27957b22015-02-12 12:50:55 +0100130// RestartService will restart only the MySQL Instance. Restarting MySQL will
131// erase any dynamic configuration settings that you have made within MySQL.
132// The MySQL service will be unavailable until the instance restarts.
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100133func RestartService(client *gophercloud.ServiceClient, id string) os.ActionResult {
134 return os.RestartService(client, id)
135}
136
Jamie Hannaford27957b22015-02-12 12:50:55 +0100137// ResizeInstance changes the memory size of the instance, assuming a valid
138// flavorRef is provided. It will also restart the MySQL service.
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100139func ResizeInstance(client *gophercloud.ServiceClient, id, flavorRef string) os.ActionResult {
140 return os.ResizeInstance(client, id, flavorRef)
141}
142
Jamie Hannaford27957b22015-02-12 12:50:55 +0100143// ResizeVolume will resize the attached volume for an instance. It supports
144// only increasing the volume size and does not support decreasing the size.
145// The volume size is in gigabytes (GB) and must be an integer.
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100146func ResizeVolume(client *gophercloud.ServiceClient, id string, size int) os.ActionResult {
147 return os.ResizeVolume(client, id, size)
148}