blob: 5bd5a70ed52404b9b27d9b025b0e9e96dc467982 [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 {
Ash Wilsonaa197a92014-10-03 11:38:08 -040013 type passwordCredentials struct {
14 Username string `json:"username"`
15 Password string `json:"password"`
16 }
17
18 type apiKeyCredentials struct {
19 Username string `json:"username"`
20 APIKey string `json:"apiKey"`
21 }
22
Ash Wilson1f110512014-10-02 15:43:47 -040023 var request struct {
24 Auth struct {
Ash Wilsonaa197a92014-10-03 11:38:08 -040025 PasswordCredentials *passwordCredentials `json:"passwordCredentials,omitempty"`
26 APIKeyCredentials *apiKeyCredentials `json:"RAX-KSKEY:apiKeyCredentials,omitempty"`
27 TenantID string `json:"tenantId,omitempty"`
28 TenantName string `json:"tenantName,omitempty"`
Ash Wilson1f110512014-10-02 15:43:47 -040029 } `json:"auth"`
30 }
31
32 // Error out if an unsupported auth option is present.
33 if auth.UserID != "" {
34 return createErr(ErrUserIDProvided)
35 }
36 if auth.DomainID != "" {
37 return createErr(ErrDomainIDProvided)
38 }
39 if auth.DomainName != "" {
40 return createErr(ErrDomainNameProvided)
41 }
42
43 // Username is always required.
44 if auth.Username == "" {
45 return createErr(ErrUsernameRequired)
46 }
47
48 // Populate either PasswordCredentials or APIKeyCredentials
49 if auth.Password != "" {
50 if auth.APIKey != "" {
51 return createErr(ErrPasswordOrAPIKey)
52 }
53
54 // Username + Password
Ash Wilsonaa197a92014-10-03 11:38:08 -040055 request.Auth.PasswordCredentials = &passwordCredentials{
56 Username: auth.Username,
57 Password: auth.Password,
58 }
Ash Wilson1f110512014-10-02 15:43:47 -040059 } else if auth.APIKey != "" {
60 // API key authentication.
Ash Wilsonaa197a92014-10-03 11:38:08 -040061 request.Auth.APIKeyCredentials = &apiKeyCredentials{
62 Username: auth.Username,
63 APIKey: auth.APIKey,
64 }
Ash Wilson1f110512014-10-02 15:43:47 -040065 } else {
66 return createErr(ErrPasswordOrAPIKey)
67 }
68
69 // Populate the TenantName or TenantID, if provided.
70 request.Auth.TenantID = auth.TenantID
71 request.Auth.TenantName = auth.TenantName
72
73 var result CreateResult
74 _, result.Err = perigee.Request("POST", listURL(client), perigee.Options{
75 ReqBody: &request,
76 Results: &result.Resp,
77 OkCodes: []int{200, 203},
78 })
79 return result
80}