blob: efa054fb399437912ef0095f0225110f70448f8b [file] [log] [blame]
Ash Wilson1f110512014-10-02 15:43:47 -04001package tokens
2
Ash Wilson4bf41a32015-02-12 15:52:44 -05003import "github.com/rackspace/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 Wilson1cf4d5f2014-10-07 14:16:18 -040041 // Username and Password are always required.
Ash Wilson1f110512014-10-02 15:43:47 -040042 if auth.Username == "" {
Ash Wilson40095f02014-10-07 15:46:40 -040043 return nil, ErrUsernameRequired
Ash Wilson1f110512014-10-02 15:43:47 -040044 }
Ash Wilson1cf4d5f2014-10-07 14:16:18 -040045 if auth.Password == "" {
Ash Wilson40095f02014-10-07 15:46:40 -040046 return nil, ErrPasswordRequired
Ash Wilson1f110512014-10-02 15:43:47 -040047 }
48
Ash Wilson40095f02014-10-07 15:46:40 -040049 // 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 Wilson1cf4d5f2014-10-07 14:16:18 -040055 }
Ash Wilson40095f02014-10-07 15:46:40 -040056
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.
71func Create(client *gophercloud.ServiceClient, auth AuthOptionsBuilder) CreateResult {
72 request, err := auth.ToTokenCreateMap()
73 if err != nil {
Ash Wilsonf548aad2014-10-20 08:35:34 -040074 return CreateResult{gophercloud.Result{Err: err}}
Ash Wilson40095f02014-10-07 15:46:40 -040075 }
Ash Wilson1f110512014-10-02 15:43:47 -040076
77 var result CreateResult
Jamie Hannaford562a7d52015-03-24 16:20:16 +010078 _, result.Err = client.Post(CreateURL(client), request, &result.Body, &gophercloud.RequestOpts{
79 OkCodes: []int{200, 203},
Ash Wilson1f110512014-10-02 15:43:47 -040080 })
81 return result
82}