add commonResult types for ExtractHeaders method
diff --git a/openstack/objectstorage/v1/containers/requests.go b/openstack/objectstorage/v1/containers/requests.go
index 8b54c81..d772a43 100644
--- a/openstack/objectstorage/v1/containers/requests.go
+++ b/openstack/objectstorage/v1/containers/requests.go
@@ -9,17 +9,16 @@
// ListOpts is a structure that holds options for listing containers.
type ListOpts struct {
Full bool
- Limit int `q:"limit"`
- Marker string `q:"marker"`
- EndMarker string `q:"end_marker"`
- Format string `q:"format"`
- Prefix string `q:"prefix"`
- Delimiter []byte `q:"delimiter"`
+ Limit int `q:"limit"`
+ Marker string `q:"marker"`
+ EndMarker string `q:"end_marker"`
+ Format string `q:"format"`
+ Prefix string `q:"prefix"`
+ Delimiter [1]byte `q:"delimiter"`
}
-// 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.
+// List is a function that retrieves containers associated with the account as well as account
+// metadata. It returns a pager which can be iterated with the EachPage function.
func List(c *gophercloud.ServiceClient, opts *ListOpts) pagination.Pager {
var headers map[string]string
@@ -34,6 +33,8 @@
if !opts.Full {
headers = map[string]string{"Accept": "text/plain", "Content-Type": "text/plain"}
}
+ } else {
+ headers = map[string]string{"Accept": "text/plain", "Content-Type": "text/plain"}
}
createPage := func(r pagination.LastHTTPResponse) pagination.Page {
@@ -61,14 +62,15 @@
}
// Create is a function that creates a new container.
-func Create(c *gophercloud.ServiceClient, containerName string, opts *CreateOpts) (Container, error) {
- var container Container
+func Create(c *gophercloud.ServiceClient, containerName string, opts *CreateOpts) CreateResult {
+ var res CreateResult
h := c.Provider.AuthenticatedHeaders()
if opts != nil {
headers, err := gophercloud.BuildHeaders(opts)
if err != nil {
- return container, err
+ res.Err = err
+ return res
}
for k, v := range headers {
@@ -80,23 +82,25 @@
}
}
- _, err := perigee.Request("PUT", containerURL(c, containerName), perigee.Options{
+ resp, err := perigee.Request("PUT", containerURL(c, containerName), perigee.Options{
MoreHeaders: h,
OkCodes: []int{201, 204},
})
- if err == nil {
- container = Container{Name: containerName}
- }
- return container, err
+ res.Resp = &resp.HttpResponse
+ res.Err = err
+ return res
}
// Delete is a function that deletes a container.
-func Delete(c *gophercloud.ServiceClient, containerName string) error {
- _, err := perigee.Request("DELETE", containerURL(c, containerName), perigee.Options{
+func Delete(c *gophercloud.ServiceClient, containerName string) DeleteResult {
+ var res DeleteResult
+ resp, err := perigee.Request("DELETE", containerURL(c, containerName), perigee.Options{
MoreHeaders: c.Provider.AuthenticatedHeaders(),
OkCodes: []int{204},
})
- return err
+ res.Resp = &resp.HttpResponse
+ res.Err = err
+ return res
}
// UpdateOpts is a structure that holds parameters for updating, creating, or deleting a
@@ -114,13 +118,15 @@
}
// Update is a function that creates, updates, or deletes a container's metadata.
-func Update(c *gophercloud.ServiceClient, containerName string, opts *UpdateOpts) error {
+func Update(c *gophercloud.ServiceClient, containerName string, opts *UpdateOpts) UpdateResult {
+ var res UpdateResult
h := c.Provider.AuthenticatedHeaders()
if opts != nil {
headers, err := gophercloud.BuildHeaders(opts)
if err != nil {
- return err
+ res.Err = err
+ return res
}
for k, v := range headers {
@@ -132,22 +138,24 @@
}
}
- _, err := perigee.Request("POST", containerURL(c, containerName), perigee.Options{
+ resp, err := perigee.Request("POST", containerURL(c, containerName), perigee.Options{
MoreHeaders: h,
OkCodes: []int{204},
})
- return err
+ res.Resp = &resp.HttpResponse
+ res.Err = err
+ return res
}
// Get is a function that retrieves the metadata of a container. To extract just the custom
// metadata, pass the GetResult response to the ExtractMetadata function.
func Get(c *gophercloud.ServiceClient, containerName string) GetResult {
- var gr GetResult
+ var res GetResult
resp, err := perigee.Request("HEAD", containerURL(c, containerName), perigee.Options{
MoreHeaders: c.Provider.AuthenticatedHeaders(),
OkCodes: []int{204},
})
- gr.Err = err
- gr.Resp = &resp.HttpResponse
- return gr
+ res.Resp = &resp.HttpResponse
+ res.Err = err
+ return res
}
diff --git a/openstack/objectstorage/v1/containers/requests_test.go b/openstack/objectstorage/v1/containers/requests_test.go
index 8ec1733..1ab09a9 100644
--- a/openstack/objectstorage/v1/containers/requests_test.go
+++ b/openstack/objectstorage/v1/containers/requests_test.go
@@ -145,7 +145,7 @@
})
client := serviceClient()
- _, err := Create(client, "testContainer", nil)
+ _, err := Create(client, "testContainer", nil).ExtractHeaders()
if err != nil {
t.Fatalf("Unexpected error creating container: %v", err)
}
@@ -163,7 +163,7 @@
})
client := serviceClient()
- err := Delete(client, "testContainer")
+ _, err := Delete(client, "testContainer").ExtractHeaders()
if err != nil {
t.Fatalf("Unexpected error deleting container: %v", err)
}
@@ -181,7 +181,7 @@
})
client := serviceClient()
- err := Update(client, "testContainer", nil)
+ _, err := Update(client, "testContainer", nil).ExtractHeaders()
if err != nil {
t.Fatalf("Unexpected error updating container metadata: %v", err)
}
diff --git a/openstack/objectstorage/v1/containers/results.go b/openstack/objectstorage/v1/containers/results.go
index 5df99ed..80425bd 100644
--- a/openstack/objectstorage/v1/containers/results.go
+++ b/openstack/objectstorage/v1/containers/results.go
@@ -2,56 +2,19 @@
import (
"fmt"
- "github.com/mitchellh/mapstructure"
- "github.com/rackspace/gophercloud"
- "github.com/rackspace/gophercloud/pagination"
"net/http"
"strings"
+
+ "github.com/mitchellh/mapstructure"
+ "github.com/rackspace/gophercloud/pagination"
)
type Container struct {
Bytes int `json:"bytes" mapstructure:"bytes"`
Count int `json:"count" mapstructure:"count"`
- Name string `json:"name" mapstructure:"name"`
+ Name string `json:"name" mapstructure:"name"`
}
-type commonResult struct {
- gophercloud.CommonResult
-}
-
-func (r GetResult) Extract() (*Container, error) {
- if r.Err != nil {
- return nil, r.Err
- }
-
- var res struct {
- Container *Container
- }
-
- err := mapstructure.Decode(r.Resp, &res)
- if err != nil {
- return nil, fmt.Errorf("Error decoding Object Storage Container: %v", err)
- }
-
- return res.Container, nil
-}
-
-type CreateResult struct {
- commonResult
-}
-
-// GetResult represents the result of a get operation.
-type GetResult struct {
- Resp *http.Response
- Err error
-}
-
-// UpdateResult represents the result of an update operation.
-type UpdateResult commonResult
-
-// DeleteResult represents the result of a delete operation.
-type DeleteResult commonResult
-
// ListResult is a *http.Response that is returned from a call to the List function.
type ContainerPage struct {
pagination.MarkerPageBase
@@ -125,6 +88,12 @@
}
}
+// GetResult represents the result of a get operation.
+type GetResult struct {
+ Resp *http.Response
+ Err error
+}
+
// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
// and returns the custom metadata associated with the container.
func (gr GetResult) ExtractMetadata() (map[string]string, error) {
@@ -140,3 +109,29 @@
}
return metadata, nil
}
+
+type commonResult struct {
+ Resp *http.Response
+ Err error
+}
+
+func (cr commonResult) ExtractHeaders() (http.Header, error) {
+ var headers http.Header
+ if cr.Err != nil {
+ return headers, cr.Err
+ }
+
+ return cr.Resp.Header, nil
+}
+
+type CreateResult struct {
+ commonResult
+}
+
+type UpdateResult struct {
+ commonResult
+}
+
+type DeleteResult struct {
+ commonResult
+}