Paginate the results of containers.List().
diff --git a/openstack/storage/v1/containers/containers.go b/openstack/storage/v1/containers/containers.go
index 3a00647..8fa19aa 100644
--- a/openstack/storage/v1/containers/containers.go
+++ b/openstack/storage/v1/containers/containers.go
@@ -1,9 +1,10 @@
package containers
import (
- "encoding/json"
- "io/ioutil"
+ "fmt"
"strings"
+
+ "github.com/rackspace/gophercloud/pagination"
)
// Container is a structure that holds information related to a storage container.
@@ -42,32 +43,47 @@
Metadata map[string]string
}
-// ExtractInfo is a function that takes a ListResult (of type *http.Response)
-// and returns the containers' information.
-func ExtractInfo(lr ListResult) ([]Container, error) {
- var ci []Container
- defer lr.Body.Close()
- body, err := ioutil.ReadAll(lr.Body)
- if err != nil {
- return ci, err
+// ExtractInfo is a function that takes a ListResult and returns the containers' information.
+func ExtractInfo(page pagination.Page) ([]Container, error) {
+ untyped := page.(ListResult).Body.([]interface{})
+ results := make([]Container, len(untyped))
+ for index, each := range untyped {
+ results[index] = Container(each.(map[string]interface{}))
}
- err = json.Unmarshal(body, &ci)
- return ci, err
+ return results, nil
}
-// ExtractNames is a function that takes a ListResult (of type *http.Response)
-// and returns the containers' names.
-func ExtractNames(lr ListResult) ([]string, error) {
- var cns []string
- defer lr.Body.Close()
- body, err := ioutil.ReadAll(lr.Body)
- if err != nil {
- return cns, err
+// ExtractNames is a function that takes a ListResult and returns the containers' names.
+func ExtractNames(page pagination.Page) ([]string, error) {
+ casted := page.(ListResult)
+ ct := casted.Header.Get("Content-Type")
+
+ switch {
+ case strings.HasPrefix(ct, "application/json"):
+ parsed, err := ExtractInfo(page)
+ if err != nil {
+ return nil, err
+ }
+
+ names := make([]string, 0, len(parsed))
+ for _, container := range parsed {
+ names = append(names, container["name"].(string))
+ }
+ return names, nil
+ case strings.HasPrefix(ct, "text/plain"):
+ names := make([]string, 0, 50)
+
+ body := string(page.(ListResult).Body.([]uint8))
+ for _, name := range strings.Split(body, "\n") {
+ if len(name) > 0 {
+ names = append(names, name)
+ }
+ }
+
+ return names, nil
+ default:
+ return nil, fmt.Errorf("Cannot extract names from response with content-type: [%s]", ct)
}
- jr := string(body)
- cns = strings.Split(jr, "\n")
- cns = cns[:len(cns)-1]
- return cns, nil
}
// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
diff --git a/openstack/storage/v1/containers/requests.go b/openstack/storage/v1/containers/requests.go
index 0db691c..2aa263a 100644
--- a/openstack/storage/v1/containers/requests.go
+++ b/openstack/storage/v1/containers/requests.go
@@ -6,10 +6,34 @@
"github.com/racker/perigee"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack/utils"
+ "github.com/rackspace/gophercloud/pagination"
)
// ListResult is a *http.Response that is returned from a call to the List function.
-type ListResult *http.Response
+type ListResult struct {
+ pagination.MarkerPageBase
+}
+
+// IsEmpty returns true if a ListResult contains no container names.
+func (r ListResult) IsEmpty() (bool, error) {
+ names, err := ExtractNames(r)
+ if err != nil {
+ return true, err
+ }
+ return len(names) == 0, nil
+}
+
+// LastMarker returns the last container name in a ListResult.
+func (r ListResult) LastMarker() (string, error) {
+ names, err := ExtractNames(r)
+ if err != nil {
+ return "", err
+ }
+ if len(names) == 0 {
+ return "", nil
+ }
+ return names[len(names)-1], nil
+}
// GetResult is a *http.Response that is returned from a call to the Get function.
type GetResult *http.Response
@@ -17,24 +41,25 @@
// List is a function that retrieves all objects in a container. It also returns the details
// for the account. To extract just the container information or names, pass the ListResult
// response to the ExtractInfo or ExtractNames function, respectively.
-func List(c *gophercloud.ServiceClient, opts ListOpts) (ListResult, error) {
- contentType := ""
-
- h := c.Provider.AuthenticatedHeaders()
+func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
+ var headers map[string]string
query := utils.BuildQuery(opts.Params)
if !opts.Full {
- contentType = "text/plain"
+ headers = map[string]string{"Content-Type": "text/plain"}
+ }
+
+ createPage := func(r pagination.LastHTTPResponse) pagination.Page {
+ p := ListResult{pagination.MarkerPageBase{LastHTTPResponse: r}}
+ p.MarkerPageBase.Owner = p
+ return p
}
url := getAccountURL(c) + query
- resp, err := perigee.Request("GET", url, perigee.Options{
- MoreHeaders: h,
- Accept: contentType,
- OkCodes: []int{200, 204},
- })
- return &resp.HttpResponse, err
+ pager := pagination.NewPager(c, url, createPage)
+ pager.Headers = headers
+ return pager
}
// Create is a function that creates a new container.