blob: 4f9885af03ce1d24261539806bbcf359d34c1cda [file] [log] [blame]
Ash Wilson54b03822014-10-07 14:18:41 -04001package tokens
2
3import (
Ash Wilson22397242014-10-07 16:10:21 -04004 "errors"
5
Ash Wilson54b03822014-10-07 14:18:41 -04006 "github.com/rackspace/gophercloud"
7 os "github.com/rackspace/gophercloud/openstack/identity/v2/tokens"
8)
9
Ash Wilson22397242014-10-07 16:10:21 -040010var (
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.
17type AuthOptions struct {
18 os.AuthOptions
19}
20
21// WrapOptions embeds a root AuthOptions struct in a package-specific one.
22func 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
28func (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 Wilson54b03822014-10-07 14:18:41 -040055// 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 Wilson22397242014-10-07 16:10:21 -040058func Create(client *gophercloud.ServiceClient, auth AuthOptions) os.CreateResult {
Ash Wilson54b03822014-10-07 14:18:41 -040059 return os.Create(client, auth)
60}