blob: 145a43d9640e4d29cad0f0571db7f709e97fc825 [file] [log] [blame]
Ash Wilsona1920082014-08-28 14:24:17 -04001package tokens
2
Ash Wilson46d913f2014-08-29 11:00:11 -04003import (
4 "time"
5
6 "github.com/mitchellh/mapstructure"
Ash Wilsona6b08312014-10-02 15:27:45 -04007 "github.com/rackspace/gophercloud"
Ash Wilson46d913f2014-08-29 11:00:11 -04008)
9
Ash Wilsona1920082014-08-28 14:24:17 -040010// TokenCreateResult contains the document structure returned from a Create call.
Ash Wilson4a52e2a2014-08-29 09:28:00 -040011type TokenCreateResult struct {
12 response map[string]interface{}
13 tokenID string
14}
Ash Wilsona1920082014-08-28 14:24:17 -040015
16// TokenID retrieves a token generated by a Create call from an token creation response.
Ash Wilson4a52e2a2014-08-29 09:28:00 -040017func (r *TokenCreateResult) TokenID() (string, error) {
18 return r.tokenID, nil
Ash Wilsona1920082014-08-28 14:24:17 -040019}
Ash Wilsone058e342014-08-29 10:31:41 -040020
21// ExpiresAt retrieves the token expiration time.
22func (r *TokenCreateResult) ExpiresAt() (time.Time, error) {
23 type tokenResp struct {
24 ExpiresAt string `mapstructure:"expires_at"`
25 }
26
27 type response struct {
28 Token tokenResp `mapstructure:"token"`
29 }
30
31 var resp response
32 err := mapstructure.Decode(r.response, &resp)
33 if err != nil {
34 return time.Time{}, err
35 }
36
37 // Attempt to parse the timestamp.
Ash Wilsona6b08312014-10-02 15:27:45 -040038 ts, err := time.Parse(gophercloud.RFC3339Milli, resp.Token.ExpiresAt)
Ash Wilsone058e342014-08-29 10:31:41 -040039 if err != nil {
40 return time.Time{}, err
41 }
42
43 return ts, nil
44}