| package v3 |
| |
| import ( |
| "time" |
| |
| "github.com/rackspace/gophercloud" |
| "github.com/rackspace/gophercloud/openstack/identity/v3/tokens" |
| ) |
| |
| // Client abstracts the connection information necessary to make API calls to Identity v3 resources. |
| // It exists mainly to adhere to the IdentityService interface. |
| type Client struct { |
| gophercloud.ServiceClient |
| } |
| |
| // Token models a token acquired from the tokens/ API resource. |
| type Token struct { |
| ID string |
| ExpiresAt time.Time |
| } |
| |
| // NewClient creates a new client associated with the v3 identity service of a provider. |
| func NewClient(provider *gophercloud.ProviderClient, endpoint string) *Client { |
| return &Client{ |
| ServiceClient: gophercloud.ServiceClient{ |
| ProviderClient: *provider, |
| Endpoint: endpoint, |
| }, |
| } |
| } |
| |
| // GetToken provides the supplied credentials to an identity v3 endpoint and attempts to acquire a token. |
| func (c *Client) GetToken(authOptions gophercloud.AuthOptions) (*Token, error) { |
| c.ServiceClient.ProviderClient.Options = authOptions |
| |
| result, err := tokens.Create(&c.ServiceClient, nil) |
| if err != nil { |
| return nil, err |
| } |
| |
| tokenID, err := result.TokenID() |
| if err != nil { |
| return nil, err |
| } |
| |
| expiresAt, err := result.ExpiresAt() |
| if err != nil { |
| return nil, err |
| } |
| |
| return &Token{ID: tokenID, ExpiresAt: expiresAt}, nil |
| } |