blob: abb001341d99eddb6a1296f599e6b2a0890f0d3e [file] [log] [blame]
Jamie Hannaforded7f4532015-02-17 14:56:30 +01001package configurations
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/openstack/db/v1/instances"
6 "github.com/gophercloud/gophercloud/pagination"
Jamie Hannaforded7f4532015-02-17 14:56:30 +01007)
8
Jamie Hannafordb0d267b2015-02-19 11:59:53 +01009// List will list all of the available configurations.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010010func List(client *gophercloud.ServiceClient) pagination.Pager {
11 pageFn := func(r pagination.PageResult) pagination.Page {
12 return ConfigPage{pagination.SinglePageBase(r)}
13 }
14
15 return pagination.NewPager(client, baseURL(client), pageFn)
16}
17
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010018// CreateOptsBuilder is a top-level interface which renders a JSON map.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010019type CreateOptsBuilder interface {
20 ToConfigCreateMap() (map[string]interface{}, error)
21}
22
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010023// DatastoreOpts is the primary options struct for creating and modifying
24// how configuration resources are associated with datastores.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010025type DatastoreOpts struct {
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010026 // [OPTIONAL] The type of datastore. Defaults to "MySQL".
27 Type string
28
29 // [OPTIONAL] The specific version of a datastore. Defaults to "5.6".
Jamie Hannaforded7f4532015-02-17 14:56:30 +010030 Version string
31}
32
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010033// ToMap renders a JSON map for a datastore setting.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010034func (opts DatastoreOpts) ToMap() (map[string]string, error) {
35 datastore := map[string]string{}
36
37 if opts.Type != "" {
38 datastore["type"] = opts.Type
39 }
40
41 if opts.Version != "" {
42 datastore["version"] = opts.Version
43 }
44
45 return datastore, nil
46}
47
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010048// CreateOpts is the struct responsible for configuring new configurations.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010049type CreateOpts struct {
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010050 // [REQUIRED] The configuration group name
51 Name string
52
53 // [REQUIRED] A map of user-defined configuration settings that will define
54 // how each associated datastore works. Each key/value pair is specific to a
55 // datastore type.
56 Values map[string]interface{}
57
58 // [OPTIONAL] Associates the configuration group with a particular datastore.
59 Datastore *DatastoreOpts
60
61 // [OPTIONAL] A human-readable explanation for the group.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010062 Description string
Jamie Hannaforded7f4532015-02-17 14:56:30 +010063}
64
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010065// ToConfigCreateMap casts a CreateOpts struct into a JSON map.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010066func (opts CreateOpts) ToConfigCreateMap() (map[string]interface{}, error) {
67 if opts.Name == "" {
Jon Perritt763e5922016-03-07 03:21:18 -060068 err := gophercloud.ErrMissingInput{}
69 err.Function = "configurations.ToConfigCreateMap"
70 err.Argument = "configurations.CreateOpts.Name"
71 return nil, err
Jamie Hannaforded7f4532015-02-17 14:56:30 +010072 }
73 if len(opts.Values) == 0 {
Jon Perritt763e5922016-03-07 03:21:18 -060074 err := gophercloud.ErrMissingInput{}
75 err.Function = "configurations.ToConfigCreateMap"
76 err.Argument = "configurations.CreateOpts.Values"
77 return nil, err
Jamie Hannaforded7f4532015-02-17 14:56:30 +010078 }
79
80 config := map[string]interface{}{
81 "name": opts.Name,
82 "values": opts.Values,
83 }
84
85 if opts.Datastore != nil {
86 ds, err := opts.Datastore.ToMap()
87 if err != nil {
88 return config, err
89 }
90 config["datastore"] = ds
91 }
92
93 if opts.Description != "" {
94 config["description"] = opts.Description
95 }
96
97 return map[string]interface{}{"configuration": config}, nil
98}
99
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100100// Create will create a new configuration group.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100101func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
102 var res CreateResult
103
104 reqBody, err := opts.ToConfigCreateMap()
105 if err != nil {
106 res.Err = err
107 return res
108 }
109
Jon Perritta33da232016-03-02 04:43:08 -0600110 _, res.Err = client.Request("POST", baseURL(client), &gophercloud.RequestOpts{
Jamie Hannaford99eced52015-03-02 15:24:22 +0100111 OkCodes: []int{200},
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100112 JSONBody: &reqBody,
113 JSONResponse: &res.Body,
114 })
115
116 return res
117}
118
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100119// Get will retrieve the details for a specified configuration group.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100120func Get(client *gophercloud.ServiceClient, configID string) GetResult {
121 var res GetResult
122
Jon Perritta33da232016-03-02 04:43:08 -0600123 _, res.Err = client.Request("GET", resourceURL(client, configID), &gophercloud.RequestOpts{
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100124 OkCodes: []int{200},
125 JSONResponse: &res.Body,
126 })
127
128 return res
129}
130
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100131// UpdateOptsBuilder is the top-level interface for casting update options into
132// JSON maps.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100133type UpdateOptsBuilder interface {
134 ToConfigUpdateMap() (map[string]interface{}, error)
135}
136
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100137// UpdateOpts is the struct responsible for modifying existing configurations.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100138type UpdateOpts struct {
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100139 // [OPTIONAL] The configuration group name
140 Name string
141
142 // [OPTIONAL] A map of user-defined configuration settings that will define
143 // how each associated datastore works. Each key/value pair is specific to a
144 // datastore type.
145 Values map[string]interface{}
146
147 // [OPTIONAL] Associates the configuration group with a particular datastore.
148 Datastore *DatastoreOpts
149
150 // [OPTIONAL] A human-readable explanation for the group.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100151 Description string
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100152}
153
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100154// ToConfigUpdateMap will cast an UpdateOpts struct into a JSON map.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100155func (opts UpdateOpts) ToConfigUpdateMap() (map[string]interface{}, error) {
156 config := map[string]interface{}{}
157
158 if opts.Name != "" {
159 config["name"] = opts.Name
160 }
161
162 if opts.Description != "" {
163 config["description"] = opts.Description
164 }
165
166 if opts.Datastore != nil {
167 ds, err := opts.Datastore.ToMap()
168 if err != nil {
169 return config, err
170 }
171 config["datastore"] = ds
172 }
173
174 if len(opts.Values) > 0 {
175 config["values"] = opts.Values
176 }
177
178 return map[string]interface{}{"configuration": config}, nil
179}
180
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100181// Update will modify an existing configuration group by performing a merge
182// between new and existing values. If the key already exists, the new value
183// will overwrite. All other keys will remain unaffected.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100184func Update(client *gophercloud.ServiceClient, configID string, opts UpdateOptsBuilder) UpdateResult {
185 var res UpdateResult
186
187 reqBody, err := opts.ToConfigUpdateMap()
188 if err != nil {
189 res.Err = err
190 return res
191 }
192
Jon Perritta33da232016-03-02 04:43:08 -0600193 _, res.Err = client.Request("PATCH", resourceURL(client, configID), &gophercloud.RequestOpts{
Jamie Hannaford52dbcee2015-10-06 16:09:56 +0200194 OkCodes: []int{200},
195 JSONBody: &reqBody,
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100196 })
197
198 return res
199}
200
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100201// Replace will modify an existing configuration group by overwriting the
202// entire parameter group with the new values provided. Any existing keys not
203// included in UpdateOptsBuilder will be deleted.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100204func Replace(client *gophercloud.ServiceClient, configID string, opts UpdateOptsBuilder) ReplaceResult {
205 var res ReplaceResult
206
207 reqBody, err := opts.ToConfigUpdateMap()
208 if err != nil {
209 res.Err = err
210 return res
211 }
212
Jon Perritta33da232016-03-02 04:43:08 -0600213 _, res.Err = client.Request("PUT", resourceURL(client, configID), &gophercloud.RequestOpts{
Jamie Hannaford52dbcee2015-10-06 16:09:56 +0200214 OkCodes: []int{202},
215 JSONBody: &reqBody,
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100216 })
217
218 return res
219}
220
Jamie Hannaford99eced52015-03-02 15:24:22 +0100221// Delete will permanently delete a configuration group. Please note that
222// config groups cannot be deleted whilst still attached to running instances -
223// you must detach and then delete them.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100224func Delete(client *gophercloud.ServiceClient, configID string) DeleteResult {
225 var res DeleteResult
226
Jon Perritta33da232016-03-02 04:43:08 -0600227 _, res.Err = client.Request("DELETE", resourceURL(client, configID), &gophercloud.RequestOpts{
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100228 OkCodes: []int{202},
229 })
230
231 return res
232}
233
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100234// ListInstances will list all the instances associated with a particular
235// configuration group.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100236func ListInstances(client *gophercloud.ServiceClient, configID string) pagination.Pager {
237 pageFn := func(r pagination.PageResult) pagination.Page {
238 return instances.InstancePage{pagination.LinkedPageBase{PageResult: r}}
239 }
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100240 return pagination.NewPager(client, instancesURL(client, configID), pageFn)
241}
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100242
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100243// ListDatastoreParams will list all the available and supported parameters
244// that can be used for a particular datastore ID and a particular version.
245// For example, if you are wondering how you can configure a MySQL 5.6 instance,
246// you can use this operation (you will need to retrieve the MySQL datastore ID
247// by using the datastores API).
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100248func ListDatastoreParams(client *gophercloud.ServiceClient, datastoreID, versionID string) pagination.Pager {
249 pageFn := func(r pagination.PageResult) pagination.Page {
250 return ParamPage{pagination.SinglePageBase(r)}
251 }
252 return pagination.NewPager(client, listDSParamsURL(client, datastoreID, versionID), pageFn)
253}
254
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100255// GetDatastoreParam will retrieve information about a specific configuration
256// parameter. For example, you can use this operation to understand more about
257// "innodb_file_per_table" configuration param for MySQL datastores. You will
258// need the param's ID first, which can be attained by using the ListDatastoreParams
259// operation.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100260func GetDatastoreParam(client *gophercloud.ServiceClient, datastoreID, versionID, paramID string) ParamResult {
261 var res ParamResult
262
Jon Perritta33da232016-03-02 04:43:08 -0600263 _, res.Err = client.Request("GET", getDSParamURL(client, datastoreID, versionID, paramID), &gophercloud.RequestOpts{
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100264 OkCodes: []int{200},
265 JSONResponse: &res.Body,
266 })
267
268 return res
269}
270
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100271// ListGlobalParams is similar to ListDatastoreParams but does not require a
272// DatastoreID.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100273func ListGlobalParams(client *gophercloud.ServiceClient, versionID string) pagination.Pager {
274 pageFn := func(r pagination.PageResult) pagination.Page {
275 return ParamPage{pagination.SinglePageBase(r)}
276 }
277 return pagination.NewPager(client, listGlobalParamsURL(client, versionID), pageFn)
278}
279
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100280// GetGlobalParam is similar to GetDatastoreParam but does not require a
281// DatastoreID.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100282func GetGlobalParam(client *gophercloud.ServiceClient, versionID, paramID string) ParamResult {
283 var res ParamResult
284
Jon Perritta33da232016-03-02 04:43:08 -0600285 _, res.Err = client.Request("GET", getGlobalParamURL(client, versionID, paramID), &gophercloud.RequestOpts{
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100286 OkCodes: []int{200},
287 JSONResponse: &res.Body,
288 })
289
290 return res
291}