Moving network v2 to new(est) pagination idiom
diff --git a/openstack/networking/v2/apiversions/requests.go b/openstack/networking/v2/apiversions/requests.go
index 8e3ff32..31e07fd 100644
--- a/openstack/networking/v2/apiversions/requests.go
+++ b/openstack/networking/v2/apiversions/requests.go
@@ -1,11 +1,18 @@
package apiversions
-import "github.com/rackspace/gophercloud"
+import (
+ "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
+)
-func ListVersions(c *gophercloud.ServiceClient) gophercloud.Pager {
- return gophercloud.NewLinkedPager(c, APIVersionsURL(c))
+func ListVersions(c *gophercloud.ServiceClient) pagination.Pager {
+ return pagination.NewPager(c, APIVersionsURL(c), func(r pagination.LastHTTPResponse) pagination.Page {
+ return APIVersionPage{pagination.SinglePageBase(r)}
+ })
}
-func ListVersionResources(c *gophercloud.ServiceClient, v string) gophercloud.Pager {
- return gophercloud.NewLinkedPager(c, APIInfoURL(c, v))
+func ListVersionResources(c *gophercloud.ServiceClient, v string) pagination.Pager {
+ return pagination.NewPager(c, APIInfoURL(c, v), func(r pagination.LastHTTPResponse) pagination.Page {
+ return APIVersionResourcePage{pagination.SinglePageBase(r)}
+ })
}
diff --git a/openstack/networking/v2/apiversions/requests_test.go b/openstack/networking/v2/apiversions/requests_test.go
index 0922318..7c713e1 100644
--- a/openstack/networking/v2/apiversions/requests_test.go
+++ b/openstack/networking/v2/apiversions/requests_test.go
@@ -6,6 +6,7 @@
"testing"
"github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
)
@@ -50,7 +51,7 @@
count := 0
- ListVersions(ServiceClient()).EachPage(func(page gophercloud.Page) (bool, error) {
+ ListVersions(ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractAPIVersions(page)
if err != nil {
@@ -126,7 +127,7 @@
count := 0
- ListVersionResources(ServiceClient(), "v2.0").EachPage(func(page gophercloud.Page) (bool, error) {
+ ListVersionResources(ServiceClient(), "v2.0").EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractVersionResources(page)
if err != nil {
diff --git a/openstack/networking/v2/apiversions/results.go b/openstack/networking/v2/apiversions/results.go
index dbf40eb..9181e7e 100644
--- a/openstack/networking/v2/apiversions/results.go
+++ b/openstack/networking/v2/apiversions/results.go
@@ -2,7 +2,7 @@
import (
"github.com/mitchellh/mapstructure"
- "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
)
type APIVersion struct {
@@ -10,12 +10,24 @@
ID string `mapstructure:"id" json:"id"`
}
-func ExtractAPIVersions(page gophercloud.Page) ([]APIVersion, error) {
+type APIVersionPage struct {
+ pagination.SinglePageBase
+}
+
+func (r APIVersionPage) IsEmpty() (bool, error) {
+ is, err := ExtractAPIVersions(r)
+ if err != nil {
+ return true, err
+ }
+ return len(is) == 0, nil
+}
+
+func ExtractAPIVersions(page pagination.Page) ([]APIVersion, error) {
var resp struct {
Versions []APIVersion `mapstructure:"versions"`
}
- err := mapstructure.Decode(page.(gophercloud.LinkedPage).Body, &resp)
+ err := mapstructure.Decode(page.(APIVersionPage).Body, &resp)
if err != nil {
return nil, err
}
@@ -28,12 +40,24 @@
Collection string `mapstructure:"collection" json:"collection"`
}
-func ExtractVersionResources(page gophercloud.Page) ([]APIVersionResource, error) {
+type APIVersionResourcePage struct {
+ pagination.SinglePageBase
+}
+
+func (r APIVersionResourcePage) IsEmpty() (bool, error) {
+ is, err := ExtractVersionResources(r)
+ if err != nil {
+ return true, err
+ }
+ return len(is) == 0, nil
+}
+
+func ExtractVersionResources(page pagination.Page) ([]APIVersionResource, error) {
var resp struct {
APIVersionResources []APIVersionResource `mapstructure:"resources"`
}
- err := mapstructure.Decode(page.(gophercloud.LinkedPage).Body, &resp)
+ err := mapstructure.Decode(page.(APIVersionResourcePage).Body, &resp)
if err != nil {
return nil, err
}
diff --git a/openstack/networking/v2/extensions/requests.go b/openstack/networking/v2/extensions/requests.go
index 1ef7b6d..7120490 100644
--- a/openstack/networking/v2/extensions/requests.go
+++ b/openstack/networking/v2/extensions/requests.go
@@ -3,6 +3,7 @@
import (
"github.com/racker/perigee"
"github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
)
func Get(c *gophercloud.ServiceClient, name string) (*Extension, error) {
@@ -21,6 +22,8 @@
return &ext, nil
}
-func List(c *gophercloud.ServiceClient) gophercloud.Pager {
- return gophercloud.NewLinkedPager(c, ListExtensionURL(c))
+func List(c *gophercloud.ServiceClient) pagination.Pager {
+ return pagination.NewPager(c, ListExtensionURL(c), func(r pagination.LastHTTPResponse) pagination.Page {
+ return ExtensionPage{pagination.SinglePageBase(r)}
+ })
}
diff --git a/openstack/networking/v2/extensions/requests_test.go b/openstack/networking/v2/extensions/requests_test.go
index ae2fbcb..d4bc34c 100644
--- a/openstack/networking/v2/extensions/requests_test.go
+++ b/openstack/networking/v2/extensions/requests_test.go
@@ -6,6 +6,7 @@
"testing"
"github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
)
@@ -48,7 +49,7 @@
count := 0
- List(ServiceClient()).EachPage(func(page gophercloud.Page) (bool, error) {
+ List(ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractExtensions(page)
if err != nil {
diff --git a/openstack/networking/v2/extensions/results.go b/openstack/networking/v2/extensions/results.go
index c249149..c67bd01 100644
--- a/openstack/networking/v2/extensions/results.go
+++ b/openstack/networking/v2/extensions/results.go
@@ -2,7 +2,7 @@
import (
"github.com/mitchellh/mapstructure"
- "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
)
type Extension struct {
@@ -14,12 +14,24 @@
Description string `json:"description"`
}
-func ExtractExtensions(page gophercloud.Page) ([]Extension, error) {
+type ExtensionPage struct {
+ pagination.SinglePageBase
+}
+
+func (r ExtensionPage) IsEmpty() (bool, error) {
+ is, err := ExtractExtensions(r)
+ if err != nil {
+ return true, err
+ }
+ return len(is) == 0, nil
+}
+
+func ExtractExtensions(page pagination.Page) ([]Extension, error) {
var resp struct {
Extensions []Extension `mapstructure:"extensions"`
}
- err := mapstructure.Decode(page.(gophercloud.LinkedPage).Body, &resp)
+ err := mapstructure.Decode(page.(ExtensionPage).Body, &resp)
if err != nil {
return nil, err
}
diff --git a/openstack/networking/v2/networks/errors.go b/openstack/networking/v2/networks/errors.go
index 7397213..7a67ef7 100644
--- a/openstack/networking/v2/networks/errors.go
+++ b/openstack/networking/v2/networks/errors.go
@@ -6,6 +6,11 @@
return fmt.Errorf("You must specify %s for this resource", attr)
}
+func err(str string) error {
+ return fmt.Errorf("%s", str)
+}
+
var (
ErrNameRequired = requiredAttr("name")
+ ErrNoURLFound = err("Next URL could not be extracted from collection")
)
diff --git a/openstack/networking/v2/networks/requests.go b/openstack/networking/v2/networks/requests.go
index 98e70d1..7cb3598 100644
--- a/openstack/networking/v2/networks/requests.go
+++ b/openstack/networking/v2/networks/requests.go
@@ -6,6 +6,7 @@
"github.com/racker/perigee"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack/utils"
+ "github.com/rackspace/gophercloud/pagination"
)
type ListOpts struct {
@@ -17,6 +18,7 @@
ID string
Page int
PerPage int
+ Limit int
}
type NetworkOpts struct {
@@ -36,7 +38,7 @@
}
}
-func List(c *gophercloud.ServiceClient, opts ListOpts) gophercloud.Pager {
+func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
// Build query parameters
q := make(map[string]string)
if opts.Status != "" {
@@ -63,9 +65,14 @@
if opts.PerPage != 0 {
q["per_page"] = strconv.Itoa(opts.PerPage)
}
+ if opts.Limit != 0 {
+ q["limit"] = strconv.Itoa(opts.Limit)
+ }
u := ListURL(c) + utils.BuildQuery(q)
- return gophercloud.NewLinkedPager(c, u)
+ return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page {
+ return NetworkPage{pagination.LinkedPageBase(r)}
+ })
}
func Get(c *gophercloud.ServiceClient, id string) (*Network, error) {
diff --git a/openstack/networking/v2/networks/requests_test.go b/openstack/networking/v2/networks/requests_test.go
index e677f8e..b4332b0 100644
--- a/openstack/networking/v2/networks/requests_test.go
+++ b/openstack/networking/v2/networks/requests_test.go
@@ -6,6 +6,7 @@
"testing"
"github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
)
@@ -72,7 +73,7 @@
client := ServiceClient()
count := 0
- List(client, ListOpts{}).EachPage(func(page gophercloud.Page) (bool, error) {
+ List(client, ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractNetworks(page)
if err != nil {
diff --git a/openstack/networking/v2/networks/results.go b/openstack/networking/v2/networks/results.go
index b23395a..1f4b809 100644
--- a/openstack/networking/v2/networks/results.go
+++ b/openstack/networking/v2/networks/results.go
@@ -2,7 +2,7 @@
import (
"github.com/mitchellh/mapstructure"
- "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
)
type NetworkProvider struct {
@@ -31,12 +31,52 @@
PortSecurityEnabled bool `json:"port_security_enabled"`
}
-func ExtractNetworks(page gophercloud.Page) ([]Network, error) {
+type NetworkPage struct {
+ pagination.LinkedPageBase
+}
+
+func (current NetworkPage) NextPageURL() (string, error) {
+ type link struct {
+ Href string `mapstructure:"href"`
+ Rel string `mapstructure:"rel"`
+ }
+ type resp struct {
+ Links []link `mapstructure:"networks_links"`
+ }
+
+ var r resp
+ err := mapstructure.Decode(current.Body, &r)
+ if err != nil {
+ return "", err
+ }
+
+ var url string
+ for _, l := range r.Links {
+ if l.Rel == "next" {
+ url = l.Href
+ }
+ }
+ if url == "" {
+ return "", nil
+ }
+
+ return url, nil
+}
+
+func (r NetworkPage) IsEmpty() (bool, error) {
+ is, err := ExtractNetworks(r)
+ if err != nil {
+ return true, nil
+ }
+ return len(is) == 0, nil
+}
+
+func ExtractNetworks(page pagination.Page) ([]Network, error) {
var resp struct {
Networks []Network `mapstructure:"networks" json:"networks"`
}
- err := mapstructure.Decode(page.(gophercloud.LinkedPage).Body, &resp)
+ err := mapstructure.Decode(page.(NetworkPage).Body, &resp)
if err != nil {
return nil, err
}