blob: 346a1492ac8365bcb31c606884ef69dc552090cf [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
Ash Wilson1f110512014-10-02 15:43:47 -040018 var request struct {
19 Auth struct {
Ash Wilson1cf4d5f2014-10-07 14:16:18 -040020 PasswordCredentials *passwordCredentials `json:"passwordCredentials"`
Ash Wilsonaa197a92014-10-03 11:38:08 -040021 TenantID string `json:"tenantId,omitempty"`
22 TenantName string `json:"tenantName,omitempty"`
Ash Wilson1f110512014-10-02 15:43:47 -040023 } `json:"auth"`
24 }
25
26 // Error out if an unsupported auth option is present.
27 if auth.UserID != "" {
28 return createErr(ErrUserIDProvided)
29 }
Ash Wilson1cf4d5f2014-10-07 14:16:18 -040030 if auth.APIKey != "" {
31 return createErr(ErrAPIKeyProvided)
32 }
Ash Wilson1f110512014-10-02 15:43:47 -040033 if auth.DomainID != "" {
34 return createErr(ErrDomainIDProvided)
35 }
36 if auth.DomainName != "" {
37 return createErr(ErrDomainNameProvided)
38 }
39
Ash Wilson1cf4d5f2014-10-07 14:16:18 -040040 // Username and Password are always required.
Ash Wilson1f110512014-10-02 15:43:47 -040041 if auth.Username == "" {
42 return createErr(ErrUsernameRequired)
43 }
Ash Wilson1cf4d5f2014-10-07 14:16:18 -040044 if auth.Password == "" {
45 return createErr(ErrPasswordRequired)
Ash Wilson1f110512014-10-02 15:43:47 -040046 }
47
Ash Wilson1cf4d5f2014-10-07 14:16:18 -040048 // Populate the request.
49 request.Auth.PasswordCredentials = &passwordCredentials{
50 Username: auth.Username,
51 Password: auth.Password,
52 }
Ash Wilson1f110512014-10-02 15:43:47 -040053 request.Auth.TenantID = auth.TenantID
54 request.Auth.TenantName = auth.TenantName
55
56 var result CreateResult
Ash Wilson1cf4d5f2014-10-07 14:16:18 -040057 _, result.Err = perigee.Request("POST", CreateURL(client), perigee.Options{
Ash Wilson1f110512014-10-02 15:43:47 -040058 ReqBody: &request,
59 Results: &result.Resp,
60 OkCodes: []int{200, 203},
61 })
62 return result
63}