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