Add configuration resource
diff --git a/rackspace/db/v1/configurations/doc.go b/rackspace/db/v1/configurations/doc.go
new file mode 100644
index 0000000..48c51d6
--- /dev/null
+++ b/rackspace/db/v1/configurations/doc.go
@@ -0,0 +1 @@
+package configurations
diff --git a/rackspace/db/v1/configurations/fixtures.go b/rackspace/db/v1/configurations/fixtures.go
new file mode 100644
index 0000000..df67e4c
--- /dev/null
+++ b/rackspace/db/v1/configurations/fixtures.go
@@ -0,0 +1,104 @@
+package configurations
+
+import "fmt"
+
+const singleConfigJSON = `
+{
+ "created": "2014-07-31T18:56:09",
+ "datastore_name": "mysql",
+ "datastore_version_id": "b00000b0-00b0-0b00-00b0-000b000000bb",
+ "datastore_version_name": "5.6",
+ "description": "example_description",
+ "id": "005a8bb7-a8df-40ee-b0b7-fc144641abc2",
+ "name": "example-configuration-name",
+ "updated": "2014-07-31T18:56:09"
+}
+`
+
+const singleConfigWithValuesJSON = `
+{
+ "created": "2014-07-31T15:02:52",
+ "datastore_name": "mysql",
+ "datastore_version_id": "b00000b0-00b0-0b00-00b0-000b000000bb",
+ "datastore_version_name": "5.6",
+ "description": "example description",
+ "id": "005a8bb7-a8df-40ee-b0b7-fc144641abc2",
+ "instance_count": 0,
+ "name": "example-configuration-name",
+ "updated": "2014-07-31T15:02:52",
+ "values": {
+ "collation_server": "latin1_swedish_ci",
+ "connect_timeout": 120
+ }
+}
+`
+
+var (
+ listConfigsJSON = fmt.Sprintf(`{"configurations": [%s]}`, singleConfigJSON)
+ getConfigJSON = fmt.Sprintf(`{"configuration": %s}`, singleConfigJSON)
+ createConfigJSON = fmt.Sprintf(`{"configuration": %s}`, singleConfigWithValuesJSON)
+)
+
+var createReq = `
+{
+ "configuration": {
+ "datastore": {
+ "type": "a00000a0-00a0-0a00-00a0-000a000000aa",
+ "version": "b00000b0-00b0-0b00-00b0-000b000000bb"
+ },
+ "description": "example description",
+ "name": "example-configuration-name",
+ "values": {
+ "collation_server": "latin1_swedish_ci",
+ "connect_timeout": 120
+ }
+ }
+}
+`
+
+var updateReq = `
+{
+ "configuration": {
+ "values": {
+ "connect_timeout": 300
+ }
+ }
+}
+`
+
+var listInstancesJSON = `
+{
+ "instances": [
+ {
+ "id": "d4603f69-ec7e-4e9b-803f-600b9205576f",
+ "name": "json_rack_instance"
+ }
+ ]
+}
+`
+
+var exampleConfig = Config{
+ Created: "2014-07-31T18:56:09",
+ DatastoreName: "mysql",
+ DatastoreVersionID: "b00000b0-00b0-0b00-00b0-000b000000bb",
+ DatastoreVersionName: "5.6",
+ Description: "example_description",
+ ID: "005a8bb7-a8df-40ee-b0b7-fc144641abc2",
+ Name: "example-configuration-name",
+ Updated: "2014-07-31T18:56:09",
+}
+
+var exampleConfigWithValues = Config{
+ Created: "2014-07-31T15:02:52",
+ DatastoreName: "mysql",
+ DatastoreVersionID: "b00000b0-00b0-0b00-00b0-000b000000bb",
+ DatastoreVersionName: "5.6",
+ Description: "example description",
+ ID: "005a8bb7-a8df-40ee-b0b7-fc144641abc2",
+ Name: "example-configuration-name",
+ Updated: "2014-07-31T15:02:52",
+ Values: map[string]interface{}{
+ "collation_server": "latin1_swedish_ci",
+ "connect_timeout": 120,
+ },
+}
diff --git a/rackspace/db/v1/configurations/requests.go b/rackspace/db/v1/configurations/requests.go
new file mode 100644
index 0000000..1dbee7a
--- /dev/null
+++ b/rackspace/db/v1/configurations/requests.go
@@ -0,0 +1,195 @@
+package configurations
+
+import (
+ "errors"
+
+ "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/openstack/db/v1/instances"
+ "github.com/rackspace/gophercloud/pagination"
+)
+
+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)
+}
+
+type CreateOptsBuilder interface {
+ ToConfigCreateMap() (map[string]interface{}, error)
+}
+
+type DatastoreOpts struct {
+ Type string
+ Version string
+}
+
+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
+}
+
+type CreateOpts struct {
+ Datastore *DatastoreOpts
+ Description string
+ Name string
+ Values map[string]interface{}
+}
+
+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
+}
+
+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{201},
+ JSONBody: &reqBody,
+ JSONResponse: &res.Body,
+ })
+
+ return res
+}
+
+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
+}
+
+type UpdateOptsBuilder interface {
+ ToConfigUpdateMap() (map[string]interface{}, error)
+}
+
+type UpdateOpts struct {
+ Datastore *DatastoreOpts
+ Description string
+ Name string
+ Values map[string]interface{}
+}
+
+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
+}
+
+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,
+ JSONResponse: &res.Body,
+ })
+
+ return res
+}
+
+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,
+ JSONResponse: &res.Body,
+ })
+
+ return res
+}
+
+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
+}
+
+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)
+}
diff --git a/rackspace/db/v1/configurations/requests_test.go b/rackspace/db/v1/configurations/requests_test.go
new file mode 100644
index 0000000..14120b3
--- /dev/null
+++ b/rackspace/db/v1/configurations/requests_test.go
@@ -0,0 +1,145 @@
+package configurations
+
+import (
+ "testing"
+
+ "github.com/rackspace/gophercloud/pagination"
+ "github.com/rackspace/gophercloud/rackspace/db/v1/instances"
+ th "github.com/rackspace/gophercloud/testhelper"
+ fake "github.com/rackspace/gophercloud/testhelper/client"
+ "github.com/rackspace/gophercloud/testhelper/fixture"
+)
+
+var (
+ configID = "{configID}"
+ _baseURL = "/configurations"
+ resURL = _baseURL + "/" + configID
+)
+
+func TestList(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ fixture.SetupHandler(t, _baseURL, "GET", "", listConfigsJSON, 200)
+
+ count := 0
+ err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
+ count++
+ actual, err := ExtractConfigs(page)
+ th.AssertNoErr(t, err)
+
+ expected := []Config{exampleConfig}
+ th.AssertDeepEquals(t, expected, actual)
+
+ return true, nil
+ })
+
+ th.AssertEquals(t, 1, count)
+ th.AssertNoErr(t, err)
+}
+
+func TestGet(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ fixture.SetupHandler(t, resURL, "GET", "", getConfigJSON, 200)
+
+ config, err := Get(fake.ServiceClient(), configID).Extract()
+ th.AssertNoErr(t, err)
+ th.AssertDeepEquals(t, &exampleConfig, config)
+}
+
+func TestCreate(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ fixture.SetupHandler(t, _baseURL, "POST", createReq, createConfigJSON, 201)
+
+ opts := CreateOpts{
+ Datastore: &DatastoreOpts{
+ Type: "a00000a0-00a0-0a00-00a0-000a000000aa",
+ Version: "b00000b0-00b0-0b00-00b0-000b000000bb",
+ },
+ Description: "example description",
+ Name: "example-configuration-name",
+ Values: map[string]interface{}{
+ "collation_server": "latin1_swedish_ci",
+ "connect_timeout": 120,
+ },
+ }
+
+ config, err := Create(fake.ServiceClient(), opts).Extract()
+ th.AssertNoErr(t, err)
+ th.AssertDeepEquals(t, &exampleConfigWithValues, config)
+}
+
+func TestUpdate(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ fixture.SetupHandler(t, resURL, "PATCH", updateReq, "", 200)
+
+ opts := UpdateOpts{
+ Values: map[string]interface{}{
+ "connect_timeout": 300,
+ },
+ }
+
+ err := Update(fake.ServiceClient(), configID, opts).ExtractErr()
+ th.AssertNoErr(t, err)
+}
+
+func TestReplace(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ fixture.SetupHandler(t, resURL, "PUT", updateReq, "", 202)
+
+ opts := UpdateOpts{
+ Values: map[string]interface{}{
+ "connect_timeout": 300,
+ },
+ }
+
+ err := Replace(fake.ServiceClient(), configID, opts).ExtractErr()
+ th.AssertNoErr(t, err)
+}
+
+func TestDelete(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ fixture.SetupHandler(t, resURL, "DELETE", "", "", 202)
+
+ err := Delete(fake.ServiceClient(), configID).ExtractErr()
+ th.AssertNoErr(t, err)
+}
+
+func TestListInstances(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ fixture.SetupHandler(t, resURL+"/instances", "GET", "", listInstancesJSON, 200)
+
+ expectedInstance := instances.Instance{
+ ID: "d4603f69-ec7e-4e9b-803f-600b9205576f",
+ Name: "json_rack_instance",
+ }
+
+ pages := 0
+ err := ListInstances(fake.ServiceClient(), configID).EachPage(func(page pagination.Page) (bool, error) {
+ pages++
+
+ actual, err := instances.ExtractInstances(page)
+ if err != nil {
+ return false, err
+ }
+
+ th.AssertDeepEquals(t, actual, []instances.Instance{expectedInstance})
+
+ return true, nil
+ })
+
+ th.AssertNoErr(t, err)
+ th.AssertEquals(t, 1, pages)
+}
diff --git a/rackspace/db/v1/configurations/results.go b/rackspace/db/v1/configurations/results.go
new file mode 100644
index 0000000..cd83d10
--- /dev/null
+++ b/rackspace/db/v1/configurations/results.go
@@ -0,0 +1,79 @@
+package configurations
+
+import (
+ "github.com/mitchellh/mapstructure"
+ "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
+)
+
+type Config struct {
+ Created string
+ Updated string
+ DatastoreName string `mapstructure:"datastore_name"`
+ DatastoreVersionID string `mapstructure:"datastore_version_id"`
+ DatastoreVersionName string `mapstructure:"datastore_version_name"`
+ Description string
+ ID string
+ Name string
+ Values map[string]interface{}
+}
+
+type ConfigPage struct {
+ pagination.SinglePageBase
+}
+
+func (r ConfigPage) IsEmpty() (bool, error) {
+ is, err := ExtractConfigs(r)
+ if err != nil {
+ return true, err
+ }
+ return len(is) == 0, nil
+}
+
+func ExtractConfigs(page pagination.Page) ([]Config, error) {
+ casted := page.(ConfigPage).Body
+
+ var resp struct {
+ Configs []Config `mapstructure:"configurations" json:"configurations"`
+ }
+
+ err := mapstructure.Decode(casted, &resp)
+ return resp.Configs, err
+}
+
+type commonResult struct {
+ gophercloud.Result
+}
+
+func (r commonResult) Extract() (*Config, error) {
+ if r.Err != nil {
+ return nil, r.Err
+ }
+
+ var response struct {
+ Config Config `mapstructure:"configuration"`
+ }
+
+ err := mapstructure.Decode(r.Body, &response)
+ return &response.Config, err
+}
+
+type GetResult struct {
+ commonResult
+}
+
+type CreateResult struct {
+ commonResult
+}
+
+type UpdateResult struct {
+ gophercloud.ErrResult
+}
+
+type ReplaceResult struct {
+ gophercloud.ErrResult
+}
+
+type DeleteResult struct {
+ gophercloud.ErrResult
+}
diff --git a/rackspace/db/v1/configurations/urls.go b/rackspace/db/v1/configurations/urls.go
new file mode 100644
index 0000000..9ac02e6
--- /dev/null
+++ b/rackspace/db/v1/configurations/urls.go
@@ -0,0 +1,15 @@
+package configurations
+
+import "github.com/rackspace/gophercloud"
+
+func baseURL(c *gophercloud.ServiceClient) string {
+ return c.ServiceURL("configurations")
+}
+
+func resourceURL(c *gophercloud.ServiceClient, configID string) string {
+ return c.ServiceURL("configurations", configID)
+}
+
+func instancesURL(c *gophercloud.ServiceClient, configID string) string {
+ return c.ServiceURL("configurations", configID, "instances")
+}