blob: ca223ebd720fb2645db669d0043caf551ca5c331 [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
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +010061
62 ReplicaOf string
Jamie Hannafordfac40db2015-02-09 17:27:08 +010063}
64
65func (opts CreateOpts) ToInstanceCreateMap() (map[string]interface{}, error) {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010066 instance, err := os.CreateOpts{
67 FlavorRef: opts.FlavorRef,
68 Size: opts.Size,
69 Name: opts.Name,
70 Databases: opts.Databases,
71 Users: opts.Users,
72 }.ToInstanceCreateMap()
73
74 if err != nil {
75 return nil, err
Jamie Hannafordfac40db2015-02-09 17:27:08 +010076 }
77
Jamie Hannaford9fdda582015-02-10 12:15:43 +010078 instance = instance["instance"].(map[string]interface{})
Jamie Hannafordfac40db2015-02-09 17:27:08 +010079
Jamie Hannafordfac40db2015-02-09 17:27:08 +010080 if opts.ConfigID != "" {
81 instance["configuration"] = opts.ConfigID
82 }
Jamie Hannaford9fdda582015-02-10 12:15:43 +010083
Jamie Hannafordfac40db2015-02-09 17:27:08 +010084 if opts.Datastore != nil {
85 ds, err := opts.Datastore.ToMap()
86 if err != nil {
87 return nil, err
88 }
89 instance["datastore"] = ds
90 }
Jamie Hannaford9fdda582015-02-10 12:15:43 +010091
Jamie Hannafordfac40db2015-02-09 17:27:08 +010092 if opts.RestorePoint != "" {
Jamie Hannaford302c0b62015-02-16 14:12:34 +010093 instance["restorePoint"] = map[string]string{"backupRef": opts.RestorePoint}
Jamie Hannafordfac40db2015-02-09 17:27:08 +010094 }
95
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +010096 if opts.ReplicaOf != "" {
97 instance["replica_of"] = opts.ReplicaOf
98 }
99
Jamie Hannafordfac40db2015-02-09 17:27:08 +0100100 return map[string]interface{}{"instance": instance}, nil
101}
102
Jamie Hannafordcc211472015-02-10 17:01:21 +0100103// Create asynchronously provisions a new database instance. It requires the
104// user to specify a flavor and a volume size. The API service then provisions
105// the instance with the requested flavor and sets up a volume of the specified
106// size, which is the storage for the database instance.
107//
108// Although this call only allows the creation of 1 instance per request, you
109// can create an instance with multiple databases and users. The default
110// binding for a MySQL instance is port 3306.
Jamie Hannaford9fdda582015-02-10 12:15:43 +0100111func Create(client *gophercloud.ServiceClient, opts os.CreateOptsBuilder) CreateResult {
112 return CreateResult{os.Create(client, opts)}
Jamie Hannafordfac40db2015-02-09 17:27:08 +0100113}
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +0100114
Jamie Hannafordcc211472015-02-10 17:01:21 +0100115// List retrieves the status and information for all database instances.
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +0100116func List(client *gophercloud.ServiceClient) pagination.Pager {
117 return os.List(client)
118}
119
Jamie Hannaford27957b22015-02-12 12:50:55 +0100120// Get retrieves the status and information for a specified database instance.
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +0100121func Get(client *gophercloud.ServiceClient, id string) GetResult {
122 return GetResult{os.Get(client, id)}
123}
Jamie Hannaford1232e042015-02-10 13:36:32 +0100124
Jamie Hannaford27957b22015-02-12 12:50:55 +0100125// Delete permanently destroys the database instance.
Jamie Hannaford1232e042015-02-10 13:36:32 +0100126func Delete(client *gophercloud.ServiceClient, id string) os.DeleteResult {
127 return os.Delete(client, id)
128}
Jamie Hannafordebcac552015-02-10 13:58:56 +0100129
Jamie Hannaford27957b22015-02-12 12:50:55 +0100130// EnableRootUser enables the login from any host for the root user and
131// provides the user with a generated root password.
Jamie Hannafordebcac552015-02-10 13:58:56 +0100132func EnableRootUser(client *gophercloud.ServiceClient, id string) os.UserRootResult {
133 return os.EnableRootUser(client, id)
134}
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100135
Jamie Hannaford27957b22015-02-12 12:50:55 +0100136// IsRootEnabled checks an instance to see if root access is enabled. It returns
137// True if root user is enabled for the specified database instance or False
138// otherwise.
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100139func IsRootEnabled(client *gophercloud.ServiceClient, id string) (bool, error) {
140 return os.IsRootEnabled(client, id)
141}
142
Jamie Hannaford27957b22015-02-12 12:50:55 +0100143// RestartService will restart only the MySQL Instance. Restarting MySQL will
144// erase any dynamic configuration settings that you have made within MySQL.
145// The MySQL service will be unavailable until the instance restarts.
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100146func RestartService(client *gophercloud.ServiceClient, id string) os.ActionResult {
147 return os.RestartService(client, id)
148}
149
Jamie Hannaford27957b22015-02-12 12:50:55 +0100150// ResizeInstance changes the memory size of the instance, assuming a valid
151// flavorRef is provided. It will also restart the MySQL service.
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100152func ResizeInstance(client *gophercloud.ServiceClient, id, flavorRef string) os.ActionResult {
153 return os.ResizeInstance(client, id, flavorRef)
154}
155
Jamie Hannaford27957b22015-02-12 12:50:55 +0100156// ResizeVolume will resize the attached volume for an instance. It supports
157// only increasing the volume size and does not support decreasing the size.
158// The volume size is in gigabytes (GB) and must be an integer.
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100159func ResizeVolume(client *gophercloud.ServiceClient, id string, size int) os.ActionResult {
160 return os.ResizeVolume(client, id, size)
161}