Extract method for Accounts
diff --git a/openstack/objectstorage/v1/accounts/fixtures.go b/openstack/objectstorage/v1/accounts/fixtures.go
index 3dad0c5..f22b687 100644
--- a/openstack/objectstorage/v1/accounts/fixtures.go
+++ b/openstack/objectstorage/v1/accounts/fixtures.go
@@ -14,9 +14,8 @@
// responds with a `Get` response.
func HandleGetAccountSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "POST")
+ th.TestMethod(t, r, "HEAD")
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")
@@ -30,9 +29,10 @@
// responds with a `Update` response.
func HandleUpdateAccountSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "HEAD")
+ th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- w.Header().Set("X-Account-Meta-Foo", "bar")
+ th.TestHeader(t, r, "X-Account-Meta-Gophercloud-Test", "accounts")
+
w.WriteHeader(http.StatusNoContent)
})
}
diff --git a/openstack/objectstorage/v1/accounts/requests_test.go b/openstack/objectstorage/v1/accounts/requests_test.go
index d6dc26b..6454c0a 100644
--- a/openstack/objectstorage/v1/accounts/requests_test.go
+++ b/openstack/objectstorage/v1/accounts/requests_test.go
@@ -7,12 +7,10 @@
fake "github.com/rackspace/gophercloud/testhelper/client"
)
-var metadata = map[string]string{"gophercloud-test": "accounts"}
-
func TestUpdateAccount(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
- HandleGetAccountSuccessfully(t)
+ HandleUpdateAccountSuccessfully(t)
options := &UpdateOpts{Metadata: map[string]string{"gophercloud-test": "accounts"}}
res := Update(fake.ServiceClient(), options)
@@ -22,12 +20,13 @@
func TestGetAccount(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
- HandleUpdateAccountSuccessfully(t)
+ HandleGetAccountSuccessfully(t)
- 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)
+ expectedMetadata := map[string]string{"Subject": "books"}
+ res := Get(fake.ServiceClient(), &GetOpts{})
+ th.AssertNoErr(t, res.Err)
+ actualMetadata, _ := res.ExtractMetadata()
+ th.CheckDeepEquals(t, expectedMetadata, actualMetadata)
+ //headers, err := res.Extract()
+ //th.AssertNoErr(t, err)
}
diff --git a/openstack/objectstorage/v1/accounts/results.go b/openstack/objectstorage/v1/accounts/results.go
index abae026..4797e96 100644
--- a/openstack/objectstorage/v1/accounts/results.go
+++ b/openstack/objectstorage/v1/accounts/results.go
@@ -1,16 +1,92 @@
package accounts
import (
+ "encoding/json"
+ "fmt"
"strings"
+ "time"
+ "github.com/mitchellh/mapstructure"
"github.com/rackspace/gophercloud"
)
+// UpdateResult is returned from a call to the Update function.
+type UpdateResult struct {
+ gophercloud.HeaderResult
+}
+
+// UpdateHeader represents the headers returned in the response from an Update request.
+type UpdateHeader struct {
+ ContentLength string `json:"Content-Length"`
+ ContentType []string `json:"Content-Type"`
+ Date time.Time `json:"-"`
+ TransID string `json:"X-Trans_ID"`
+}
+
+// Extract will return a struct of headers returned from a call to Get. To obtain
+// a map of headers, call the ExtractHeader method on the GetResult.
+func (ur UpdateResult) Extract() (UpdateHeader, error) {
+ var uh UpdateHeader
+ if ur.Err != nil {
+ return uh, ur.Err
+ }
+
+ b, err := json.Marshal(ur.Header)
+ if err != nil {
+ return uh, err
+ }
+
+ err = json.Unmarshal(b, &uh)
+ if err != nil {
+ return uh, err
+ }
+
+ date, err := time.Parse(time.RFC1123, ur.Header["Date"][0])
+ if err != nil {
+ return uh, err
+ }
+
+ uh.Date = date
+
+ return uh, nil
+}
+
+// GetHeader represents the headers returned in the response from a Get request.
+type GetHeader struct {
+ BytesUsed int64 `json:"X-Account-Bytes-Used"`
+ ContainerCount int `json:"X-Accound-Container-Count"`
+ ContentLength int64 `json:"Content-Length"`
+ ContentType string `json:"Content-Type"`
+ Date time.Time `mapstructure:"-" json:"-"`
+ ObjectCount int64 `json:"X-Account-Object-Count"`
+ TransID string `json:"X-Trans-Id"`
+}
+
// GetResult is returned from a call to the Get function.
type GetResult struct {
gophercloud.HeaderResult
}
+// Extract will return a struct of headers returned from a call to Get. To obtain
+// a map of headers, call the ExtractHeader method on the GetResult.
+func (gr GetResult) Extract() (GetHeader, error) {
+ fmt.Printf("raw response header: %+v\n", gr.Header)
+
+ var gh GetHeader
+
+ if err := mapstructure.Decode(gr.Header, &gh); err != nil {
+ return gh, err
+ }
+
+ t, err := time.Parse(time.RFC1123, gr.Header["Date"][0])
+ if err != nil {
+ return gh, err
+ }
+ gh.Date = t
+
+ return gh, nil
+}
+
// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
// and returns the custom metatdata associated with the account.
func (gr GetResult) ExtractMetadata() (map[string]string, error) {
@@ -27,8 +103,3 @@
}
return metadata, nil
}
-
-// UpdateResult is returned from a call to the Update function.
-type UpdateResult struct {
- gophercloud.HeaderResult
-}