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