Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 1 | package tokens |
| 2 | |
Jon Perritt | a3302e1 | 2016-03-07 03:48:59 -0600 | [diff] [blame^] | 3 | import "github.com/gophercloud/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 | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame] | 41 | // Populate the request map. |
| 42 | authMap := make(map[string]interface{}) |
| 43 | |
jrperritt | 95b74c8 | 2015-07-28 20:39:27 -0600 | [diff] [blame] | 44 | if auth.Username != "" { |
Jon Perritt | a3302e1 | 2016-03-07 03:48:59 -0600 | [diff] [blame^] | 45 | if auth.Password == "" { |
| 46 | err := gophercloud.ErrMissingInput{} |
| 47 | err.Function = "tokens.ToTokenCreateMap" |
| 48 | err.Argument = "tokens.AuthOptions.Password" |
| 49 | return nil, err |
| 50 | } |
| 51 | authMap["passwordCredentials"] = map[string]interface{}{ |
| 52 | "username": auth.Username, |
| 53 | "password": auth.Password, |
jrperritt | 95b74c8 | 2015-07-28 20:39:27 -0600 | [diff] [blame] | 54 | } |
jrperritt | 1f218c8 | 2015-07-29 08:54:18 -0600 | [diff] [blame] | 55 | } else if auth.TokenID != "" { |
jrperritt | 95b74c8 | 2015-07-28 20:39:27 -0600 | [diff] [blame] | 56 | authMap["token"] = map[string]interface{}{ |
jrperritt | 1f218c8 | 2015-07-29 08:54:18 -0600 | [diff] [blame] | 57 | "id": auth.TokenID, |
jrperritt | 95b74c8 | 2015-07-28 20:39:27 -0600 | [diff] [blame] | 58 | } |
| 59 | } else { |
Jon Perritt | a3302e1 | 2016-03-07 03:48:59 -0600 | [diff] [blame^] | 60 | err := gophercloud.ErrMissingInput{} |
| 61 | err.Function = "tokens.ToTokenCreateMap" |
| 62 | err.Argument = "tokens.AuthOptions.Username/tokens.AuthOptions.TokenID" |
| 63 | err.Info = "You must provide either username/password or tenantID/token values." |
| 64 | return nil, err |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame] | 65 | } |
Ash Wilson | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame] | 66 | |
| 67 | if auth.TenantID != "" { |
| 68 | authMap["tenantId"] = auth.TenantID |
| 69 | } |
| 70 | if auth.TenantName != "" { |
| 71 | authMap["tenantName"] = auth.TenantName |
| 72 | } |
| 73 | |
| 74 | return map[string]interface{}{"auth": authMap}, nil |
| 75 | } |
| 76 | |
| 77 | // Create authenticates to the identity service and attempts to acquire a Token. |
| 78 | // If successful, the CreateResult |
| 79 | // Generally, rather than interact with this call directly, end users should call openstack.AuthenticatedClient(), |
| 80 | // which abstracts all of the gory details about navigating service catalogs and such. |
| 81 | func Create(client *gophercloud.ServiceClient, auth AuthOptionsBuilder) CreateResult { |
| 82 | request, err := auth.ToTokenCreateMap() |
| 83 | if err != nil { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 84 | return CreateResult{gophercloud.Result{Err: err}} |
Ash Wilson | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame] | 85 | } |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 86 | |
| 87 | var result CreateResult |
Jamie Hannaford | 562a7d5 | 2015-03-24 16:20:16 +0100 | [diff] [blame] | 88 | _, result.Err = client.Post(CreateURL(client), request, &result.Body, &gophercloud.RequestOpts{ |
| 89 | OkCodes: []int{200, 203}, |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 90 | }) |
| 91 | return result |
| 92 | } |
hzlouchao | f6e2926 | 2015-10-27 12:51:08 +0800 | [diff] [blame] | 93 | |
Jon Perritt | a3302e1 | 2016-03-07 03:48:59 -0600 | [diff] [blame^] | 94 | // Get validates and retrieves information for user's token. |
hzlouchao | f6e2926 | 2015-10-27 12:51:08 +0800 | [diff] [blame] | 95 | func Get(client *gophercloud.ServiceClient, token string) GetResult { |
hzlouchao | b764089 | 2015-11-04 21:37:20 +0800 | [diff] [blame] | 96 | var result GetResult |
| 97 | _, result.Err = client.Get(GetURL(client, token), &result.Body, &gophercloud.RequestOpts{ |
| 98 | OkCodes: []int{200, 203}, |
| 99 | }) |
hzlouchao | b764089 | 2015-11-04 21:37:20 +0800 | [diff] [blame] | 100 | return result |
| 101 | } |