Adding delegate for Get instance + tests
diff --git a/rackspace/db/v1/instances/delegate.go b/rackspace/db/v1/instances/delegate.go
index c8551df..16f1bb1 100644
--- a/rackspace/db/v1/instances/delegate.go
+++ b/rackspace/db/v1/instances/delegate.go
@@ -3,6 +3,7 @@
import (
"github.com/rackspace/gophercloud"
os "github.com/rackspace/gophercloud/openstack/db/v1/instances"
+ "github.com/rackspace/gophercloud/pagination"
)
// DatastoreOpts represents the configuration for how an instance stores data.
@@ -95,3 +96,11 @@
func Create(client *gophercloud.ServiceClient, opts os.CreateOptsBuilder) CreateResult {
return CreateResult{os.Create(client, opts)}
}
+
+func List(client *gophercloud.ServiceClient) pagination.Pager {
+ return os.List(client)
+}
+
+func Get(client *gophercloud.ServiceClient, id string) GetResult {
+ return GetResult{os.Get(client, id)}
+}
diff --git a/rackspace/db/v1/instances/delegate_test.go b/rackspace/db/v1/instances/delegate_test.go
index 03dd47a..835297c 100644
--- a/rackspace/db/v1/instances/delegate_test.go
+++ b/rackspace/db/v1/instances/delegate_test.go
@@ -9,6 +9,29 @@
fake "github.com/rackspace/gophercloud/testhelper/client"
)
+var instanceID = "d4603f69-ec7e-4e9b-803f-600b9205576f"
+
+var expectedInstance = &Instance{
+ Created: "2014-02-13T21:47:13",
+ Updated: "2014-02-13T21:47:13",
+ Datastore: Datastore{Type: "mysql", Version: "5.6"},
+ Flavor: os.Flavor{
+ ID: "1",
+ Links: []gophercloud.Link{
+ gophercloud.Link{Href: "https://ord.databases.api.rackspacecloud.com/v1.0/1234/flavors/1", Rel: "self"},
+ gophercloud.Link{Href: "https://ord.databases.api.rackspacecloud.com/v1.0/1234/flavors/1", Rel: "bookmark"},
+ },
+ },
+ Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.rackspaceclouddb.com",
+ ID: instanceID,
+ Links: []gophercloud.Link{
+ gophercloud.Link{Href: "https://ord.databases.api.rackspacecloud.com/v1.0/1234/flavors/1", Rel: "self"},
+ },
+ Name: "json_rack_instance",
+ Status: "BUILD",
+ Volume: os.Volume{Size: 2},
+}
+
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
@@ -37,27 +60,18 @@
instance, err := Create(fake.ServiceClient(), opts).Extract()
- expected := &Instance{
- Created: "2014-02-13T21:47:13",
- Updated: "2014-02-13T21:47:13",
- Datastore: Datastore{Type: "mysql", Version: "5.6"},
- Flavor: os.Flavor{
- ID: "1",
- Links: []gophercloud.Link{
- gophercloud.Link{Href: "https://ord.databases.api.rackspacecloud.com/v1.0/1234/flavors/1", Rel: "self"},
- gophercloud.Link{Href: "https://ord.databases.api.rackspacecloud.com/v1.0/1234/flavors/1", Rel: "bookmark"},
- },
- },
- Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.rackspaceclouddb.com",
- ID: "d4603f69-ec7e-4e9b-803f-600b9205576f",
- Links: []gophercloud.Link{
- gophercloud.Link{Href: "https://ord.databases.api.rackspacecloud.com/v1.0/1234/flavors/1", Rel: "self"},
- },
- Name: "json_rack_instance",
- Status: "BUILD",
- Volume: os.Volume{Size: 2},
- }
+ th.AssertNoErr(t, err)
+ th.AssertDeepEquals(t, expectedInstance, instance)
+}
+
+func TestGet(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ HandleGetInstanceSuccessfully(t, instanceID)
+
+ instance, err := Get(fake.ServiceClient(), instanceID).Extract()
th.AssertNoErr(t, err)
- th.AssertDeepEquals(t, expected, instance)
+ th.AssertDeepEquals(t, expectedInstance, instance)
}
diff --git a/rackspace/db/v1/instances/fixtures.go b/rackspace/db/v1/instances/fixtures.go
index 79537ab..ac2471e 100644
--- a/rackspace/db/v1/instances/fixtures.go
+++ b/rackspace/db/v1/instances/fixtures.go
@@ -9,6 +9,45 @@
fake "github.com/rackspace/gophercloud/testhelper/client"
)
+const singleInstanceJson = `
+{
+ "instance": {
+ "created": "2014-02-13T21:47:13",
+ "datastore": {
+ "type": "mysql",
+ "version": "5.6"
+ },
+ "flavor": {
+ "id": "1",
+ "links": [
+ {
+ "href": "https://ord.databases.api.rackspacecloud.com/v1.0/1234/flavors/1",
+ "rel": "self"
+ },
+ {
+ "href": "https://ord.databases.api.rackspacecloud.com/v1.0/1234/flavors/1",
+ "rel": "bookmark"
+ }
+ ]
+ },
+ "links": [
+ {
+ "href": "https://ord.databases.api.rackspacecloud.com/v1.0/1234/flavors/1",
+ "rel": "self"
+ }
+ ],
+ "hostname": "e09ad9a3f73309469cf1f43d11e79549caf9acf2.rackspaceclouddb.com",
+ "id": "d4603f69-ec7e-4e9b-803f-600b9205576f",
+ "name": "json_rack_instance",
+ "status": "BUILD",
+ "updated": "2014-02-13T21:47:13",
+ "volume": {
+ "size": 2
+ }
+ }
+}
+`
+
func HandleCreateInstanceSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/instances", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
@@ -51,43 +90,17 @@
}
`)
- fmt.Fprintf(w, `
-{
- "instance": {
- "created": "2014-02-13T21:47:13",
- "datastore": {
- "type": "mysql",
- "version": "5.6"
- },
- "flavor": {
- "id": "1",
- "links": [
- {
- "href": "https://ord.databases.api.rackspacecloud.com/v1.0/1234/flavors/1",
- "rel": "self"
- },
- {
- "href": "https://ord.databases.api.rackspacecloud.com/v1.0/1234/flavors/1",
- "rel": "bookmark"
- }
- ]
- },
- "links": [
- {
- "href": "https://ord.databases.api.rackspacecloud.com/v1.0/1234/flavors/1",
- "rel": "self"
- }
- ],
- "hostname": "e09ad9a3f73309469cf1f43d11e79549caf9acf2.rackspaceclouddb.com",
- "id": "d4603f69-ec7e-4e9b-803f-600b9205576f",
- "name": "json_rack_instance",
- "status": "BUILD",
- "updated": "2014-02-13T21:47:13",
- "volume": {
- "size": 2
- }
- }
+ fmt.Fprintf(w, singleInstanceJson)
+ })
}
-`)
+
+func HandleGetInstanceSuccessfully(t *testing.T, id string) {
+ th.Mux.HandleFunc("/instances/"+id, func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+
+ w.Header().Add("Content-Type", "application/json")
+
+ fmt.Fprintf(w, singleInstanceJson)
})
}
diff --git a/rackspace/db/v1/instances/results.go b/rackspace/db/v1/instances/results.go
index df13aed..cab403a 100644
--- a/rackspace/db/v1/instances/results.go
+++ b/rackspace/db/v1/instances/results.go
@@ -24,21 +24,32 @@
Volume os.Volume
}
-// CreateResult represents the result of a Create operation.
-type CreateResult struct {
- os.CreateResult
-}
-
-func (r CreateResult) Extract() (*Instance, error) {
- if r.Err != nil {
- return nil, r.Err
+func commonExtract(err error, body interface{}) (*Instance, error) {
+ if err != nil {
+ return nil, err
}
var response struct {
Instance Instance `mapstructure:"instance"`
}
- err := mapstructure.Decode(r.Body, &response)
-
+ err = mapstructure.Decode(body, &response)
return &response.Instance, err
}
+
+// CreateResult represents the result of a Create operation.
+type CreateResult struct {
+ os.CreateResult
+}
+
+func (r CreateResult) Extract() (*Instance, error) {
+ return commonExtract(r.Err, r.Body)
+}
+
+type GetResult struct {
+ os.GetResult
+}
+
+func (r GetResult) Extract() (*Instance, error) {
+ return commonExtract(r.Err, r.Body)
+}