Updating account results to make them more consistent
diff --git a/openstack/objectstorage/v1/accounts/requests.go b/openstack/objectstorage/v1/accounts/requests.go
index 2ba4744..5de09da 100644
--- a/openstack/objectstorage/v1/accounts/requests.go
+++ b/openstack/objectstorage/v1/accounts/requests.go
@@ -13,22 +13,29 @@
}
// Update is a function that creates, updates, or deletes an account's metadata.
-func Update(c *gophercloud.ServiceClient, opts UpdateOpts) error {
- h := c.Provider.AuthenticatedHeaders()
+func Update(c *gophercloud.ServiceClient, opts UpdateOpts) UpdateResult {
+ headers := c.Provider.AuthenticatedHeaders()
for k, v := range opts.Headers {
- h[k] = v
+ headers[k] = v
}
for k, v := range opts.Metadata {
- h["X-Account-Meta-"+k] = v
+ headers["X-Account-Meta-"+k] = v
}
- _, err := perigee.Request("POST", accountURL(c), perigee.Options{
- MoreHeaders: h,
+ var res UpdateResult
+
+ var resp *perigee.Response
+
+ resp, res.Err = perigee.Request("POST", accountURL(c), perigee.Options{
+ MoreHeaders: headers,
OkCodes: []int{204},
})
- return err
+
+ res.Resp = &resp.HttpResponse
+
+ return res
}
// GetOpts is a structure that contains parameters for getting an account's metadata.
@@ -38,16 +45,23 @@
// Get is a function that retrieves an account's metadata. To extract just the custom
// metadata, pass the GetResult response to the ExtractMetadata function.
-func Get(c *gophercloud.ServiceClient, opts GetOpts) (GetResult, error) {
- h := c.Provider.AuthenticatedHeaders()
+func Get(c *gophercloud.ServiceClient, opts GetOpts) GetResult {
+ headers := c.Provider.AuthenticatedHeaders()
for k, v := range opts.Headers {
- h[k] = v
+ headers[k] = v
}
- resp, err := perigee.Request("HEAD", accountURL(c), perigee.Options{
- MoreHeaders: h,
+ var res GetResult
+ var resp *perigee.Response
+
+ resp, res.Err = perigee.Request("HEAD", accountURL(c), perigee.Options{
+ MoreHeaders: headers,
+ Results: &res.Resp,
OkCodes: []int{204},
})
- return &resp.HttpResponse, err
+
+ res.Resp = &resp.HttpResponse
+
+ return res
}
diff --git a/openstack/objectstorage/v1/accounts/requests_test.go b/openstack/objectstorage/v1/accounts/requests_test.go
index ae9524f..8cb2be8 100644
--- a/openstack/objectstorage/v1/accounts/requests_test.go
+++ b/openstack/objectstorage/v1/accounts/requests_test.go
@@ -2,7 +2,6 @@
import (
"net/http"
- "reflect"
"testing"
"github.com/rackspace/gophercloud/testhelper"
@@ -19,13 +18,21 @@
testhelper.TestMethod(t, r, "POST")
testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
testhelper.TestHeader(t, r, "X-Account-Meta-Gophercloud-Test", "accounts")
+
+ w.Header().Set("X-Account-Container-Count", "2")
+ w.Header().Set("X-Account-Bytes-Used", "14")
+ w.Header().Set("X-Account-Meta-Subject", "books")
+
w.WriteHeader(http.StatusNoContent)
})
- err := Update(fake.ServiceClient(), UpdateOpts{Metadata: metadata})
- if err != nil {
- t.Fatalf("Unable to update account: %v", err)
- }
+ res := Update(fake.ServiceClient(), UpdateOpts{Metadata: metadata})
+
+ metadata := res.ExtractMetadata()
+ expected := map[string]string{"Subject": "books"}
+
+ testhelper.AssertDeepEquals(t, expected, metadata)
+ testhelper.AssertNoErr(t, res.Err)
}
func TestGetAccount(t *testing.T) {
@@ -35,23 +42,19 @@
testhelper.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
testhelper.TestMethod(t, r, "HEAD")
testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+
+ w.Header().Set("X-Account-Container-Count", "2")
+ w.Header().Set("X-Account-Bytes-Used", "14")
+ w.Header().Set("X-Account-Meta-Subject", "books")
+
w.WriteHeader(http.StatusNoContent)
})
- _, err := Get(fake.ServiceClient(), GetOpts{})
- if err != nil {
- t.Fatalf("Unable to get account metadata: %v", err)
- }
-}
+ res := Get(fake.ServiceClient(), GetOpts{})
-func TestExtractAccountMetadata(t *testing.T) {
- getResult := &http.Response{}
+ metadata := res.ExtractMetadata()
+ expected := map[string]string{"Subject": "books"}
- expected := map[string]string{}
-
- actual := ExtractMetadata(getResult)
-
- if !reflect.DeepEqual(expected, actual) {
- t.Errorf("Expected: %+v\nActual:%+v", expected, actual)
- }
+ testhelper.AssertDeepEquals(t, expected, metadata)
+ testhelper.AssertNoErr(t, res.Err)
}
diff --git a/openstack/objectstorage/v1/accounts/results.go b/openstack/objectstorage/v1/accounts/results.go
index 13a894b..8ec2eff 100644
--- a/openstack/objectstorage/v1/accounts/results.go
+++ b/openstack/objectstorage/v1/accounts/results.go
@@ -3,20 +3,36 @@
import (
"net/http"
"strings"
+
+ "github.com/rackspace/gophercloud"
)
-// GetResult is a *http.Response that is returned from a call to the Get function.
-type GetResult *http.Response
+type commonResult struct {
+ gophercloud.CommonResult
+ Resp *http.Response
+}
+
+// GetResult represents the result of a create operation.
+type GetResult struct {
+ commonResult
+}
+
+// UpdateResult represents the result of an update operation.
+type UpdateResult struct {
+ commonResult
+}
// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
// and returns the custom metatdata associated with the account.
-func ExtractMetadata(gr GetResult) map[string]string {
+func (res commonResult) ExtractMetadata() map[string]string {
metadata := make(map[string]string)
- for k, v := range gr.Header {
+
+ for k, v := range res.Resp.Header {
if strings.HasPrefix(k, "X-Account-Meta-") {
key := strings.TrimPrefix(k, "X-Account-Meta-")
metadata[key] = v[0]
}
}
+
return metadata
}