| 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 | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 10 | // commonResult is the deferred result of a Create or a Get call. | 
 | 11 | type commonResult struct { | 
| Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 12 | 	gophercloud.Result | 
| Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 13 | } | 
| Ash Wilson | a192008 | 2014-08-28 14:24:17 -0400 | [diff] [blame] | 14 |  | 
| Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 15 | // Extract interprets a commonResult as a Token. | 
 | 16 | func (r commonResult) Extract() (*Token, error) { | 
 | 17 | 	if r.Err != nil { | 
 | 18 | 		return nil, r.Err | 
| Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 19 | 	} | 
 | 20 |  | 
| Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 21 | 	var response struct { | 
 | 22 | 		Token struct { | 
 | 23 | 			ExpiresAt string `mapstructure:"expires_at"` | 
 | 24 | 		} `mapstructure:"token"` | 
| Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 25 | 	} | 
 | 26 |  | 
| Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 27 | 	var token Token | 
 | 28 |  | 
 | 29 | 	// Parse the token itself from the stored headers. | 
| Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 30 | 	token.ID = r.Header.Get("X-Subject-Token") | 
| Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 31 |  | 
| Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 32 | 	err := mapstructure.Decode(r.Body, &response) | 
| Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 33 | 	if err != nil { | 
| Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 34 | 		return nil, err | 
| Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 35 | 	} | 
 | 36 |  | 
 | 37 | 	// Attempt to parse the timestamp. | 
| Ash Wilson | f25ae37 | 2014-10-06 14:40:29 -0400 | [diff] [blame] | 38 | 	token.ExpiresAt, err = time.Parse(gophercloud.RFC3339Milli, response.Token.ExpiresAt) | 
| Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 39 |  | 
| Jamie Hannaford | a253adf | 2014-10-08 17:14:24 +0200 | [diff] [blame] | 40 | 	return &token, err | 
| Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 41 | } | 
 | 42 |  | 
 | 43 | // CreateResult is the deferred response from a Create call. | 
 | 44 | type CreateResult struct { | 
 | 45 | 	commonResult | 
 | 46 | } | 
 | 47 |  | 
 | 48 | // createErr quickly creates a CreateResult that reports an error. | 
 | 49 | func createErr(err error) CreateResult { | 
 | 50 | 	return CreateResult{ | 
| Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 51 | 		commonResult: commonResult{Result: gophercloud.Result{Err: err}}, | 
| Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 52 | 	} | 
 | 53 | } | 
 | 54 |  | 
 | 55 | // GetResult is the deferred response from a Get call. | 
 | 56 | type GetResult struct { | 
 | 57 | 	commonResult | 
 | 58 | } | 
 | 59 |  | 
| Jamie Hannaford | f38dd2e | 2014-10-27 11:36:54 +0100 | [diff] [blame] | 60 | // RevokeResult is the deferred response from a Revoke call. | 
 | 61 | type RevokeResult struct { | 
 | 62 | 	commonResult | 
 | 63 | } | 
 | 64 |  | 
| Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 65 | // Token is a string that grants a user access to a controlled set of services in an OpenStack provider. | 
 | 66 | // Each Token is valid for a set length of time. | 
 | 67 | type Token struct { | 
 | 68 | 	// ID is the issued token. | 
 | 69 | 	ID string | 
 | 70 |  | 
 | 71 | 	// ExpiresAt is the timestamp at which this token will no longer be accepted. | 
 | 72 | 	ExpiresAt time.Time | 
| Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 73 | } |