fix images 'AllPages' paging (#266)
* fix images AllPages paging; make AllPages more robust
* need to keep using the local page when iterating in AllPages
diff --git a/openstack/imageservice/v2/images/testing/requests_test.go b/openstack/imageservice/v2/images/testing/requests_test.go
index 788eec4..fdd8402 100644
--- a/openstack/imageservice/v2/images/testing/requests_test.go
+++ b/openstack/imageservice/v2/images/testing/requests_test.go
@@ -44,6 +44,19 @@
th.AssertEquals(t, 3, count)
}
+func TestAllPagesImage(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ HandleImageListSuccessfully(t)
+
+ pages, err := images.List(fakeclient.ServiceClient(), nil).AllPages()
+ th.AssertNoErr(t, err)
+ images, err := images.ExtractImages(pages)
+ th.AssertNoErr(t, err)
+ th.AssertEquals(t, 3, len(images))
+}
+
func TestCreateImage(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
diff --git a/pagination/pager.go b/pagination/pager.go
index 1b5192a..76e4af1 100644
--- a/pagination/pager.go
+++ b/pagination/pager.go
@@ -145,27 +145,26 @@
// Switch on the page body type. Recognized types are `map[string]interface{}`,
// `[]byte`, and `[]interface{}`.
- switch testPage.GetBody().(type) {
+ switch pb := testPage.GetBody().(type) {
case map[string]interface{}:
// key is the map key for the page body if the body type is `map[string]interface{}`.
var key string
// Iterate over the pages to concatenate the bodies.
err = p.EachPage(func(page Page) (bool, error) {
b := page.GetBody().(map[string]interface{})
- for k := range b {
+ for k, v := range b {
// If it's a linked page, we don't want the `links`, we want the other one.
if !strings.HasSuffix(k, "links") {
- key = k
+ switch vt := v.(type) {
+ case map[string]interface{}:
+ key = k
+ pagesSlice = append(pagesSlice, vt)
+ case []interface{}:
+ key = k
+ pagesSlice = append(pagesSlice, vt...)
+ }
}
}
- switch keyType := b[key].(type) {
- case map[string]interface{}:
- pagesSlice = append(pagesSlice, keyType)
- case []interface{}:
- pagesSlice = append(pagesSlice, b[key].([]interface{})...)
- default:
- return false, fmt.Errorf("Unsupported page body type: %+v", keyType)
- }
return true, nil
})
if err != nil {
@@ -216,7 +215,7 @@
default:
err := gophercloud.ErrUnexpectedType{}
err.Expected = "map[string]interface{}/[]byte/[]interface{}"
- err.Actual = fmt.Sprintf("%v", reflect.TypeOf(testPage.GetBody()))
+ err.Actual = fmt.Sprintf("%T", pb)
return nil, err
}