blob: 072c141523a280584d4bf3c85dee2278dbe99bca [file] [log] [blame]
Jamie Hannaforded7f4532015-02-17 14:56:30 +01001package configurations
2
3import (
Krzysztof Szukiełojć3f41d082017-05-07 14:43:06 +02004 "gerrit.mcp.mirantis.net/debian/gophercloud.git"
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02005 "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/db/v1/instances"
6 "gerrit.mcp.mirantis.net/debian/gophercloud.git/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}})
jrperritt29ae6b32016-04-13 12:59:37 -050057 return
Jamie Hannaforded7f4532015-02-17 14:56:30 +010058}
59
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010060// Get will retrieve the details for a specified configuration group.
Jon Perritt3860b512016-03-29 12:01:48 -050061func Get(client *gophercloud.ServiceClient, configID string) (r GetResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060062 _, r.Err = client.Get(resourceURL(client, configID), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -050063 return
Jamie Hannaforded7f4532015-02-17 14:56:30 +010064}
65
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010066// UpdateOptsBuilder is the top-level interface for casting update options into
67// JSON maps.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010068type UpdateOptsBuilder interface {
69 ToConfigUpdateMap() (map[string]interface{}, error)
70}
71
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010072// UpdateOpts is the struct responsible for modifying existing configurations.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010073type UpdateOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -060074 // The configuration group name
75 Name string `json:"name,omitempty"`
76 // A map of user-defined configuration settings that will define
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010077 // how each associated datastore works. Each key/value pair is specific to a
78 // datastore type.
Jon Perrittdb0ae142016-03-13 00:33:41 -060079 Values map[string]interface{} `json:"values,omitempty"`
80 // Associates the configuration group with a particular datastore.
81 Datastore *DatastoreOpts `json:"datastore,omitempty"`
82 // A human-readable explanation for the group.
83 Description string `json:"description,omitempty"`
Jamie Hannaforded7f4532015-02-17 14:56:30 +010084}
85
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010086// ToConfigUpdateMap will cast an UpdateOpts struct into a JSON map.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010087func (opts UpdateOpts) ToConfigUpdateMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060088 return gophercloud.BuildRequestBody(opts, "configuration")
Jamie Hannaforded7f4532015-02-17 14:56:30 +010089}
90
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010091// Update will modify an existing configuration group by performing a merge
92// between new and existing values. If the key already exists, the new value
93// will overwrite. All other keys will remain unaffected.
Jon Perritt3860b512016-03-29 12:01:48 -050094func Update(client *gophercloud.ServiceClient, configID string, opts UpdateOptsBuilder) (r UpdateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060095 b, err := opts.ToConfigUpdateMap()
Jamie Hannaforded7f4532015-02-17 14:56:30 +010096 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060097 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050098 return
Jamie Hannaforded7f4532015-02-17 14:56:30 +010099 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600100 _, r.Err = client.Patch(resourceURL(client, configID), &b, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500101 return
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100102}
103
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100104// Replace will modify an existing configuration group by overwriting the
105// entire parameter group with the new values provided. Any existing keys not
106// included in UpdateOptsBuilder will be deleted.
Jon Perritt3860b512016-03-29 12:01:48 -0500107func Replace(client *gophercloud.ServiceClient, configID string, opts UpdateOptsBuilder) (r ReplaceResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600108 b, err := opts.ToConfigUpdateMap()
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100109 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600110 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500111 return
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100112 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600113 _, r.Err = client.Put(resourceURL(client, configID), &b, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500114 return
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100115}
116
Jamie Hannaford99eced52015-03-02 15:24:22 +0100117// Delete will permanently delete a configuration group. Please note that
118// config groups cannot be deleted whilst still attached to running instances -
119// you must detach and then delete them.
Jon Perritt3860b512016-03-29 12:01:48 -0500120func Delete(client *gophercloud.ServiceClient, configID string) (r DeleteResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600121 _, r.Err = client.Delete(resourceURL(client, configID), nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500122 return
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100123}
124
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100125// ListInstances will list all the instances associated with a particular
126// configuration group.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100127func ListInstances(client *gophercloud.ServiceClient, configID string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600128 return pagination.NewPager(client, instancesURL(client, configID), func(r pagination.PageResult) pagination.Page {
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100129 return instances.InstancePage{pagination.LinkedPageBase{PageResult: r}}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600130 })
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100131}
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100132
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100133// ListDatastoreParams will list all the available and supported parameters
134// that can be used for a particular datastore ID and a particular version.
135// For example, if you are wondering how you can configure a MySQL 5.6 instance,
136// you can use this operation (you will need to retrieve the MySQL datastore ID
137// by using the datastores API).
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100138func ListDatastoreParams(client *gophercloud.ServiceClient, datastoreID, versionID string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600139 return pagination.NewPager(client, listDSParamsURL(client, datastoreID, versionID), func(r pagination.PageResult) pagination.Page {
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100140 return ParamPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600141 })
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100142}
143
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100144// GetDatastoreParam will retrieve information about a specific configuration
145// parameter. For example, you can use this operation to understand more about
146// "innodb_file_per_table" configuration param for MySQL datastores. You will
147// need the param's ID first, which can be attained by using the ListDatastoreParams
148// operation.
Jon Perritt3860b512016-03-29 12:01:48 -0500149func GetDatastoreParam(client *gophercloud.ServiceClient, datastoreID, versionID, paramID string) (r ParamResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600150 _, r.Err = client.Get(getDSParamURL(client, datastoreID, versionID, paramID), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500151 return
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100152}
153
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100154// ListGlobalParams is similar to ListDatastoreParams but does not require a
155// DatastoreID.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100156func ListGlobalParams(client *gophercloud.ServiceClient, versionID string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600157 return pagination.NewPager(client, listGlobalParamsURL(client, versionID), func(r pagination.PageResult) pagination.Page {
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100158 return ParamPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600159 })
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100160}
161
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100162// GetGlobalParam is similar to GetDatastoreParam but does not require a
163// DatastoreID.
Jon Perritt3860b512016-03-29 12:01:48 -0500164func GetGlobalParam(client *gophercloud.ServiceClient, versionID, paramID string) (r ParamResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600165 _, r.Err = client.Get(getGlobalParamURL(client, versionID, paramID), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500166 return
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100167}