Ash Wilson | 54b0382 | 2014-10-07 14:18:41 -0400 | [diff] [blame] | 1 | package tokens |
| 2 | |
| 3 | import ( |
Ash Wilson | 2239724 | 2014-10-07 16:10:21 -0400 | [diff] [blame] | 4 | "errors" |
| 5 | |
Ash Wilson | 54b0382 | 2014-10-07 14:18:41 -0400 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
| 7 | os "github.com/rackspace/gophercloud/openstack/identity/v2/tokens" |
| 8 | ) |
| 9 | |
Ash Wilson | 2239724 | 2014-10-07 16:10:21 -0400 | [diff] [blame] | 10 | var ( |
| 11 | // ErrPasswordProvided is returned if both a password and an API key are provided to Create. |
| 12 | ErrPasswordProvided = errors.New("Please provide either a password or an API key.") |
| 13 | ) |
| 14 | |
| 15 | // AuthOptions wraps the OpenStack AuthOptions struct to be able to customize the request body |
| 16 | // when API key authentication is used. |
| 17 | type AuthOptions struct { |
| 18 | os.AuthOptions |
| 19 | } |
| 20 | |
| 21 | // WrapOptions embeds a root AuthOptions struct in a package-specific one. |
| 22 | func WrapOptions(original gophercloud.AuthOptions) AuthOptions { |
| 23 | return AuthOptions{AuthOptions: os.WrapOptions(original)} |
| 24 | } |
| 25 | |
| 26 | // ToTokenCreateMap serializes an AuthOptions into a request body. If an API key is provided, it |
| 27 | // will be used, otherwise |
| 28 | func (auth AuthOptions) ToTokenCreateMap() (map[string]interface{}, error) { |
| 29 | if auth.APIKey == "" { |
| 30 | return auth.AuthOptions.ToTokenCreateMap() |
| 31 | } |
| 32 | |
| 33 | // Verify that other required attributes are present. |
| 34 | if auth.Username == "" { |
| 35 | return nil, os.ErrUsernameRequired |
| 36 | } |
| 37 | |
| 38 | authMap := make(map[string]interface{}) |
| 39 | |
| 40 | authMap["RAX-KSKEY:apiKeyCredentials"] = map[string]interface{}{ |
| 41 | "username": auth.Username, |
| 42 | "apiKey": auth.APIKey, |
| 43 | } |
| 44 | |
| 45 | if auth.TenantID != "" { |
| 46 | authMap["tenantId"] = auth.TenantID |
| 47 | } |
| 48 | if auth.TenantName != "" { |
| 49 | authMap["tenantName"] = auth.TenantName |
| 50 | } |
| 51 | |
| 52 | return map[string]interface{}{"auth": authMap}, nil |
| 53 | } |
| 54 | |
Ash Wilson | 54b0382 | 2014-10-07 14:18:41 -0400 | [diff] [blame] | 55 | // Create authenticates to Rackspace's identity service and attempts to acquire a Token. Rather |
| 56 | // than interact with this service directly, users should generally call |
| 57 | // rackspace.AuthenticatedClient(). |
Ash Wilson | 2239724 | 2014-10-07 16:10:21 -0400 | [diff] [blame] | 58 | func Create(client *gophercloud.ServiceClient, auth AuthOptions) os.CreateResult { |
Ash Wilson | 54b0382 | 2014-10-07 14:18:41 -0400 | [diff] [blame] | 59 | return os.Create(client, auth) |
| 60 | } |