Implement images.Get(). Image tests now pass.
diff --git a/openstack/compute/v2/images/images.go b/openstack/compute/v2/images/images.go
index 0dee058..0fc3e59 100644
--- a/openstack/compute/v2/images/images.go
+++ b/openstack/compute/v2/images/images.go
@@ -36,3 +36,10 @@
err := mapstructure.Decode(results, casted)
return results, err
}
+
+// ExtractImage converts the result of a Get call into a more usable Image structure.
+func ExtractImage(result GetResult) (Image, error) {
+ var decoded Image
+ err := mapstructure.Decode(result, &decoded)
+ return decoded, err
+}
diff --git a/openstack/compute/v2/images/images_test.go b/openstack/compute/v2/images/images_test.go
index 76ffbec..217baee 100644
--- a/openstack/compute/v2/images/images_test.go
+++ b/openstack/compute/v2/images/images_test.go
@@ -8,33 +8,38 @@
const (
// This example was taken from: http://docs.openstack.org/api/openstack-compute/2/content/Rebuild_Server-d1e3538.html
- simpleImageJSON = `{
+ simpleImageJSON = `
+ {
"id": "52415800-8b69-11e0-9b19-734f6f006e54",
"name": "CentOS 5.2",
- "links": [{
- "rel": "self",
- "href": "http://servers.api.openstack.org/v2/1234/images/52415800-8b69-11e0-9b19-734f6f006e54"
- },{
- "rel": "bookmark",
- "href": "http://servers.api.openstack.org/1234/images/52415800-8b69-11e0-9b19-734f6f006e54"
- }]
- }`
+ "links": [
+ {
+ "rel": "self",
+ "href": "http://servers.api.openstack.org/v2/1234/images/52415800-8b69-11e0-9b19-734f6f006e54"
+ },
+ {
+ "rel": "bookmark",
+ "href": "http://servers.api.openstack.org/1234/images/52415800-8b69-11e0-9b19-734f6f006e54"
+ }
+ ]
+ }
+ `
)
func TestExtractImage(t *testing.T) {
- var simpleImageMap map[string]interface{}
- err := json.Unmarshal([]byte(simpleImageJSON), &simpleImageMap)
+ var simpleImage GetResult
+ err := json.Unmarshal([]byte(simpleImageJSON), &simpleImage)
if err != nil {
t.Fatal(err)
}
- image, err := ExtractImage(simpleImageMap)
+ image, err := ExtractImage(simpleImage)
if err != nil {
t.Fatal(err)
}
- if image.Id != "52415800-8b69-11e0-9b19-734f6f006e54" {
- t.Fatal("I expected an image ID of 52415800-8b69-11e0-9b19-734f6f006e54; got " + image.Id)
+ if image.ID != "52415800-8b69-11e0-9b19-734f6f006e54" {
+ t.Fatal("I expected an image ID of 52415800-8b69-11e0-9b19-734f6f006e54; got " + image.ID)
}
if image.Name != "CentOS 5.2" {
diff --git a/openstack/compute/v2/images/requests.go b/openstack/compute/v2/images/requests.go
index 832979c..8736382 100644
--- a/openstack/compute/v2/images/requests.go
+++ b/openstack/compute/v2/images/requests.go
@@ -1,6 +1,7 @@
package images
import (
+ "github.com/racker/perigee"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
@@ -32,6 +33,10 @@
return images[len(images)-1].ID, nil
}
+// GetResult opaquely stores the result of a Get call.
+// Use ExtractImage() to translate it into this provider's version of an Image structure.
+type GetResult map[string]interface{}
+
// List enumerates the available images.
func List(client *gophercloud.ServiceClient) pagination.Pager {
createPage := func(r pagination.LastHTTPResponse) pagination.Page {
@@ -42,3 +47,15 @@
return pagination.NewPager(client, getListURL(client), createPage)
}
+
+// Get acquires additional detail about a specific image by ID.
+// Use ExtractImage() to intepret the result as an openstack Image.
+func Get(client *gophercloud.ServiceClient, id string) (GetResult, error) {
+ var result GetResult
+ _, err := perigee.Request("GET", getImageURL(client, id), perigee.Options{
+ MoreHeaders: client.Provider.AuthenticatedHeaders(),
+ Results: &result,
+ OkCodes: []int{200},
+ })
+ return result, err
+}
diff --git a/openstack/compute/v2/images/urls.go b/openstack/compute/v2/images/urls.go
index 0e40a4e..93fa8d2 100644
--- a/openstack/compute/v2/images/urls.go
+++ b/openstack/compute/v2/images/urls.go
@@ -5,3 +5,7 @@
func getListURL(client *gophercloud.ServiceClient) string {
return client.ServiceURL("images", "detail")
}
+
+func getImageURL(client *gophercloud.ServiceClient, id string) string {
+ return client.ServiceURL("images", id)
+}