Accounts updated unit tests; common ExtractHeaders method
diff --git a/openstack/objectstorage/v1/accounts/requests.go b/openstack/objectstorage/v1/accounts/requests.go
index 5de09da..b30439c 100644
--- a/openstack/objectstorage/v1/accounts/requests.go
+++ b/openstack/objectstorage/v1/accounts/requests.go
@@ -5,63 +5,75 @@
"github.com/rackspace/gophercloud"
)
-// UpdateOpts is a structure that contains parameters for updating, creating, or deleting an
-// account's metadata.
-type UpdateOpts struct {
- Metadata map[string]string
- Headers map[string]string
-}
-
-// Update is a function that creates, updates, or deletes an account's metadata.
-func Update(c *gophercloud.ServiceClient, opts UpdateOpts) UpdateResult {
- headers := c.Provider.AuthenticatedHeaders()
-
- for k, v := range opts.Headers {
- headers[k] = v
- }
-
- for k, v := range opts.Metadata {
- headers["X-Account-Meta-"+k] = v
- }
-
- var res UpdateResult
-
- var resp *perigee.Response
-
- resp, res.Err = perigee.Request("POST", accountURL(c), perigee.Options{
- MoreHeaders: headers,
- OkCodes: []int{204},
- })
-
- res.Resp = &resp.HttpResponse
-
- return res
-}
-
// GetOpts is a structure that contains parameters for getting an account's metadata.
type GetOpts struct {
- Headers map[string]string
+ Newest bool `h:"X-Newest"`
}
// 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 {
- headers := c.Provider.AuthenticatedHeaders()
+// metadata, call the ExtractMetadata method on the GetResult. To extract all the headers that are
+// returned (including the metadata), call the ExtractHeaders method on the GetResult.
+func Get(c *gophercloud.ServiceClient, opts *GetOpts) GetResult {
+ var res GetResult
+ h := c.Provider.AuthenticatedHeaders()
- for k, v := range opts.Headers {
- headers[k] = v
+ if opts != nil {
+ headers, err := gophercloud.BuildHeaders(opts)
+ if err != nil {
+ res.Err = err
+ return res
+ }
+
+ for k, v := range headers {
+ h[k] = v
+ }
}
- var res GetResult
- var resp *perigee.Response
-
- resp, res.Err = perigee.Request("HEAD", accountURL(c), perigee.Options{
- MoreHeaders: headers,
- Results: &res.Resp,
+ resp, err := perigee.Request("HEAD", getURL(c), perigee.Options{
+ MoreHeaders: h,
OkCodes: []int{204},
})
-
res.Resp = &resp.HttpResponse
+ res.Err = err
+ return res
+}
+// UpdateOpts is a structure that contains parameters for updating, creating, or deleting an
+// account's metadata.
+type UpdateOpts struct {
+ Metadata map[string]string
+ ContentType string `h:"Content-Type"`
+ DetectContentType bool `h:"X-Detect-Content-Type"`
+ TempURLKey string `h:"X-Account-Meta-Temp-URL-Key"`
+ TempURLKey2 string `h:"X-Account-Meta-Temp-URL-Key-2"`
+}
+
+// Update is a function that creates, updates, or deletes an account's metadata. To extract the
+// headers returned, call the ExtractHeaders method on the UpdateResult.
+func Update(c *gophercloud.ServiceClient, opts *UpdateOpts) UpdateResult {
+ var res UpdateResult
+ h := c.Provider.AuthenticatedHeaders()
+
+ if opts != nil {
+ headers, err := gophercloud.BuildHeaders(opts)
+ if err != nil {
+ res.Err = err
+ return res
+ }
+ for k, v := range headers {
+ h[k] = v
+ }
+
+ for k, v := range opts.Metadata {
+ h["X-Account-Meta-"+k] = v
+ }
+ }
+
+ resp, err := perigee.Request("POST", updateURL(c), perigee.Options{
+ MoreHeaders: h,
+ OkCodes: []int{204},
+ })
+ res.Resp = &resp.HttpResponse
+ res.Err = err
return res
}
diff --git a/openstack/objectstorage/v1/accounts/requests_test.go b/openstack/objectstorage/v1/accounts/requests_test.go
index 8cb2be8..0090eea 100644
--- a/openstack/objectstorage/v1/accounts/requests_test.go
+++ b/openstack/objectstorage/v1/accounts/requests_test.go
@@ -4,20 +4,20 @@
"net/http"
"testing"
- "github.com/rackspace/gophercloud/testhelper"
+ th "github.com/rackspace/gophercloud/testhelper"
fake "github.com/rackspace/gophercloud/testhelper/client"
)
var metadata = map[string]string{"gophercloud-test": "accounts"}
func TestUpdateAccount(t *testing.T) {
- testhelper.SetupHTTP()
- defer testhelper.TeardownHTTP()
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
- testhelper.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
- testhelper.TestMethod(t, r, "POST")
- testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- testhelper.TestHeader(t, r, "X-Account-Meta-Gophercloud-Test", "accounts")
+ th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "POST")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.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")
@@ -26,35 +26,28 @@
w.WriteHeader(http.StatusNoContent)
})
- 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)
+ options := &UpdateOpts{Metadata: map[string]string{"gophercloud-test": "accounts"}}
+ _, err := Update(fake.ServiceClient(), options).ExtractHeaders()
+ if err != nil {
+ t.Fatalf("Unable to update account: %v", err)
+ }
}
func TestGetAccount(t *testing.T) {
- testhelper.SetupHTTP()
- defer testhelper.TeardownHTTP()
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
- 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")
-
+ th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "HEAD")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ w.Header().Set("X-Account-Meta-Foo", "bar")
w.WriteHeader(http.StatusNoContent)
})
- res := Get(fake.ServiceClient(), GetOpts{})
-
- metadata := res.ExtractMetadata()
- expected := map[string]string{"Subject": "books"}
-
- testhelper.AssertDeepEquals(t, expected, metadata)
- testhelper.AssertNoErr(t, res.Err)
+ expected := map[string]string{"Foo": "bar"}
+ actual, err := Get(fake.ServiceClient(), &GetOpts{}).ExtractMetadata()
+ if err != nil {
+ t.Fatalf("Unable to get account metadata: %v", err)
+ }
+ th.CheckDeepEquals(t, expected, actual)
}
diff --git a/openstack/objectstorage/v1/accounts/results.go b/openstack/objectstorage/v1/accounts/results.go
index 8ec2eff..8ff8183 100644
--- a/openstack/objectstorage/v1/accounts/results.go
+++ b/openstack/objectstorage/v1/accounts/results.go
@@ -1,38 +1,34 @@
package accounts
import (
- "net/http"
"strings"
- "github.com/rackspace/gophercloud"
+ objectstorage "github.com/rackspace/gophercloud/openstack/objectstorage/v1"
)
-type commonResult struct {
- gophercloud.CommonResult
- Resp *http.Response
-}
-
-// GetResult represents the result of a create operation.
+// GetResult is returned from a call to the Get function. See v1.CommonResult.
type GetResult struct {
- commonResult
-}
-
-// UpdateResult represents the result of an update operation.
-type UpdateResult struct {
- commonResult
+ objectstorage.CommonResult
}
// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
// and returns the custom metatdata associated with the account.
-func (res commonResult) ExtractMetadata() map[string]string {
- metadata := make(map[string]string)
+func (gr GetResult) ExtractMetadata() (map[string]string, error) {
+ if gr.Err != nil {
+ return nil, gr.Err
+ }
- for k, v := range res.Resp.Header {
+ metadata := make(map[string]string)
+ for k, v := range gr.Resp.Header {
if strings.HasPrefix(k, "X-Account-Meta-") {
key := strings.TrimPrefix(k, "X-Account-Meta-")
metadata[key] = v[0]
}
}
+ return metadata, nil
+}
- return metadata
+// UpdateResult is returned from a call to the Update function. See v1.CommonResult.
+type UpdateResult struct {
+ objectstorage.CommonResult
}
diff --git a/openstack/objectstorage/v1/accounts/urls.go b/openstack/objectstorage/v1/accounts/urls.go
index 53b1343..9952fe4 100644
--- a/openstack/objectstorage/v1/accounts/urls.go
+++ b/openstack/objectstorage/v1/accounts/urls.go
@@ -2,7 +2,10 @@
import "github.com/rackspace/gophercloud"
-// accountURL returns the URI for making Account requests.
-func accountURL(c *gophercloud.ServiceClient) string {
+func getURL(c *gophercloud.ServiceClient) string {
return c.Endpoint
}
+
+func updateURL(c *gophercloud.ServiceClient) string {
+ return getURL(c)
+}
diff --git a/openstack/objectstorage/v1/accounts/urls_test.go b/openstack/objectstorage/v1/accounts/urls_test.go
index f127a5e..074d52d 100644
--- a/openstack/objectstorage/v1/accounts/urls_test.go
+++ b/openstack/objectstorage/v1/accounts/urls_test.go
@@ -4,14 +4,23 @@
"testing"
"github.com/rackspace/gophercloud"
+ th "github.com/rackspace/gophercloud/testhelper"
)
-func TestAccountURL(t *testing.T) {
- client := gophercloud.ServiceClient{
- Endpoint: "http://localhost:5000/v3/",
- }
- url := accountURL(&client)
- if url != "http://localhost:5000/v3/" {
- t.Errorf("Unexpected service URL generated: [%s]", url)
- }
+const endpoint = "http://localhost:57909/"
+
+func endpointClient() *gophercloud.ServiceClient {
+ return &gophercloud.ServiceClient{Endpoint: endpoint}
+}
+
+func TestGetURL(t *testing.T) {
+ actual := getURL(endpointClient())
+ expected := endpoint
+ th.CheckEquals(t, expected, actual)
+}
+
+func TestUpdateURL(t *testing.T) {
+ actual := updateURL(endpointClient())
+ expected := endpoint
+ th.CheckEquals(t, expected, actual)
}
diff --git a/openstack/objectstorage/v1/common.go b/openstack/objectstorage/v1/common.go
new file mode 100644
index 0000000..1a6c44a
--- /dev/null
+++ b/openstack/objectstorage/v1/common.go
@@ -0,0 +1,25 @@
+package v1
+
+import (
+ "net/http"
+)
+
+// CommonResult is a structure that contains the response and error of a call to an
+// object storage endpoint.
+type CommonResult struct {
+ Resp *http.Response
+ Err error
+}
+
+// ExtractHeaders will extract and return the headers from a *http.Response.
+func (cr CommonResult) ExtractHeaders() (http.Header, error) {
+ if cr.Err != nil {
+ return nil, cr.Err
+ }
+
+ var headers http.Header
+ if cr.Err != nil {
+ return headers, cr.Err
+ }
+ return cr.Resp.Header, nil
+}