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