1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
package configurations
import (
"errors"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack/db/v1/instances"
"github.com/rackspace/gophercloud/pagination"
)
// List will list all of the available configurations.
func List(client *gophercloud.ServiceClient) pagination.Pager {
pageFn := func(r pagination.PageResult) pagination.Page {
return ConfigPage{pagination.SinglePageBase(r)}
}
return pagination.NewPager(client, baseURL(client), pageFn)
}
// CreateOptsBuilder is a top-level interface which renders a JSON map.
type CreateOptsBuilder interface {
ToConfigCreateMap() (map[string]interface{}, error)
}
// DatastoreOpts is the primary options struct for creating and modifying
// how configuration resources are associated with datastores.
type DatastoreOpts struct {
// [OPTIONAL] The type of datastore. Defaults to "MySQL".
Type string
// [OPTIONAL] The specific version of a datastore. Defaults to "5.6".
Version string
}
// ToMap renders a JSON map for a datastore setting.
func (opts DatastoreOpts) ToMap() (map[string]string, error) {
datastore := map[string]string{}
if opts.Type != "" {
datastore["type"] = opts.Type
}
if opts.Version != "" {
datastore["version"] = opts.Version
}
return datastore, nil
}
// CreateOpts is the struct responsible for configuring new configurations.
type CreateOpts struct {
// [REQUIRED] The configuration group name
Name string
// [REQUIRED] A map of user-defined configuration settings that will define
// how each associated datastore works. Each key/value pair is specific to a
// datastore type.
Values map[string]interface{}
// [OPTIONAL] Associates the configuration group with a particular datastore.
Datastore *DatastoreOpts
// [OPTIONAL] A human-readable explanation for the group.
Description string
}
// ToConfigCreateMap casts a CreateOpts struct into a JSON map.
func (opts CreateOpts) ToConfigCreateMap() (map[string]interface{}, error) {
if opts.Name == "" {
return nil, errors.New("Name is a required field")
}
if len(opts.Values) == 0 {
return nil, errors.New("Values must be a populated map")
}
config := map[string]interface{}{
"name": opts.Name,
"values": opts.Values,
}
if opts.Datastore != nil {
ds, err := opts.Datastore.ToMap()
if err != nil {
return config, err
}
config["datastore"] = ds
}
if opts.Description != "" {
config["description"] = opts.Description
}
return map[string]interface{}{"configuration": config}, nil
}
// Create will create a new configuration group.
func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
var res CreateResult
reqBody, err := opts.ToConfigCreateMap()
if err != nil {
res.Err = err
return res
}
_, res.Err = client.Request("POST", baseURL(client), gophercloud.RequestOpts{
OkCodes: []int{200},
JSONBody: &reqBody,
JSONResponse: &res.Body,
})
return res
}
// Get will retrieve the details for a specified configuration group.
func Get(client *gophercloud.ServiceClient, configID string) GetResult {
var res GetResult
_, res.Err = client.Request("GET", resourceURL(client, configID), gophercloud.RequestOpts{
OkCodes: []int{200},
JSONResponse: &res.Body,
})
return res
}
// UpdateOptsBuilder is the top-level interface for casting update options into
// JSON maps.
type UpdateOptsBuilder interface {
ToConfigUpdateMap() (map[string]interface{}, error)
}
// UpdateOpts is the struct responsible for modifying existing configurations.
type UpdateOpts struct {
// [OPTIONAL] The configuration group name
Name string
// [OPTIONAL] A map of user-defined configuration settings that will define
// how each associated datastore works. Each key/value pair is specific to a
// datastore type.
Values map[string]interface{}
// [OPTIONAL] Associates the configuration group with a particular datastore.
Datastore *DatastoreOpts
// [OPTIONAL] A human-readable explanation for the group.
Description string
}
// ToConfigUpdateMap will cast an UpdateOpts struct into a JSON map.
func (opts UpdateOpts) ToConfigUpdateMap() (map[string]interface{}, error) {
config := map[string]interface{}{}
if opts.Name != "" {
config["name"] = opts.Name
}
if opts.Description != "" {
config["description"] = opts.Description
}
if opts.Datastore != nil {
ds, err := opts.Datastore.ToMap()
if err != nil {
return config, err
}
config["datastore"] = ds
}
if len(opts.Values) > 0 {
config["values"] = opts.Values
}
return map[string]interface{}{"configuration": config}, nil
}
// Update will modify an existing configuration group by performing a merge
// between new and existing values. If the key already exists, the new value
// will overwrite. All other keys will remain unaffected.
func Update(client *gophercloud.ServiceClient, configID string, opts UpdateOptsBuilder) UpdateResult {
var res UpdateResult
reqBody, err := opts.ToConfigUpdateMap()
if err != nil {
res.Err = err
return res
}
_, res.Err = client.Request("PATCH", resourceURL(client, configID), gophercloud.RequestOpts{
OkCodes: []int{200},
JSONBody: &reqBody,
})
return res
}
// Replace will modify an existing configuration group by overwriting the
// entire parameter group with the new values provided. Any existing keys not
// included in UpdateOptsBuilder will be deleted.
func Replace(client *gophercloud.ServiceClient, configID string, opts UpdateOptsBuilder) ReplaceResult {
var res ReplaceResult
reqBody, err := opts.ToConfigUpdateMap()
if err != nil {
res.Err = err
return res
}
_, res.Err = client.Request("PUT", resourceURL(client, configID), gophercloud.RequestOpts{
OkCodes: []int{202},
JSONBody: &reqBody,
})
return res
}
// Delete will permanently delete a configuration group. Please note that
// config groups cannot be deleted whilst still attached to running instances -
// you must detach and then delete them.
func Delete(client *gophercloud.ServiceClient, configID string) DeleteResult {
var res DeleteResult
_, res.Err = client.Request("DELETE", resourceURL(client, configID), gophercloud.RequestOpts{
OkCodes: []int{202},
})
return res
}
// ListInstances will list all the instances associated with a particular
// configuration group.
func ListInstances(client *gophercloud.ServiceClient, configID string) pagination.Pager {
pageFn := func(r pagination.PageResult) pagination.Page {
return instances.InstancePage{pagination.LinkedPageBase{PageResult: r}}
}
return pagination.NewPager(client, instancesURL(client, configID), pageFn)
}
// ListDatastoreParams will list all the available and supported parameters
// that can be used for a particular datastore ID and a particular version.
// For example, if you are wondering how you can configure a MySQL 5.6 instance,
// you can use this operation (you will need to retrieve the MySQL datastore ID
// by using the datastores API).
func ListDatastoreParams(client *gophercloud.ServiceClient, datastoreID, versionID string) pagination.Pager {
pageFn := func(r pagination.PageResult) pagination.Page {
return ParamPage{pagination.SinglePageBase(r)}
}
return pagination.NewPager(client, listDSParamsURL(client, datastoreID, versionID), pageFn)
}
// GetDatastoreParam will retrieve information about a specific configuration
// parameter. For example, you can use this operation to understand more about
// "innodb_file_per_table" configuration param for MySQL datastores. You will
// need the param's ID first, which can be attained by using the ListDatastoreParams
// operation.
func GetDatastoreParam(client *gophercloud.ServiceClient, datastoreID, versionID, paramID string) ParamResult {
var res ParamResult
_, res.Err = client.Request("GET", getDSParamURL(client, datastoreID, versionID, paramID), gophercloud.RequestOpts{
OkCodes: []int{200},
JSONResponse: &res.Body,
})
return res
}
// ListGlobalParams is similar to ListDatastoreParams but does not require a
// DatastoreID.
func ListGlobalParams(client *gophercloud.ServiceClient, versionID string) pagination.Pager {
pageFn := func(r pagination.PageResult) pagination.Page {
return ParamPage{pagination.SinglePageBase(r)}
}
return pagination.NewPager(client, listGlobalParamsURL(client, versionID), pageFn)
}
// GetGlobalParam is similar to GetDatastoreParam but does not require a
// DatastoreID.
func GetGlobalParam(client *gophercloud.ServiceClient, versionID, paramID string) ParamResult {
var res ParamResult
_, res.Err = client.Request("GET", getGlobalParamURL(client, versionID, paramID), gophercloud.RequestOpts{
OkCodes: []int{200},
JSONResponse: &res.Body,
})
return res
}
|