Get instance :rocket:
diff --git a/openstack/db/v1/instances/fixtures.go b/openstack/db/v1/instances/fixtures.go
index f990165..716fe09 100644
--- a/openstack/db/v1/instances/fixtures.go
+++ b/openstack/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://my-openstack.com/v1.0/1234/flavors/1",
+ "rel": "self"
+ },
+ {
+ "href": "https://my-openstack.com/v1.0/1234/flavors/1",
+ "rel": "bookmark"
+ }
+ ]
+ },
+ "links": [
+ {
+ "href": "https://my-openstack.com/v1.0/1234/instances/1",
+ "rel": "self"
+ }
+ ],
+ "hostname": "e09ad9a3f73309469cf1f43d11e79549caf9acf2.my-openstack.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")
@@ -50,44 +89,7 @@
}
`)
- fmt.Fprintf(w, `
-{
- "instance": {
- "created": "2014-02-13T21:47:13",
- "datastore": {
- "type": "mysql",
- "version": "5.6"
- },
- "flavor": {
- "id": "1",
- "links": [
- {
- "href": "https://my-openstack.com/v1.0/1234/flavors/1",
- "rel": "self"
- },
- {
- "href": "https://my-openstack.com/v1.0/1234/flavors/1",
- "rel": "bookmark"
- }
- ]
- },
- "links": [
- {
- "href": "https://my-openstack.com/v1.0/1234/instances/1",
- "rel": "self"
- }
- ],
- "hostname": "e09ad9a3f73309469cf1f43d11e79549caf9acf2.my-openstack.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)
})
}
@@ -125,10 +127,6 @@
{
"href": "https://openstack.example.com/v1.0/1234/instances/8fb081af-f237-44f5-80cc-b46be1840ca9",
"rel": "self"
- },
- {
- "href": "https://openstack.example.com/instances/8fb081af-f237-44f5-80cc-b46be1840ca9",
- "rel": "bookmark"
}
]
}
@@ -137,3 +135,14 @@
`)
})
}
+
+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/openstack/db/v1/instances/requests.go b/openstack/db/v1/instances/requests.go
index eb60342..d5709b3 100644
--- a/openstack/db/v1/instances/requests.go
+++ b/openstack/db/v1/instances/requests.go
@@ -197,3 +197,18 @@
return pagination.NewPager(client, baseURL(client), createPageFn)
}
+
+func Get(client *gophercloud.ServiceClient, id string) GetResult {
+ var res GetResult
+
+ resp, err := perigee.Request("GET", resourceURL(client, id), perigee.Options{
+ MoreHeaders: client.AuthenticatedHeaders(),
+ Results: &res.Body,
+ OkCodes: []int{200},
+ })
+
+ res.Header = resp.HttpResponse.Header
+ res.Err = err
+
+ return res
+}
diff --git a/openstack/db/v1/instances/requests_test.go b/openstack/db/v1/instances/requests_test.go
index 236deae..6c0825b 100644
--- a/openstack/db/v1/instances/requests_test.go
+++ b/openstack/db/v1/instances/requests_test.go
@@ -9,6 +9,28 @@
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",
+ Flavor: Flavor{
+ ID: "1",
+ Links: []gophercloud.Link{
+ gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "self"},
+ gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "bookmark"},
+ },
+ },
+ Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.my-openstack.com",
+ ID: instanceID,
+ Links: []gophercloud.Link{
+ gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/instances/1", Rel: "self"},
+ },
+ Name: "json_rack_instance",
+ Status: "BUILD",
+ Volume: Volume{Size: 2},
+}
+
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
@@ -36,28 +58,8 @@
instance, err := Create(fake.ServiceClient(), opts).Extract()
- expected := &Instance{
- Created: "2014-02-13T21:47:13",
- Updated: "2014-02-13T21:47:13",
- Flavor: Flavor{
- ID: "1",
- Links: []gophercloud.Link{
- gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "self"},
- gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "bookmark"},
- },
- },
- Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.my-openstack.com",
- ID: "d4603f69-ec7e-4e9b-803f-600b9205576f",
- Links: []gophercloud.Link{
- gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/instances/1", Rel: "self"},
- },
- Name: "json_rack_instance",
- Status: "BUILD",
- Volume: Volume{Size: 2},
- }
-
th.AssertNoErr(t, err)
- th.AssertDeepEquals(t, expected, instance)
+ th.AssertDeepEquals(t, expectedInstance, instance)
}
func TestInstanceList(t *testing.T) {
@@ -106,3 +108,15 @@
t.Errorf("Expected 1 page, saw %d", pages)
}
}
+
+func TestGetInstance(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, instance, expectedInstance)
+}
diff --git a/openstack/db/v1/instances/results.go b/openstack/db/v1/instances/results.go
index 0207329..562f752 100644
--- a/openstack/db/v1/instances/results.go
+++ b/openstack/db/v1/instances/results.go
@@ -27,12 +27,20 @@
Volume Volume
}
-// CreateResult represents the result of a Create operation.
-type CreateResult struct {
+type commonResult struct {
gophercloud.Result
}
-func (r CreateResult) Extract() (*Instance, error) {
+// CreateResult represents the result of a Create operation.
+type CreateResult struct {
+ commonResult
+}
+
+type GetResult struct {
+ commonResult
+}
+
+func (r commonResult) Extract() (*Instance, error) {
if r.Err != nil {
return nil, r.Err
}
diff --git a/openstack/db/v1/instances/urls.go b/openstack/db/v1/instances/urls.go
index e049b6c..d30e762 100644
--- a/openstack/db/v1/instances/urls.go
+++ b/openstack/db/v1/instances/urls.go
@@ -5,3 +5,7 @@
func baseURL(c *gophercloud.ServiceClient) string {
return c.ServiceURL("instances")
}
+
+func resourceURL(c *gophercloud.ServiceClient, id string) string {
+ return c.ServiceURL("instances", id)
+}