Parse token expiration from the response.
diff --git a/openstack/identity/v3/tokens/results.go b/openstack/identity/v3/tokens/results.go
index 0f44744..9f0d623 100644
--- a/openstack/identity/v3/tokens/results.go
+++ b/openstack/identity/v3/tokens/results.go
@@ -10,3 +10,28 @@
func (r *TokenCreateResult) TokenID() (string, error) {
return r.tokenID, nil
}
+
+// ExpiresAt retrieves the token expiration time.
+func (r *TokenCreateResult) ExpiresAt() (time.Time, error) {
+ type tokenResp struct {
+ ExpiresAt string `mapstructure:"expires_at"`
+ }
+
+ type response struct {
+ Token tokenResp `mapstructure:"token"`
+ }
+
+ var resp response
+ err := mapstructure.Decode(r.response, &resp)
+ if err != nil {
+ return time.Time{}, err
+ }
+
+ // Attempt to parse the timestamp.
+ ts, err := time.Parse(RFC3339Milli, resp.Token.ExpiresAt)
+ if err != nil {
+ return time.Time{}, err
+ }
+
+ return ts, nil
+}
diff --git a/openstack/identity/v3/tokens/results_test.go b/openstack/identity/v3/tokens/results_test.go
index ca0d563..669db61 100644
--- a/openstack/identity/v3/tokens/results_test.go
+++ b/openstack/identity/v3/tokens/results_test.go
@@ -1,6 +1,9 @@
package tokens
-import "testing"
+import (
+ "testing"
+ "time"
+)
func TestTokenID(t *testing.T) {
result := TokenCreateResult{tokenID: "1234"}
@@ -10,3 +13,25 @@
t.Errorf("Expected tokenID of 1234, got %s", token)
}
}
+
+func TestExpiresAt(t *testing.T) {
+ resp := map[string]interface{}{
+ "token": map[string]string{
+ "expires_at": "2013-02-02T18:30:59.000000Z",
+ },
+ }
+
+ result := TokenCreateResult{
+ tokenID: "1234",
+ response: resp,
+ }
+
+ expected, _ := time.Parse(time.UnixDate, "Sat Feb 2 18:30:59 UTC 2013")
+ actual, err := result.ExpiresAt()
+ if err != nil {
+ t.Errorf("Error extraction expiration time: %v", err)
+ }
+ if actual != expected {
+ t.Errorf("Expected expiration time %s, but was %s", expected.Format(time.UnixDate), actual.Format(time.UnixDate))
+ }
+}