Override the ExtractExtensions function.
It turns out that identity changes the response body structure.
diff --git a/openstack/identity/v2/extensions/delegate.go b/openstack/identity/v2/extensions/delegate.go
index d08e1fd..1992a2c 100644
--- a/openstack/identity/v2/extensions/delegate.go
+++ b/openstack/identity/v2/extensions/delegate.go
@@ -1,6 +1,7 @@
package extensions
import (
+ "github.com/mitchellh/mapstructure"
"github.com/rackspace/gophercloud"
common "github.com/rackspace/gophercloud/openstack/common/extensions"
"github.com/rackspace/gophercloud/pagination"
@@ -16,17 +17,58 @@
common.GetResult
}
-// ExtractExtensions interprets a Page as a slice of Extensions.
+// ExtensionPage is a single page of Extension results.
+type ExtensionPage struct {
+ common.ExtensionPage
+}
+
+// IsEmpty returns true if the current page contains at least one Extension.
+func (page ExtensionPage) IsEmpty() (bool, error) {
+ is, err := ExtractExtensions(page)
+ if err != nil {
+ return true, err
+ }
+ return len(is) == 0, nil
+}
+
+// ExtractExtensions accepts a Page struct, specifically an ExtensionPage struct, and extracts the
+// elements into a slice of Extension structs.
func ExtractExtensions(page pagination.Page) ([]Extension, error) {
- inner, err := common.ExtractExtensions(page)
+ // Identity v2 adds an intermediate "values" object.
+
+ type extension struct {
+ Updated string `mapstructure:"updated"`
+ Name string `mapstructure:"name"`
+ Namespace string `mapstructure:"namespace"`
+ Alias string `mapstructure:"alias"`
+ Description string `mapstructure:"description"`
+ Links []interface{} `mapstructure:"links"`
+ }
+
+ var resp struct {
+ Extensions struct {
+ Values []extension `mapstructure:"values"`
+ } `mapstructure:"extensions"`
+ }
+
+ err := mapstructure.Decode(page.(ExtensionPage).Body, &resp)
if err != nil {
return nil, err
}
- outer := make([]Extension, len(inner))
- for index, ext := range inner {
- outer[index] = Extension{ext}
+
+ exts := make([]Extension, len(resp.Extensions.Values))
+ for i, original := range resp.Extensions.Values {
+ exts[i] = Extension{common.Extension{
+ Updated: original.Updated,
+ Name: original.Name,
+ Namespace: original.Namespace,
+ Alias: original.Alias,
+ Description: original.Description,
+ Links: original.Links,
+ }}
}
- return outer, nil
+
+ return exts, err
}
// Get retrieves information for a specific extension using its alias.
@@ -37,5 +79,7 @@
// List returns a Pager which allows you to iterate over the full collection of extensions.
// It does not accept query parameters.
func List(c *gophercloud.ServiceClient) pagination.Pager {
- return common.List(c)
+ return pagination.NewPager(c, common.ListExtensionURL(c), func(r pagination.LastHTTPResponse) pagination.Page {
+ return ExtensionPage{common.ExtensionPage{SinglePageBase: pagination.SinglePageBase(r)}}
+ })
}
diff --git a/openstack/identity/v2/extensions/delegate_test.go b/openstack/identity/v2/extensions/delegate_test.go
index 5cc7c12..90eb21b 100644
--- a/openstack/identity/v2/extensions/delegate_test.go
+++ b/openstack/identity/v2/extensions/delegate_test.go
@@ -26,7 +26,7 @@
th.SetupHTTP()
defer th.TeardownHTTP()
- th.Mux.HandleFunc("/v2.0/extensions", func(w http.ResponseWriter, r *http.Request) {
+ th.Mux.HandleFunc("/extensions", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", TokenID)
@@ -34,28 +34,28 @@
fmt.Fprintf(w, `
{
- "extensions": [
- {
- "updated": "2013-01-20T00:00:00-00:00",
- "name": "Neutron Service Type Management",
- "links": [],
- "namespace": "http://docs.openstack.org/ext/neutron/service-type/api/v1.0",
- "alias": "service-type",
- "description": "API for retrieving service providers for Neutron advanced services"
- }
- ]
+ "extensions": {
+ "values": [
+ {
+ "updated": "2013-01-20T00:00:00-00:00",
+ "name": "Neutron Service Type Management",
+ "links": [],
+ "namespace": "http://docs.openstack.org/ext/neutron/service-type/api/v1.0",
+ "alias": "service-type",
+ "description": "API for retrieving service providers for Neutron advanced services"
+ }
+ ]
+ }
}
- `)
+ `)
})
count := 0
- List(ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
+ err := List(ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractExtensions(page)
- if err != nil {
- t.Errorf("Failed to extract extensions: %v", err)
- }
+ th.AssertNoErr(t, err)
expected := []Extension{
Extension{
@@ -74,17 +74,15 @@
return true, nil
})
-
- if count != 1 {
- t.Errorf("Expected 1 page, got %d", count)
- }
+ th.AssertNoErr(t, err)
+ th.CheckEquals(t, 1, count)
}
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
- th.Mux.HandleFunc("/v2.0/extensions/agent", func(w http.ResponseWriter, r *http.Request) {
+ th.Mux.HandleFunc("/extensions/agent", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", TokenID)