blob: 9e1bf059c9430ffd08d0cb54648b5e890c09b535 [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 {
Jon Perritt3860b512016-03-29 12:01:48 -050032 // The configuration group name
Jon Perrittdb0ae142016-03-13 00:33:41 -060033 Name string `json:"name" required:"true"`
Jon Perritt3860b512016-03-29 12:01:48 -050034 // A map of user-defined configuration settings that will define
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010035 // how each associated datastore works. Each key/value pair is specific to a
36 // datastore type.
Jon Perrittdb0ae142016-03-13 00:33:41 -060037 Values map[string]interface{} `json:"values" required:"true"`
Jon Perrittdb0ae142016-03-13 00:33:41 -060038 // Associates the configuration group with a particular datastore.
39 Datastore *DatastoreOpts `json:"datastore,omitempty"`
Jon Perrittdb0ae142016-03-13 00:33:41 -060040 // A human-readable explanation for the group.
41 Description string `json:"description,omitempty"`
Jamie Hannaforded7f4532015-02-17 14:56:30 +010042}
43
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010044// ToConfigCreateMap casts a CreateOpts struct into a JSON map.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010045func (opts CreateOpts) ToConfigCreateMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060046 return gophercloud.BuildRequestBody(opts, "configuration")
Jamie Hannaforded7f4532015-02-17 14:56:30 +010047}
48
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010049// Create will create a new configuration group.
Jon Perritt3860b512016-03-29 12:01:48 -050050func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060051 b, err := opts.ToConfigCreateMap()
Jamie Hannaforded7f4532015-02-17 14:56:30 +010052 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060053 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050054 return
Jamie Hannaforded7f4532015-02-17 14:56:30 +010055 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060056 _, r.Err = client.Post(baseURL(client), &b, &r.Body, &gophercloud.RequestOpts{OkCodes: []int{200}})
Jamie Hannaforded7f4532015-02-17 14:56:30 +010057}
58
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010059// Get will retrieve the details for a specified configuration group.
Jon Perritt3860b512016-03-29 12:01:48 -050060func Get(client *gophercloud.ServiceClient, configID string) (r GetResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060061 _, r.Err = client.Get(resourceURL(client, configID), &r.Body, nil)
Jamie Hannaforded7f4532015-02-17 14:56:30 +010062}
63
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010064// UpdateOptsBuilder is the top-level interface for casting update options into
65// JSON maps.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010066type UpdateOptsBuilder interface {
67 ToConfigUpdateMap() (map[string]interface{}, error)
68}
69
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010070// UpdateOpts is the struct responsible for modifying existing configurations.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010071type UpdateOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -060072 // The configuration group name
73 Name string `json:"name,omitempty"`
74 // A map of user-defined configuration settings that will define
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010075 // how each associated datastore works. Each key/value pair is specific to a
76 // datastore type.
Jon Perrittdb0ae142016-03-13 00:33:41 -060077 Values map[string]interface{} `json:"values,omitempty"`
78 // Associates the configuration group with a particular datastore.
79 Datastore *DatastoreOpts `json:"datastore,omitempty"`
80 // A human-readable explanation for the group.
81 Description string `json:"description,omitempty"`
Jamie Hannaforded7f4532015-02-17 14:56:30 +010082}
83
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010084// ToConfigUpdateMap will cast an UpdateOpts struct into a JSON map.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010085func (opts UpdateOpts) ToConfigUpdateMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060086 return gophercloud.BuildRequestBody(opts, "configuration")
Jamie Hannaforded7f4532015-02-17 14:56:30 +010087}
88
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010089// Update will modify an existing configuration group by performing a merge
90// between new and existing values. If the key already exists, the new value
91// will overwrite. All other keys will remain unaffected.
Jon Perritt3860b512016-03-29 12:01:48 -050092func Update(client *gophercloud.ServiceClient, configID string, opts UpdateOptsBuilder) (r UpdateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060093 b, err := opts.ToConfigUpdateMap()
Jamie Hannaforded7f4532015-02-17 14:56:30 +010094 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060095 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050096 return
Jamie Hannaforded7f4532015-02-17 14:56:30 +010097 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060098 _, r.Err = client.Patch(resourceURL(client, configID), &b, nil, nil)
Jamie Hannaforded7f4532015-02-17 14:56:30 +010099}
100
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100101// Replace will modify an existing configuration group by overwriting the
102// entire parameter group with the new values provided. Any existing keys not
103// included in UpdateOptsBuilder will be deleted.
Jon Perritt3860b512016-03-29 12:01:48 -0500104func Replace(client *gophercloud.ServiceClient, configID string, opts UpdateOptsBuilder) (r ReplaceResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600105 b, err := opts.ToConfigUpdateMap()
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100106 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600107 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500108 return
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100109 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600110 _, r.Err = client.Put(resourceURL(client, configID), &b, nil, nil)
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100111}
112
Jamie Hannaford99eced52015-03-02 15:24:22 +0100113// Delete will permanently delete a configuration group. Please note that
114// config groups cannot be deleted whilst still attached to running instances -
115// you must detach and then delete them.
Jon Perritt3860b512016-03-29 12:01:48 -0500116func Delete(client *gophercloud.ServiceClient, configID string) (r DeleteResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600117 _, r.Err = client.Delete(resourceURL(client, configID), nil)
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100118}
119
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100120// ListInstances will list all the instances associated with a particular
121// configuration group.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100122func ListInstances(client *gophercloud.ServiceClient, configID string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600123 return pagination.NewPager(client, instancesURL(client, configID), func(r pagination.PageResult) pagination.Page {
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100124 return instances.InstancePage{pagination.LinkedPageBase{PageResult: r}}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600125 })
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100126}
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100127
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100128// ListDatastoreParams will list all the available and supported parameters
129// that can be used for a particular datastore ID and a particular version.
130// For example, if you are wondering how you can configure a MySQL 5.6 instance,
131// you can use this operation (you will need to retrieve the MySQL datastore ID
132// by using the datastores API).
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100133func ListDatastoreParams(client *gophercloud.ServiceClient, datastoreID, versionID string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600134 return pagination.NewPager(client, listDSParamsURL(client, datastoreID, versionID), func(r pagination.PageResult) pagination.Page {
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100135 return ParamPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600136 })
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100137}
138
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100139// GetDatastoreParam will retrieve information about a specific configuration
140// parameter. For example, you can use this operation to understand more about
141// "innodb_file_per_table" configuration param for MySQL datastores. You will
142// need the param's ID first, which can be attained by using the ListDatastoreParams
143// operation.
Jon Perritt3860b512016-03-29 12:01:48 -0500144func GetDatastoreParam(client *gophercloud.ServiceClient, datastoreID, versionID, paramID string) (r ParamResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600145 _, r.Err = client.Get(getDSParamURL(client, datastoreID, versionID, paramID), &r.Body, nil)
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100146}
147
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100148// ListGlobalParams is similar to ListDatastoreParams but does not require a
149// DatastoreID.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100150func ListGlobalParams(client *gophercloud.ServiceClient, versionID string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600151 return pagination.NewPager(client, listGlobalParamsURL(client, versionID), func(r pagination.PageResult) pagination.Page {
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100152 return ParamPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600153 })
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100154}
155
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100156// GetGlobalParam is similar to GetDatastoreParam but does not require a
157// DatastoreID.
Jon Perritt3860b512016-03-29 12:01:48 -0500158func GetGlobalParam(client *gophercloud.ServiceClient, versionID, paramID string) (r ParamResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600159 _, r.Err = client.Get(getGlobalParamURL(client, versionID, paramID), &r.Body, nil)
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100160}