Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 1 | package v3 |
| 2 | |
Ash Wilson | 8a85a91 | 2014-08-28 15:09:58 -0400 | [diff] [blame] | 3 | import ( |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 4 | "time" |
| 5 | |
Ash Wilson | 8a85a91 | 2014-08-28 15:09:58 -0400 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
Ash Wilson | 131b775 | 2014-08-29 12:53:55 -0400 | [diff] [blame^] | 7 | "github.com/rackspace/gophercloud/openstack/identity/v3/tokens" |
Ash Wilson | 8a85a91 | 2014-08-28 15:09:58 -0400 | [diff] [blame] | 8 | ) |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 9 | |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 10 | // Client abstracts the connection information necessary to make API calls to Identity v3 resources. |
| 11 | // It exists mainly to adhere to the IdentityService interface. |
Ash Wilson | 131b775 | 2014-08-29 12:53:55 -0400 | [diff] [blame^] | 12 | type Client struct { |
| 13 | gophercloud.ServiceClient |
| 14 | } |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 15 | |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 16 | // Token models a token acquired from the tokens/ API resource. |
| 17 | type Token struct { |
| 18 | ID string |
| 19 | ExpiresAt time.Time |
| 20 | } |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 21 | |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 22 | // NewClient creates a new client associated with the v3 identity service of a provider. |
| 23 | func NewClient(provider *gophercloud.ProviderClient) *Client { |
| 24 | return &Client{ |
Ash Wilson | 131b775 | 2014-08-29 12:53:55 -0400 | [diff] [blame^] | 25 | ServiceClient: gophercloud.ServiceClient{ |
| 26 | ProviderClient: *provider, |
| 27 | Endpoint: provider.IdentityEndpoint + "v3/", |
| 28 | }, |
Ash Wilson | 8a85a91 | 2014-08-28 15:09:58 -0400 | [diff] [blame] | 29 | } |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 30 | } |
Ash Wilson | 8a85a91 | 2014-08-28 15:09:58 -0400 | [diff] [blame] | 31 | |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 32 | // Authenticate provides the supplied credentials to an identity v3 endpoint and attempts to acquire a token. |
| 33 | func (c *Client) Authenticate(authOptions gophercloud.AuthOptions) (*Token, error) { |
Ash Wilson | 131b775 | 2014-08-29 12:53:55 -0400 | [diff] [blame^] | 34 | c.ServiceClient.ProviderClient.Options = authOptions |
| 35 | |
| 36 | result, err := tokens.Create(&c.ServiceClient, nil) |
| 37 | if err != nil { |
| 38 | return nil, err |
| 39 | } |
| 40 | |
| 41 | tokenID, err := result.TokenID() |
| 42 | if err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | |
| 46 | expiresAt, err := result.ExpiresAt() |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | |
| 51 | return &Token{ |
| 52 | ID: tokenID, |
| 53 | ExpiresAt: expiresAt, |
| 54 | }, nil |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 55 | } |