blob: c0441ab8bd6390ec3e698e070a30083b926d9e8b [file] [log] [blame]
Ash Wilson1f110512014-10-02 15:43:47 -04001package tokens
2
3import (
4 "github.com/racker/perigee"
5 "github.com/rackspace/gophercloud"
6)
7
8// Create authenticates to the identity service and attempts to acquire a Token.
9// If successful, the CreateResult
10// Generally, rather than interact with this call directly, end users should call openstack.AuthenticatedClient(),
11// which abstracts all of the gory details about navigating service catalogs and such.
12func Create(client *gophercloud.ServiceClient, auth gophercloud.AuthOptions) CreateResult {
13 var request struct {
14 Auth struct {
15 PasswordCredentials struct {
16 Username string `json:"username"`
17 Password string `json:"password"`
18 } `json:"passwordCredentials,omitempty"`
19 APIKeyCredentials struct {
20 Username string `json:"username"`
21 APIKey string `json:"apiKey"`
22 } `json:"RAX-KSKEY:apiKeyCredentials,omitempty"`
23 TenantID string `json:"tenantId,omitempty"`
24 TenantName string `json:"tenantName,omitempty"`
25 } `json:"auth"`
26 }
27
28 // Error out if an unsupported auth option is present.
29 if auth.UserID != "" {
30 return createErr(ErrUserIDProvided)
31 }
32 if auth.DomainID != "" {
33 return createErr(ErrDomainIDProvided)
34 }
35 if auth.DomainName != "" {
36 return createErr(ErrDomainNameProvided)
37 }
38
39 // Username is always required.
40 if auth.Username == "" {
41 return createErr(ErrUsernameRequired)
42 }
43
44 // Populate either PasswordCredentials or APIKeyCredentials
45 if auth.Password != "" {
46 if auth.APIKey != "" {
47 return createErr(ErrPasswordOrAPIKey)
48 }
49
50 // Username + Password
51 request.Auth.PasswordCredentials.Username = auth.Username
52 request.Auth.PasswordCredentials.Password = auth.Password
53 } else if auth.APIKey != "" {
54 // API key authentication.
55 request.Auth.APIKeyCredentials.Username = auth.Username
56 request.Auth.APIKeyCredentials.APIKey = auth.APIKey
57 } else {
58 return createErr(ErrPasswordOrAPIKey)
59 }
60
61 // Populate the TenantName or TenantID, if provided.
62 request.Auth.TenantID = auth.TenantID
63 request.Auth.TenantName = auth.TenantName
64
65 var result CreateResult
66 _, result.Err = perigee.Request("POST", listURL(client), perigee.Options{
67 ReqBody: &request,
68 Results: &result.Resp,
69 OkCodes: []int{200, 203},
70 })
71 return result
72}