blob: eb59cc248fbd2b05f7214215e9e3b507aa0790c1 [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 {
Jon Perrittdb0ae142016-03-13 00:33:41 -060011 return pagination.NewPager(client, baseURL(client), func(r pagination.PageResult) pagination.Page {
Jamie Hannaforded7f4532015-02-17 14:56:30 +010012 return ConfigPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -060013 })
Jamie Hannaforded7f4532015-02-17 14:56:30 +010014}
15
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010016// CreateOptsBuilder is a top-level interface which renders a JSON map.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010017type CreateOptsBuilder interface {
18 ToConfigCreateMap() (map[string]interface{}, error)
19}
20
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010021// DatastoreOpts is the primary options struct for creating and modifying
22// how configuration resources are associated with datastores.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010023type DatastoreOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -060024 // The type of datastore. Defaults to "MySQL".
25 Type string `json:"type,omitempty"`
26 // The specific version of a datastore. Defaults to "5.6".
27 Version string `json:"version,omitempty"`
Jamie Hannaforded7f4532015-02-17 14:56:30 +010028}
29
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010030// CreateOpts is the struct responsible for configuring new configurations.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010031type CreateOpts struct {
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010032 // [REQUIRED] The configuration group name
Jon Perrittdb0ae142016-03-13 00:33:41 -060033 Name string `json:"name" required:"true"`
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010034
35 // [REQUIRED] A map of user-defined configuration settings that will define
36 // how each associated datastore works. Each key/value pair is specific to a
37 // datastore type.
Jon Perrittdb0ae142016-03-13 00:33:41 -060038 Values map[string]interface{} `json:"values" required:"true"`
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010039
Jon Perrittdb0ae142016-03-13 00:33:41 -060040 // Associates the configuration group with a particular datastore.
41 Datastore *DatastoreOpts `json:"datastore,omitempty"`
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010042
Jon Perrittdb0ae142016-03-13 00:33:41 -060043 // A human-readable explanation for the group.
44 Description string `json:"description,omitempty"`
Jamie Hannaforded7f4532015-02-17 14:56:30 +010045}
46
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010047// ToConfigCreateMap casts a CreateOpts struct into a JSON map.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010048func (opts CreateOpts) ToConfigCreateMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060049 return gophercloud.BuildRequestBody(opts, "configuration")
Jamie Hannaforded7f4532015-02-17 14:56:30 +010050}
51
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010052// Create will create a new configuration group.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010053func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060054 var r CreateResult
55 b, err := opts.ToConfigCreateMap()
Jamie Hannaforded7f4532015-02-17 14:56:30 +010056 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060057 r.Err = err
58 return r
Jamie Hannaforded7f4532015-02-17 14:56:30 +010059 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060060 _, r.Err = client.Post(baseURL(client), &b, &r.Body, &gophercloud.RequestOpts{OkCodes: []int{200}})
61 return r
Jamie Hannaforded7f4532015-02-17 14:56:30 +010062}
63
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010064// Get will retrieve the details for a specified configuration group.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010065func Get(client *gophercloud.ServiceClient, configID string) GetResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060066 var r GetResult
67 _, r.Err = client.Get(resourceURL(client, configID), &r.Body, nil)
68 return r
Jamie Hannaforded7f4532015-02-17 14:56:30 +010069}
70
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010071// UpdateOptsBuilder is the top-level interface for casting update options into
72// JSON maps.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010073type UpdateOptsBuilder interface {
74 ToConfigUpdateMap() (map[string]interface{}, error)
75}
76
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010077// UpdateOpts is the struct responsible for modifying existing configurations.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010078type UpdateOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -060079 // The configuration group name
80 Name string `json:"name,omitempty"`
81 // A map of user-defined configuration settings that will define
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010082 // how each associated datastore works. Each key/value pair is specific to a
83 // datastore type.
Jon Perrittdb0ae142016-03-13 00:33:41 -060084 Values map[string]interface{} `json:"values,omitempty"`
85 // Associates the configuration group with a particular datastore.
86 Datastore *DatastoreOpts `json:"datastore,omitempty"`
87 // A human-readable explanation for the group.
88 Description string `json:"description,omitempty"`
Jamie Hannaforded7f4532015-02-17 14:56:30 +010089}
90
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010091// ToConfigUpdateMap will cast an UpdateOpts struct into a JSON map.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010092func (opts UpdateOpts) ToConfigUpdateMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060093 return gophercloud.BuildRequestBody(opts, "configuration")
Jamie Hannaforded7f4532015-02-17 14:56:30 +010094}
95
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010096// Update will modify an existing configuration group by performing a merge
97// between new and existing values. If the key already exists, the new value
98// will overwrite. All other keys will remain unaffected.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010099func Update(client *gophercloud.ServiceClient, configID string, opts UpdateOptsBuilder) UpdateResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600100 var r UpdateResult
101 b, err := opts.ToConfigUpdateMap()
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100102 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600103 r.Err = err
104 return r
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100105 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600106 _, r.Err = client.Patch(resourceURL(client, configID), &b, nil, nil)
107 return r
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100108}
109
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100110// Replace will modify an existing configuration group by overwriting the
111// entire parameter group with the new values provided. Any existing keys not
112// included in UpdateOptsBuilder will be deleted.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100113func Replace(client *gophercloud.ServiceClient, configID string, opts UpdateOptsBuilder) ReplaceResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600114 var r ReplaceResult
115 b, err := opts.ToConfigUpdateMap()
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100116 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600117 r.Err = err
118 return r
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100119 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600120 _, r.Err = client.Put(resourceURL(client, configID), &b, nil, nil)
121 return r
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100122}
123
Jamie Hannaford99eced52015-03-02 15:24:22 +0100124// Delete will permanently delete a configuration group. Please note that
125// config groups cannot be deleted whilst still attached to running instances -
126// you must detach and then delete them.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100127func Delete(client *gophercloud.ServiceClient, configID string) DeleteResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600128 var r DeleteResult
129 _, r.Err = client.Delete(resourceURL(client, configID), nil)
130 return r
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100131}
132
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100133// ListInstances will list all the instances associated with a particular
134// configuration group.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100135func ListInstances(client *gophercloud.ServiceClient, configID string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600136 return pagination.NewPager(client, instancesURL(client, configID), func(r pagination.PageResult) pagination.Page {
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100137 return instances.InstancePage{pagination.LinkedPageBase{PageResult: r}}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600138 })
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100139}
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100140
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100141// ListDatastoreParams will list all the available and supported parameters
142// that can be used for a particular datastore ID and a particular version.
143// For example, if you are wondering how you can configure a MySQL 5.6 instance,
144// you can use this operation (you will need to retrieve the MySQL datastore ID
145// by using the datastores API).
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100146func ListDatastoreParams(client *gophercloud.ServiceClient, datastoreID, versionID string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600147 return pagination.NewPager(client, listDSParamsURL(client, datastoreID, versionID), func(r pagination.PageResult) pagination.Page {
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100148 return ParamPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600149 })
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100150}
151
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100152// GetDatastoreParam will retrieve information about a specific configuration
153// parameter. For example, you can use this operation to understand more about
154// "innodb_file_per_table" configuration param for MySQL datastores. You will
155// need the param's ID first, which can be attained by using the ListDatastoreParams
156// operation.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100157func GetDatastoreParam(client *gophercloud.ServiceClient, datastoreID, versionID, paramID string) ParamResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600158 var r ParamResult
159 _, r.Err = client.Get(getDSParamURL(client, datastoreID, versionID, paramID), &r.Body, nil)
160 return r
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100161}
162
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100163// ListGlobalParams is similar to ListDatastoreParams but does not require a
164// DatastoreID.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100165func ListGlobalParams(client *gophercloud.ServiceClient, versionID string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600166 return pagination.NewPager(client, listGlobalParamsURL(client, versionID), func(r pagination.PageResult) pagination.Page {
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100167 return ParamPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600168 })
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100169}
170
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100171// GetGlobalParam is similar to GetDatastoreParam but does not require a
172// DatastoreID.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100173func GetGlobalParam(client *gophercloud.ServiceClient, versionID, paramID string) ParamResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600174 var r ParamResult
175 _, r.Err = client.Get(getGlobalParamURL(client, versionID, paramID), &r.Body, nil)
176 return r
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100177}