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 | |
| 18 | type apiKeyCredentials struct { |
| 19 | Username string `json:"username"` |
| 20 | APIKey string `json:"apiKey"` |
| 21 | } |
| 22 | |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 23 | var request struct { |
| 24 | Auth struct { |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 25 | 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 Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 29 | } `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 Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 55 | request.Auth.PasswordCredentials = &passwordCredentials{ |
| 56 | Username: auth.Username, |
| 57 | Password: auth.Password, |
| 58 | } |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 59 | } else if auth.APIKey != "" { |
| 60 | // API key authentication. |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 61 | request.Auth.APIKeyCredentials = &apiKeyCredentials{ |
| 62 | Username: auth.Username, |
| 63 | APIKey: auth.APIKey, |
| 64 | } |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 65 | } 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 | } |