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