blob: 6064bc7127b30ad5738027c63ccacd40f0992cae [file] [log] [blame]
Ash Wilson1f110512014-10-02 15:43:47 -04001package tokens
2
Jon Perritta3302e12016-03-07 03:48:59 -06003import "github.com/gophercloud/gophercloud"
Ash Wilson1f110512014-10-02 15:43:47 -04004
Ash Wilson40095f02014-10-07 15:46:40 -04005// AuthOptionsBuilder describes any argument that may be passed to the Create call.
6type AuthOptionsBuilder interface {
Ash Wilsonaa197a92014-10-03 11:38:08 -04007
Ash Wilson40095f02014-10-07 15:46:40 -04008 // ToTokenCreateMap assembles the Create request body, returning an error if parameters are
9 // missing or inconsistent.
10 ToTokenCreateMap() (map[string]interface{}, error)
11}
Ash Wilson1f110512014-10-02 15:43:47 -040012
Ash Wilson40095f02014-10-07 15:46:40 -040013// AuthOptions wraps a gophercloud AuthOptions in order to adhere to the AuthOptionsBuilder
14// interface.
15type AuthOptions struct {
16 gophercloud.AuthOptions
17}
18
Ash Wilson22397242014-10-07 16:10:21 -040019// WrapOptions embeds a root AuthOptions struct in a package-specific one.
20func WrapOptions(original gophercloud.AuthOptions) AuthOptions {
21 return AuthOptions{AuthOptions: original}
22}
23
Ash Wilson40095f02014-10-07 15:46:40 -040024// ToTokenCreateMap converts AuthOptions into nested maps that can be serialized into a JSON
25// request.
26func (auth AuthOptions) ToTokenCreateMap() (map[string]interface{}, error) {
Ash Wilson1f110512014-10-02 15:43:47 -040027 // Error out if an unsupported auth option is present.
28 if auth.UserID != "" {
Ash Wilson40095f02014-10-07 15:46:40 -040029 return nil, ErrUserIDProvided
Ash Wilson1f110512014-10-02 15:43:47 -040030 }
Ash Wilson1cf4d5f2014-10-07 14:16:18 -040031 if auth.APIKey != "" {
Ash Wilson40095f02014-10-07 15:46:40 -040032 return nil, ErrAPIKeyProvided
Ash Wilson1cf4d5f2014-10-07 14:16:18 -040033 }
Ash Wilson1f110512014-10-02 15:43:47 -040034 if auth.DomainID != "" {
Ash Wilson40095f02014-10-07 15:46:40 -040035 return nil, ErrDomainIDProvided
Ash Wilson1f110512014-10-02 15:43:47 -040036 }
37 if auth.DomainName != "" {
Ash Wilson40095f02014-10-07 15:46:40 -040038 return nil, ErrDomainNameProvided
Ash Wilson1f110512014-10-02 15:43:47 -040039 }
40
Ash Wilson40095f02014-10-07 15:46:40 -040041 // Populate the request map.
42 authMap := make(map[string]interface{})
43
jrperritt95b74c82015-07-28 20:39:27 -060044 if auth.Username != "" {
Jon Perritta3302e12016-03-07 03:48:59 -060045 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,
jrperritt95b74c82015-07-28 20:39:27 -060054 }
jrperritt1f218c82015-07-29 08:54:18 -060055 } else if auth.TokenID != "" {
jrperritt95b74c82015-07-28 20:39:27 -060056 authMap["token"] = map[string]interface{}{
jrperritt1f218c82015-07-29 08:54:18 -060057 "id": auth.TokenID,
jrperritt95b74c82015-07-28 20:39:27 -060058 }
59 } else {
Jon Perritta3302e12016-03-07 03:48:59 -060060 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 Wilson1cf4d5f2014-10-07 14:16:18 -040065 }
Ash Wilson40095f02014-10-07 15:46:40 -040066
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.
81func Create(client *gophercloud.ServiceClient, auth AuthOptionsBuilder) CreateResult {
82 request, err := auth.ToTokenCreateMap()
83 if err != nil {
Ash Wilsonf548aad2014-10-20 08:35:34 -040084 return CreateResult{gophercloud.Result{Err: err}}
Ash Wilson40095f02014-10-07 15:46:40 -040085 }
Ash Wilson1f110512014-10-02 15:43:47 -040086
87 var result CreateResult
Jamie Hannaford562a7d52015-03-24 16:20:16 +010088 _, result.Err = client.Post(CreateURL(client), request, &result.Body, &gophercloud.RequestOpts{
89 OkCodes: []int{200, 203},
Ash Wilson1f110512014-10-02 15:43:47 -040090 })
91 return result
92}
hzlouchaof6e29262015-10-27 12:51:08 +080093
Jon Perritta3302e12016-03-07 03:48:59 -060094// Get validates and retrieves information for user's token.
hzlouchaof6e29262015-10-27 12:51:08 +080095func Get(client *gophercloud.ServiceClient, token string) GetResult {
hzlouchaob7640892015-11-04 21:37:20 +080096 var result GetResult
97 _, result.Err = client.Get(GetURL(client, token), &result.Body, &gophercloud.RequestOpts{
98 OkCodes: []int{200, 203},
99 })
hzlouchaob7640892015-11-04 21:37:20 +0800100 return result
101}