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
+}
diff --git a/openstack/objectstorage/v1/objects/requests.go b/openstack/objectstorage/v1/objects/requests.go
index 900290c..bc21496 100644
--- a/openstack/objectstorage/v1/objects/requests.go
+++ b/openstack/objectstorage/v1/objects/requests.go
@@ -40,6 +40,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 {
@@ -69,7 +71,7 @@
// To extract just the content, pass the DownloadResult response to the ExtractContent
// function.
func Download(c *gophercloud.ServiceClient, containerName, objectName string, opts *DownloadOpts) DownloadResult {
- var dr DownloadResult
+ var res DownloadResult
url := objectURL(c, containerName, objectName)
h := c.Provider.AuthenticatedHeaders()
@@ -77,8 +79,8 @@
if opts != nil {
headers, err := gophercloud.BuildHeaders(opts)
if err != nil {
- dr.Err = err
- return dr
+ res.Err = err
+ return res
}
for k, v := range headers {
@@ -87,8 +89,8 @@
query, err := gophercloud.BuildQueryString(opts)
if err != nil {
- dr.Err = err
- return dr
+ res.Err = err
+ return res
}
url += query.String()
}
@@ -97,9 +99,9 @@
MoreHeaders: h,
OkCodes: []int{200},
})
- dr.Err = err
- dr.Resp = &resp.HttpResponse
- return dr
+ res.Err = err
+ res.Resp = &resp.HttpResponse
+ return res
}
// CreateOpts is a structure that holds parameters for creating an object.
@@ -123,7 +125,8 @@
}
// Create is a function that creates a new object or replaces an existing object.
-func Create(c *gophercloud.ServiceClient, containerName, objectName string, content io.Reader, opts *CreateOpts) error {
+func Create(c *gophercloud.ServiceClient, containerName, objectName string, content io.Reader, opts *CreateOpts) CreateResult {
+ var res CreateResult
var reqBody []byte
url := objectURL(c, containerName, objectName)
@@ -132,7 +135,8 @@
if opts != nil {
headers, err := gophercloud.BuildHeaders(opts)
if err != nil {
- return nil
+ res.Err = err
+ return res
}
for k, v := range headers {
@@ -145,8 +149,10 @@
query, err := gophercloud.BuildQueryString(opts)
if err != nil {
- return err
+ res.Err = err
+ return res
}
+
url += query.String()
}
@@ -154,16 +160,19 @@
reqBody = make([]byte, 0)
_, err := content.Read(reqBody)
if err != nil {
- return err
+ res.Err = err
+ return res
}
}
- _, err := perigee.Request("PUT", url, perigee.Options{
+ resp, err := perigee.Request("PUT", url, perigee.Options{
ReqBody: reqBody,
MoreHeaders: h,
OkCodes: []int{201},
})
- return err
+ res.Resp = &resp.HttpResponse
+ res.Err = err
+ return res
}
// CopyOpts is a structure that holds parameters for copying one object to another.
@@ -176,15 +185,18 @@
}
// Copy is a function that copies one object to another.
-func Copy(c *gophercloud.ServiceClient, containerName, objectName string, opts *CopyOpts) error {
+func Copy(c *gophercloud.ServiceClient, containerName, objectName string, opts *CopyOpts) CopyResult {
+ var res CopyResult
h := c.Provider.AuthenticatedHeaders()
if opts == nil {
- return fmt.Errorf("Required CopyOpts field 'Destination' not set.")
+ res.Err = fmt.Errorf("Required CopyOpts field 'Destination' not set.")
+ return res
}
headers, err := gophercloud.BuildHeaders(opts)
if err != nil {
- return err
+ res.Err = err
+ return res
}
for k, v := range headers {
h[k] = v
@@ -195,11 +207,12 @@
}
url := objectURL(c, containerName, objectName)
- _, err = perigee.Request("COPY", url, perigee.Options{
+ resp, err := perigee.Request("COPY", url, perigee.Options{
MoreHeaders: h,
OkCodes: []int{201},
})
- return err
+ res.Resp = &resp.HttpResponse
+ return res
}
// DeleteOpts is a structure that holds parameters for deleting an object.
@@ -208,22 +221,26 @@
}
// Delete is a function that deletes an object.
-func Delete(c *gophercloud.ServiceClient, containerName, objectName string, opts *DeleteOpts) error {
+func Delete(c *gophercloud.ServiceClient, containerName, objectName string, opts *DeleteOpts) DeleteResult {
+ var res DeleteResult
url := objectURL(c, containerName, objectName)
if opts != nil {
query, err := gophercloud.BuildQueryString(opts)
if err != nil {
- return err
+ res.Err = err
+ return res
}
url += query.String()
}
- _, err := perigee.Request("DELETE", url, perigee.Options{
+ resp, err := perigee.Request("DELETE", url, perigee.Options{
MoreHeaders: c.Provider.AuthenticatedHeaders(),
OkCodes: []int{204},
})
- return err
+ res.Resp = &resp.HttpResponse
+ res.Err = err
+ return res
}
// GetOpts is a structure that holds parameters for getting an object's metadata.
@@ -235,14 +252,14 @@
// Get is a function that retrieves the metadata of an object. To extract just the custom
// metadata, pass the GetResult response to the ExtractMetadata function.
func Get(c *gophercloud.ServiceClient, containerName, objectName string, opts *GetOpts) GetResult {
- var gr GetResult
+ var res GetResult
url := objectURL(c, containerName, objectName)
if opts != nil {
query, err := gophercloud.BuildQueryString(opts)
if err != nil {
- gr.Err = err
- return gr
+ res.Err = err
+ return res
}
url += query.String()
}
@@ -251,9 +268,9 @@
MoreHeaders: c.Provider.AuthenticatedHeaders(),
OkCodes: []int{200, 204},
})
- gr.Err = err
- gr.Resp = &resp.HttpResponse
- return gr
+ res.Err = err
+ res.Resp = &resp.HttpResponse
+ return res
}
// UpdateOpts is a structure that holds parameters for updating, creating, or deleting an
@@ -269,13 +286,15 @@
}
// Update is a function that creates, updates, or deletes an object's metadata.
-func Update(c *gophercloud.ServiceClient, containerName, objectName string, opts *UpdateOpts) error {
+func Update(c *gophercloud.ServiceClient, containerName, objectName string, opts *UpdateOpts) UpdateResult {
+ var res UpdateResult
h := c.Provider.AuthenticatedHeaders()
if opts != nil {
headers, err := gophercloud.BuildHeaders(opts)
if err != nil {
- return nil
+ res.Err = err
+ return res
}
for k, v := range headers {
@@ -288,9 +307,11 @@
}
url := objectURL(c, containerName, objectName)
- _, err := perigee.Request("POST", url, perigee.Options{
+ resp, err := perigee.Request("POST", url, perigee.Options{
MoreHeaders: h,
OkCodes: []int{202},
})
- return err
+ res.Resp = &resp.HttpResponse
+ res.Err = err
+ return res
}
diff --git a/openstack/objectstorage/v1/objects/requests_test.go b/openstack/objectstorage/v1/objects/requests_test.go
index 6747544..15956cd 100644
--- a/openstack/objectstorage/v1/objects/requests_test.go
+++ b/openstack/objectstorage/v1/objects/requests_test.go
@@ -180,7 +180,7 @@
client := serviceClient()
content := bytes.NewBufferString("Did gyre and gimble in the wabe")
- err := Create(client, "testContainer", "testObject", content, nil)
+ _, err := Create(client, "testContainer", "testObject", content, nil).ExtractHeaders()
if err != nil {
t.Fatalf("Unexpected error creating object: %v", err)
}
@@ -199,7 +199,7 @@
})
client := serviceClient()
- err := Copy(client, "testContainer", "testObject", &CopyOpts{Destination: "/newTestContainer/newTestObject"})
+ _, err := Copy(client, "testContainer", "testObject", &CopyOpts{Destination: "/newTestContainer/newTestObject"}).ExtractHeaders()
if err != nil {
t.Fatalf("Unexpected error copying object: %v", err)
}
@@ -217,7 +217,7 @@
})
client := serviceClient()
- err := Delete(client, "testContainer", "testObject", nil)
+ _, err := Delete(client, "testContainer", "testObject", nil).ExtractHeaders()
if err != nil {
t.Fatalf("Unexpected error deleting object: %v", err)
}
@@ -236,7 +236,7 @@
})
client := serviceClient()
- err := Update(client, "testContainer", "testObject", &UpdateOpts{Metadata: metadata})
+ _, err := Update(client, "testContainer", "testObject", &UpdateOpts{Metadata: metadata}).ExtractHeaders()
if err != nil {
t.Fatalf("Unexpected error updating object metadata: %v", err)
}
diff --git a/openstack/objectstorage/v1/objects/results.go b/openstack/objectstorage/v1/objects/results.go
index cab0dfb..aaeb040 100644
--- a/openstack/objectstorage/v1/objects/results.go
+++ b/openstack/objectstorage/v1/objects/results.go
@@ -45,18 +45,6 @@
return names[len(names)-1], nil
}
-// DownloadResult is a *http.Response that is returned from a call to the Download function.
-type DownloadResult struct {
- Resp *http.Response
- Err error
-}
-
-// GetResult is a *http.Response that is returned from a call to the Get function.
-type GetResult struct {
- Resp *http.Response
- Err error
-}
-
// ExtractInfo is a function that takes a page of objects and returns their full information.
func ExtractInfo(page pagination.Page) ([]Object, error) {
untyped := page.(ObjectPage).Body.([]interface{})
@@ -106,6 +94,11 @@
}
}
+// DownloadResult is a *http.Response that is returned from a call to the Download function.
+type DownloadResult struct {
+ commonResult
+}
+
// ExtractContent is a function that takes a DownloadResult (of type *http.Response)
// and returns the object's content.
func (dr DownloadResult) ExtractContent() ([]byte, error) {
@@ -121,6 +114,11 @@
return body, nil
}
+// GetResult is a *http.Response that is returned from a call to the Get function.
+type GetResult struct {
+ commonResult
+}
+
// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
// and returns the custom metadata associated with the object.
func (gr GetResult) ExtractMetadata() (map[string]string, error) {
@@ -136,3 +134,33 @@
}
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
+}
+
+type CopyResult struct {
+ commonResult
+}