Ash Wilson | a192008 | 2014-08-28 14:24:17 -0400 | [diff] [blame] | 1 | package tokens |
| 2 | |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 3 | import ( |
| 4 | "time" |
| 5 | |
| 6 | "github.com/mitchellh/mapstructure" |
Ash Wilson | a6b0831 | 2014-10-02 15:27:45 -0400 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud" |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 8 | ) |
| 9 | |
Ash Wilson | a192008 | 2014-08-28 14:24:17 -0400 | [diff] [blame] | 10 | // TokenCreateResult contains the document structure returned from a Create call. |
Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 11 | type TokenCreateResult struct { |
| 12 | response map[string]interface{} |
| 13 | tokenID string |
| 14 | } |
Ash Wilson | a192008 | 2014-08-28 14:24:17 -0400 | [diff] [blame] | 15 | |
| 16 | // TokenID retrieves a token generated by a Create call from an token creation response. |
Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 17 | func (r *TokenCreateResult) TokenID() (string, error) { |
| 18 | return r.tokenID, nil |
Ash Wilson | a192008 | 2014-08-28 14:24:17 -0400 | [diff] [blame] | 19 | } |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 20 | |
| 21 | // ExpiresAt retrieves the token expiration time. |
| 22 | func (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 Wilson | a6b0831 | 2014-10-02 15:27:45 -0400 | [diff] [blame] | 38 | ts, err := time.Parse(gophercloud.RFC3339Milli, resp.Token.ExpiresAt) |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 39 | if err != nil { |
| 40 | return time.Time{}, err |
| 41 | } |
| 42 | |
| 43 | return ts, nil |
| 44 | } |