Use the new pagination in identity/3/services.
diff --git a/openstack/identity/v3/services/requests.go b/openstack/identity/v3/services/requests.go
index e261192..f6cb22d 100644
--- a/openstack/identity/v3/services/requests.go
+++ b/openstack/identity/v3/services/requests.go
@@ -1,6 +1,7 @@
package services
import (
+ "net/http"
"strconv"
"github.com/racker/perigee"
@@ -42,7 +43,7 @@
}
// List enumerates the services available to a specific user.
-func List(client *gophercloud.ServiceClient, opts ListOpts) (*ServiceList, error) {
+func List(client *gophercloud.ServiceClient, opts ListOpts) gophercloud.Pager {
q := make(map[string]string)
if opts.ServiceType != "" {
q["type"] = opts.ServiceType
@@ -55,17 +56,16 @@
}
u := getListURL(client) + utils.BuildQuery(q)
- var resp ServiceList
- _, err := perigee.Request("GET", u, perigee.Options{
- MoreHeaders: client.Provider.AuthenticatedHeaders(),
- Results: &resp,
- OkCodes: []int{200},
+ return gophercloud.NewLinkedPager(u, func(next string) (http.Response, error) {
+ resp, err := perigee.Request("GET", u, perigee.Options{
+ MoreHeaders: client.Provider.AuthenticatedHeaders(),
+ OkCodes: []int{200},
+ })
+ if err != nil {
+ return http.Response{}, err
+ }
+ return resp.HttpResponse, nil
})
- if err != nil {
- return nil, err
- }
-
- return &resp, nil
}
// Get returns additional information about a service, given its ID.
diff --git a/openstack/identity/v3/services/requests_test.go b/openstack/identity/v3/services/requests_test.go
index 9f43db3..9fea519 100644
--- a/openstack/identity/v3/services/requests_test.go
+++ b/openstack/identity/v3/services/requests_test.go
@@ -98,33 +98,40 @@
client := serviceClient()
- result, err := List(client, ListOpts{})
- if err != nil {
- t.Fatalf("Error listing services: %v", err)
- }
+ count := 0
+ List(client, ListOpts{}).EachPage(func(page gophercloud.Page) bool {
+ count++
+ actual, err := ExtractServices(page)
+ if err != nil {
+ t.Errorf("Unexpected error extracting services: %v", err)
+ return false
+ }
- collection, err := gophercloud.AllPages(result)
- actual := AsServices(collection)
+ desc0 := "Service One"
+ desc1 := "Service Two"
+ expected := []Service{
+ Service{
+ Description: &desc0,
+ ID: "1234",
+ Name: "service-one",
+ Type: "identity",
+ },
+ Service{
+ Description: &desc1,
+ ID: "9876",
+ Name: "service-two",
+ Type: "compute",
+ },
+ }
- desc0 := "Service One"
- desc1 := "Service Two"
- expected := []Service{
- Service{
- Description: &desc0,
- ID: "1234",
- Name: "service-one",
- Type: "identity",
- },
- Service{
- Description: &desc1,
- ID: "9876",
- Name: "service-two",
- Type: "compute",
- },
- }
+ if !reflect.DeepEqual(expected, actual) {
+ t.Errorf("Expected %#v, got %#v", expected, actual)
+ }
- if !reflect.DeepEqual(expected, actual) {
- t.Errorf("Expected %#v, got %#v", expected, actual)
+ return true
+ })
+ if count != 1 {
+ t.Errorf("Expected 1 page, got %d", count)
}
}
diff --git a/openstack/identity/v3/services/results.go b/openstack/identity/v3/services/results.go
index 88510b4..537ea2e 100644
--- a/openstack/identity/v3/services/results.go
+++ b/openstack/identity/v3/services/results.go
@@ -1,10 +1,9 @@
package services
import (
- "fmt"
+ "github.com/rackspace/gophercloud"
"github.com/mitchellh/mapstructure"
- "github.com/rackspace/gophercloud"
)
// Service is the result of a list or information query.
@@ -15,54 +14,12 @@
Type string `json:"type"`
}
-// ServiceList is a collection of Services.
-type ServiceList struct {
- gophercloud.PaginationLinks `json:"links"`
-
- client *gophercloud.ServiceClient
- Services []Service `json:"services"`
-}
-
-// Pager indicates that the ServiceList is paginated by next and previous links.
-func (list ServiceList) Pager() gophercloud.Pager {
- return gophercloud.NewLinkPager(list)
-}
-
-// Concat returns a new collection that's the result of appending a new collection at the end of this one.
-func (list ServiceList) Concat(other gophercloud.Collection) gophercloud.Collection {
- return ServiceList{
- client: list.client,
- Services: append(list.Services, AsServices(other)...),
- }
-}
-
-// Service returns the ServiceClient used to acquire this list.
-func (list ServiceList) Service() *gophercloud.ServiceClient {
- return list.client
-}
-
-// Links accesses pagination information for the current page.
-func (list ServiceList) Links() gophercloud.PaginationLinks {
- return list.PaginationLinks
-}
-
-// Interpret parses a follow-on JSON response as an additional page.
-func (list ServiceList) Interpret(json interface{}) (gophercloud.LinkCollection, error) {
- mapped, ok := json.(map[string]interface{})
- if !ok {
- return nil, fmt.Errorf("Unexpected JSON response: %#v", json)
+// ExtractServices extracts a slice of Services from a Collection acquired from List.
+func ExtractServices(page gophercloud.Page) ([]Service, error) {
+ var response struct {
+ Services []Service `mapstructure:"services"`
}
- var result ServiceList
- err := mapstructure.Decode(mapped, &result)
- if err != nil {
- return nil, err
- }
- return result, nil
-}
-
-// AsServices extracts a slice of Services from a Collection acquired from List.
-// It panics if the Collection does not actually contain Services.
-func AsServices(results gophercloud.Collection) []Service {
- return results.(*ServiceList).Services
+ err := mapstructure.Decode(page.(gophercloud.LinkedPage).Body, &response)
+ return response.Services, err
}