Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 1 | package tokens |
| 2 | |
| 3 | import ( |
| 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. |
| 12 | func Create(client *gophercloud.ServiceClient, auth gophercloud.AuthOptions) CreateResult { |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 13 | type passwordCredentials struct { |
| 14 | Username string `json:"username"` |
| 15 | Password string `json:"password"` |
| 16 | } |
| 17 | |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 18 | var request struct { |
| 19 | Auth struct { |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 20 | PasswordCredentials *passwordCredentials `json:"passwordCredentials"` |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 21 | TenantID string `json:"tenantId,omitempty"` |
| 22 | TenantName string `json:"tenantName,omitempty"` |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 23 | } `json:"auth"` |
| 24 | } |
| 25 | |
| 26 | // Error out if an unsupported auth option is present. |
| 27 | if auth.UserID != "" { |
| 28 | return createErr(ErrUserIDProvided) |
| 29 | } |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 30 | if auth.APIKey != "" { |
| 31 | return createErr(ErrAPIKeyProvided) |
| 32 | } |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 33 | if auth.DomainID != "" { |
| 34 | return createErr(ErrDomainIDProvided) |
| 35 | } |
| 36 | if auth.DomainName != "" { |
| 37 | return createErr(ErrDomainNameProvided) |
| 38 | } |
| 39 | |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 40 | // Username and Password are always required. |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 41 | if auth.Username == "" { |
| 42 | return createErr(ErrUsernameRequired) |
| 43 | } |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 44 | if auth.Password == "" { |
| 45 | return createErr(ErrPasswordRequired) |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 46 | } |
| 47 | |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 48 | // Populate the request. |
| 49 | request.Auth.PasswordCredentials = &passwordCredentials{ |
| 50 | Username: auth.Username, |
| 51 | Password: auth.Password, |
| 52 | } |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 53 | request.Auth.TenantID = auth.TenantID |
| 54 | request.Auth.TenantName = auth.TenantName |
| 55 | |
| 56 | var result CreateResult |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 57 | _, result.Err = perigee.Request("POST", CreateURL(client), perigee.Options{ |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 58 | ReqBody: &request, |
| 59 | Results: &result.Resp, |
| 60 | OkCodes: []int{200, 203}, |
| 61 | }) |
| 62 | return result |
| 63 | } |