blob: f4df692a429cde61acee5c24215480372792fabe [file] [log] [blame]
Jamie Hannaford936a5472015-02-10 14:38:28 +01001package instances
2
3import (
Jamie Hannaford936a5472015-02-10 14:38:28 +01004 "github.com/rackspace/gophercloud"
Jamie Hannaford72749162015-10-14 12:14:32 +02005 osDBs "github.com/rackspace/gophercloud/openstack/db/v1/databases"
6 os "github.com/rackspace/gophercloud/openstack/db/v1/instances"
7 osUsers "github.com/rackspace/gophercloud/openstack/db/v1/users"
Jamie Hannaford302c0b62015-02-16 14:12:34 +01008 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford2e817322015-02-16 15:29:17 +01009 "github.com/rackspace/gophercloud/rackspace/db/v1/backups"
Jamie Hannaford936a5472015-02-10 14:38:28 +010010)
11
Jamie Hannaford72749162015-10-14 12:14:32 +020012// CreateOpts is the struct responsible for configuring a new database instance.
13type CreateOpts struct {
14 // Either the integer UUID (in string form) of the flavor, or its URI
15 // reference as specified in the response from the List() call. Required.
16 FlavorRef string
17
18 // Specifies the volume size in gigabytes (GB). The value must be between 1
19 // and 300. Required.
20 Size int
21
22 // Name of the instance to create. The length of the name is limited to
23 // 255 characters and any characters are permitted. Optional.
24 Name string
25
26 // A slice of database information options.
Jamie Hannaford2e695a32015-11-16 16:36:10 +010027 Databases osDBs.CreateOptsBuilder
Jamie Hannaford72749162015-10-14 12:14:32 +020028
29 // A slice of user information options.
Jamie Hannaford2e695a32015-11-16 16:36:10 +010030 Users osUsers.CreateOptsBuilder
Jamie Hannaford72749162015-10-14 12:14:32 +020031
32 // ID of the configuration group to associate with the instance. Optional.
33 ConfigID string
34
35 // Options to configure the type of datastore the instance will use. This is
36 // optional, and if excluded will default to MySQL.
37 Datastore *os.DatastoreOpts
38
39 // Specifies the backup ID from which to restore the database instance. There
40 // are some things to be aware of before using this field. When you execute
41 // the Restore Backup operation, a new database instance is created to store
42 // the backup whose ID is specified by the restorePoint attribute. This will
43 // mean that:
44 // - All users, passwords and access that were on the instance at the time of
45 // the backup will be restored along with the databases.
46 // - You can create new users or databases if you want, but they cannot be
47 // the same as the ones from the instance that was backed up.
48 RestorePoint string
49
50 ReplicaOf string
51}
52
53func (opts CreateOpts) ToInstanceCreateMap() (map[string]interface{}, error) {
54 instance, err := os.CreateOpts{
55 FlavorRef: opts.FlavorRef,
56 Size: opts.Size,
57 Name: opts.Name,
58 Databases: opts.Databases,
59 Users: opts.Users,
60 }.ToInstanceCreateMap()
61
62 if err != nil {
63 return nil, err
64 }
65
66 instance = instance["instance"].(map[string]interface{})
67
68 if opts.ConfigID != "" {
69 instance["configuration"] = opts.ConfigID
70 }
71
72 if opts.Datastore != nil {
73 ds, err := opts.Datastore.ToMap()
74 if err != nil {
75 return nil, err
76 }
77 instance["datastore"] = ds
78 }
79
80 if opts.RestorePoint != "" {
81 instance["restorePoint"] = map[string]string{"backupRef": opts.RestorePoint}
82 }
83
84 if opts.ReplicaOf != "" {
85 instance["replica_of"] = opts.ReplicaOf
86 }
87
88 return map[string]interface{}{"instance": instance}, nil
89}
90
91// Create asynchronously provisions a new database instance. It requires the
92// user to specify a flavor and a volume size. The API service then provisions
93// the instance with the requested flavor and sets up a volume of the specified
94// size, which is the storage for the database instance.
95//
96// Although this call only allows the creation of 1 instance per request, you
97// can create an instance with multiple databases and users. The default
98// binding for a MySQL instance is port 3306.
99func Create(client *gophercloud.ServiceClient, opts os.CreateOptsBuilder) CreateResult {
100 return CreateResult{os.Create(client, opts)}
101}
102
103// ListOpts specifies all of the query options to be used when returning a list
104// of database instances.
105type ListOpts struct {
106 // IncludeHA includes or excludes High Availability instances from the result set
107 IncludeHA bool `q:"include_ha"`
108
109 // IncludeReplicas includes or excludes Replica instances from the result set
110 IncludeReplicas bool `q:"include_replicas"`
111}
112
113// ToInstanceListQuery formats a ListOpts into a query string.
114func (opts ListOpts) ToInstanceListQuery() (string, error) {
115 q, err := gophercloud.BuildQueryString(opts)
116 if err != nil {
117 return "", err
118 }
119 return q.String(), nil
120}
121
122// List retrieves the status and information for all database instances.
123func List(client *gophercloud.ServiceClient, opts *ListOpts) pagination.Pager {
124 url := baseURL(client)
125
126 if opts != nil {
127 query, err := opts.ToInstanceListQuery()
128 if err != nil {
129 return pagination.Pager{Err: err}
130 }
131 url += query
132 }
133
134 createPageFn := func(r pagination.PageResult) pagination.Page {
135 return os.InstancePage{pagination.LinkedPageBase{PageResult: r}}
136 }
137
138 return pagination.NewPager(client, url, createPageFn)
139}
140
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100141// GetDefaultConfig lists the default configuration settings from the template
142// that was applied to the specified instance. In a sense, this is the vanilla
143// configuration setting applied to an instance. Further configuration can be
144// applied by associating an instance with a configuration group.
Jamie Hannaford936a5472015-02-10 14:38:28 +0100145func GetDefaultConfig(client *gophercloud.ServiceClient, id string) ConfigResult {
146 var res ConfigResult
147
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100148 _, res.Err = client.Request("GET", configURL(client, id), gophercloud.RequestOpts{
149 JSONResponse: &res.Body,
150 OkCodes: []int{200},
Jamie Hannaford936a5472015-02-10 14:38:28 +0100151 })
152
Jamie Hannaford936a5472015-02-10 14:38:28 +0100153 return res
154}
Jamie Hannafordf77fc102015-02-10 14:56:02 +0100155
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100156// AssociateWithConfigGroup associates a specified instance to a specified
157// configuration group. If any of the parameters within a configuration group
158// require a restart, then the instance will transition into a restart.
Jamie Hannafordf77fc102015-02-10 14:56:02 +0100159func AssociateWithConfigGroup(client *gophercloud.ServiceClient, instanceID, configGroupID string) UpdateResult {
160 reqBody := map[string]string{
161 "configuration": configGroupID,
162 }
163
164 var res UpdateResult
165
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100166 _, res.Err = client.Request("PUT", resourceURL(client, instanceID), gophercloud.RequestOpts{
167 JSONBody: map[string]map[string]string{"instance": reqBody},
168 OkCodes: []int{202},
Jamie Hannafordf77fc102015-02-10 14:56:02 +0100169 })
170
Jamie Hannafordf77fc102015-02-10 14:56:02 +0100171 return res
172}
Jamie Hannaford302c0b62015-02-16 14:12:34 +0100173
Jamie Hannaford99eced52015-03-02 15:24:22 +0100174// DetachFromConfigGroup will detach an instance from all config groups.
175func DetachFromConfigGroup(client *gophercloud.ServiceClient, instanceID string) UpdateResult {
176 return AssociateWithConfigGroup(client, instanceID, "")
177}
178
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100179// ListBackups will list all the backups for a specified database instance.
Jamie Hannaford302c0b62015-02-16 14:12:34 +0100180func ListBackups(client *gophercloud.ServiceClient, instanceID string) pagination.Pager {
Jamie Hannaford2e817322015-02-16 15:29:17 +0100181 pageFn := func(r pagination.PageResult) pagination.Page {
182 return backups.BackupPage{pagination.SinglePageBase(r)}
183 }
184 return pagination.NewPager(client, backupsURL(client, instanceID), pageFn)
Jamie Hannaford302c0b62015-02-16 14:12:34 +0100185}
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100186
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100187// DetachReplica will detach a specified replica instance from its source
188// instance, effectively allowing it to operate independently. Detaching a
189// replica will restart the MySQL service on the instance.
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100190func DetachReplica(client *gophercloud.ServiceClient, replicaID string) DetachResult {
191 var res DetachResult
192
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100193 _, res.Err = client.Request("PATCH", resourceURL(client, replicaID), gophercloud.RequestOpts{
194 JSONBody: map[string]interface{}{"instance": map[string]string{"replica_of": "", "slave_of": ""}},
195 OkCodes: []int{202},
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100196 })
197
198 return res
199}