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 | |
Ash Wilson | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame^] | 8 | // AuthOptionsBuilder describes any argument that may be passed to the Create call. |
| 9 | type AuthOptionsBuilder interface { |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 10 | |
Ash Wilson | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame^] | 11 | // ToTokenCreateMap assembles the Create request body, returning an error if parameters are |
| 12 | // missing or inconsistent. |
| 13 | ToTokenCreateMap() (map[string]interface{}, error) |
| 14 | } |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 15 | |
Ash Wilson | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame^] | 16 | // AuthOptions wraps a gophercloud AuthOptions in order to adhere to the AuthOptionsBuilder |
| 17 | // interface. |
| 18 | type AuthOptions struct { |
| 19 | gophercloud.AuthOptions |
| 20 | } |
| 21 | |
| 22 | // ToTokenCreateMap converts AuthOptions into nested maps that can be serialized into a JSON |
| 23 | // request. |
| 24 | func (auth AuthOptions) ToTokenCreateMap() (map[string]interface{}, error) { |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 25 | // Error out if an unsupported auth option is present. |
| 26 | if auth.UserID != "" { |
Ash Wilson | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame^] | 27 | return nil, ErrUserIDProvided |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 28 | } |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 29 | if auth.APIKey != "" { |
Ash Wilson | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame^] | 30 | return nil, ErrAPIKeyProvided |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 31 | } |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 32 | if auth.DomainID != "" { |
Ash Wilson | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame^] | 33 | return nil, ErrDomainIDProvided |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 34 | } |
| 35 | if auth.DomainName != "" { |
Ash Wilson | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame^] | 36 | return nil, ErrDomainNameProvided |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 37 | } |
| 38 | |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 39 | // Username and Password are always required. |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 40 | if auth.Username == "" { |
Ash Wilson | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame^] | 41 | return nil, ErrUsernameRequired |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 42 | } |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 43 | if auth.Password == "" { |
Ash Wilson | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame^] | 44 | return nil, ErrPasswordRequired |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 45 | } |
| 46 | |
Ash Wilson | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame^] | 47 | // Populate the request map. |
| 48 | authMap := make(map[string]interface{}) |
| 49 | |
| 50 | authMap["passwordCredentials"] = map[string]interface{}{ |
| 51 | "username": auth.Username, |
| 52 | "password": auth.Password, |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 53 | } |
Ash Wilson | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame^] | 54 | |
| 55 | if auth.TenantID != "" { |
| 56 | authMap["tenantId"] = auth.TenantID |
| 57 | } |
| 58 | if auth.TenantName != "" { |
| 59 | authMap["tenantName"] = auth.TenantName |
| 60 | } |
| 61 | |
| 62 | return map[string]interface{}{"auth": authMap}, nil |
| 63 | } |
| 64 | |
| 65 | // Create authenticates to the identity service and attempts to acquire a Token. |
| 66 | // If successful, the CreateResult |
| 67 | // Generally, rather than interact with this call directly, end users should call openstack.AuthenticatedClient(), |
| 68 | // which abstracts all of the gory details about navigating service catalogs and such. |
| 69 | func Create(client *gophercloud.ServiceClient, auth AuthOptionsBuilder) CreateResult { |
| 70 | request, err := auth.ToTokenCreateMap() |
| 71 | if err != nil { |
| 72 | return CreateResult{gophercloud.CommonResult{Err: err}} |
| 73 | } |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 74 | |
| 75 | var result CreateResult |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 76 | _, result.Err = perigee.Request("POST", CreateURL(client), perigee.Options{ |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 77 | ReqBody: &request, |
| 78 | Results: &result.Resp, |
| 79 | OkCodes: []int{200, 203}, |
| 80 | }) |
| 81 | return result |
| 82 | } |