Ash Wilson | 54b0382 | 2014-10-07 14:18:41 -0400 | [diff] [blame] | 1 | package tokens |
| 2 | |
| 3 | import ( |
| 4 | "github.com/racker/perigee" |
| 5 | "github.com/rackspace/gophercloud" |
| 6 | os "github.com/rackspace/gophercloud/openstack/identity/v2/tokens" |
| 7 | ) |
| 8 | |
| 9 | // Create authenticates to Rackspace's identity service and attempts to acquire a Token. Rather |
| 10 | // than interact with this service directly, users should generally call |
| 11 | // rackspace.AuthenticatedClient(). |
| 12 | func Create(client *gophercloud.ServiceClient, auth gophercloud.AuthOptions) os.CreateResult { |
| 13 | if auth.APIKey != "" { |
| 14 | // Authenticate with the provided API key. |
| 15 | |
| 16 | if auth.Username == "" { |
| 17 | return createErr(os.ErrUsernameRequired) |
| 18 | } |
| 19 | |
| 20 | var request struct { |
| 21 | Auth struct { |
| 22 | APIKeyCredentials struct { |
| 23 | Username string `json:"username"` |
| 24 | APIKey string `json:"apiKey"` |
| 25 | } `json:"RAX-KSKEY:apiKeyCredentials"` |
| 26 | TenantID string `json:"tenantId,omitempty"` |
| 27 | TenantName string `json:"tenantName,omitempty"` |
| 28 | } `json:"auth"` |
| 29 | } |
| 30 | |
| 31 | request.Auth.APIKeyCredentials.Username = auth.Username |
| 32 | request.Auth.APIKeyCredentials.APIKey = auth.APIKey |
| 33 | request.Auth.TenantID = auth.TenantID |
| 34 | request.Auth.TenantName = auth.TenantName |
| 35 | |
| 36 | var result os.CreateResult |
| 37 | _, result.Err = perigee.Request("POST", os.CreateURL(client), perigee.Options{ |
| 38 | ReqBody: &request, |
| 39 | Results: &result.Resp, |
| 40 | OkCodes: []int{200, 203}, |
| 41 | }) |
| 42 | return result |
| 43 | } |
| 44 | |
| 45 | return os.Create(client, auth) |
| 46 | } |
| 47 | |
| 48 | func createErr(err error) os.CreateResult { |
| 49 | return os.CreateResult{CommonResult: gophercloud.CommonResult{Err: err}} |
| 50 | } |