1package configurations
2
3import (
4	"github.com/gophercloud/gophercloud"
5	"github.com/gophercloud/gophercloud/openstack/db/v1/instances"
6	"github.com/gophercloud/gophercloud/pagination"
7)
8
9// List will list all of the available configurations.
10func List(client *gophercloud.ServiceClient) pagination.Pager {
11	return pagination.NewPager(client, baseURL(client), func(r pagination.PageResult) pagination.Page {
12		return ConfigPage{pagination.SinglePageBase(r)}
13	})
14}
15
16// CreateOptsBuilder is a top-level interface which renders a JSON map.
17type CreateOptsBuilder interface {
18	ToConfigCreateMap() (map[string]interface{}, error)
19}
20
21// DatastoreOpts is the primary options struct for creating and modifying
22// how configuration resources are associated with datastores.
23type DatastoreOpts struct {
24	// 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"`
28}
29
30// CreateOpts is the struct responsible for configuring new configurations.
31type CreateOpts struct {
32	// The configuration group name
33	Name string `json:"name" required:"true"`
34	// A map of user-defined configuration settings that will define
35	// how each associated datastore works. Each key/value pair is specific to a
36	// datastore type.
37	Values map[string]interface{} `json:"values" required:"true"`
38	// Associates the configuration group with a particular datastore.
39	Datastore *DatastoreOpts `json:"datastore,omitempty"`
40	// A human-readable explanation for the group.
41	Description string `json:"description,omitempty"`
42}
43
44// ToConfigCreateMap casts a CreateOpts struct into a JSON map.
45func (opts CreateOpts) ToConfigCreateMap() (map[string]interface{}, error) {
46	return gophercloud.BuildRequestBody(opts, "configuration")
47}
48
49// Create will create a new configuration group.
50func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
51	b, err := opts.ToConfigCreateMap()
52	if err != nil {
53		r.Err = err
54		return
55	}
56	_, r.Err = client.Post(baseURL(client), &b, &r.Body, &gophercloud.RequestOpts{OkCodes: []int{200}})
57	return
58}
59
60// Get will retrieve the details for a specified configuration group.
61func Get(client *gophercloud.ServiceClient, configID string) (r GetResult) {
62	_, r.Err = client.Get(resourceURL(client, configID), &r.Body, nil)
63	return
64}
65
66// UpdateOptsBuilder is the top-level interface for casting update options into
67// JSON maps.
68type UpdateOptsBuilder interface {
69	ToConfigUpdateMap() (map[string]interface{}, error)
70}
71
72// UpdateOpts is the struct responsible for modifying existing configurations.
73type UpdateOpts struct {
74	// The configuration group name
75	Name string `json:"name,omitempty"`
76	// A map of user-defined configuration settings that will define
77	// how each associated datastore works. Each key/value pair is specific to a
78	// datastore type.
79	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"`
84}
85
86// ToConfigUpdateMap will cast an UpdateOpts struct into a JSON map.
87func (opts UpdateOpts) ToConfigUpdateMap() (map[string]interface{}, error) {
88	return gophercloud.BuildRequestBody(opts, "configuration")
89}
90
91// 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.
94func Update(client *gophercloud.ServiceClient, configID string, opts UpdateOptsBuilder) (r UpdateResult) {
95	b, err := opts.ToConfigUpdateMap()
96	if err != nil {
97		r.Err = err
98		return
99	}
100	_, r.Err = client.Patch(resourceURL(client, configID), &b, nil, nil)
101	return
102}
103
104// 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.
107func Replace(client *gophercloud.ServiceClient, configID string, opts UpdateOptsBuilder) (r ReplaceResult) {
108	b, err := opts.ToConfigUpdateMap()
109	if err != nil {
110		r.Err = err
111		return
112	}
113	_, r.Err = client.Put(resourceURL(client, configID), &b, nil, nil)
114	return
115}
116
117// 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.
120func Delete(client *gophercloud.ServiceClient, configID string) (r DeleteResult) {
121	_, r.Err = client.Delete(resourceURL(client, configID), nil)
122	return
123}
124
125// ListInstances will list all the instances associated with a particular
126// configuration group.
127func ListInstances(client *gophercloud.ServiceClient, configID string) pagination.Pager {
128	return pagination.NewPager(client, instancesURL(client, configID), func(r pagination.PageResult) pagination.Page {
129		return instances.InstancePage{LinkedPageBase: pagination.LinkedPageBase{PageResult: r}}
130	})
131}
132
133// 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).
138func ListDatastoreParams(client *gophercloud.ServiceClient, datastoreID, versionID string) pagination.Pager {
139	return pagination.NewPager(client, listDSParamsURL(client, datastoreID, versionID), func(r pagination.PageResult) pagination.Page {
140		return ParamPage{pagination.SinglePageBase(r)}
141	})
142}
143
144// 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.
149func GetDatastoreParam(client *gophercloud.ServiceClient, datastoreID, versionID, paramID string) (r ParamResult) {
150	_, r.Err = client.Get(getDSParamURL(client, datastoreID, versionID, paramID), &r.Body, nil)
151	return
152}
153
154// ListGlobalParams is similar to ListDatastoreParams but does not require a
155// DatastoreID.
156func ListGlobalParams(client *gophercloud.ServiceClient, versionID string) pagination.Pager {
157	return pagination.NewPager(client, listGlobalParamsURL(client, versionID), func(r pagination.PageResult) pagination.Page {
158		return ParamPage{pagination.SinglePageBase(r)}
159	})
160}
161
162// GetGlobalParam is similar to GetDatastoreParam but does not require a
163// DatastoreID.
164func GetGlobalParam(client *gophercloud.ServiceClient, versionID, paramID string) (r ParamResult) {
165	_, r.Err = client.Get(getGlobalParamURL(client, versionID, paramID), &r.Body, nil)
166	return
167}
168