blob: 96c28cf8ff1715daaa56d85335805bf1ce5bc3c3 [file] [log] [blame]
Ash Wilson1f110512014-10-02 15:43:47 -04001package tokens
2
3import (
4 "github.com/racker/perigee"
5 "github.com/rackspace/gophercloud"
6)
7
Ash Wilson40095f02014-10-07 15:46:40 -04008// AuthOptionsBuilder describes any argument that may be passed to the Create call.
9type AuthOptionsBuilder interface {
Ash Wilsonaa197a92014-10-03 11:38:08 -040010
Ash Wilson40095f02014-10-07 15:46:40 -040011 // ToTokenCreateMap assembles the Create request body, returning an error if parameters are
12 // missing or inconsistent.
13 ToTokenCreateMap() (map[string]interface{}, error)
14}
Ash Wilson1f110512014-10-02 15:43:47 -040015
Ash Wilson40095f02014-10-07 15:46:40 -040016// AuthOptions wraps a gophercloud AuthOptions in order to adhere to the AuthOptionsBuilder
17// interface.
18type AuthOptions struct {
19 gophercloud.AuthOptions
20}
21
22// ToTokenCreateMap converts AuthOptions into nested maps that can be serialized into a JSON
23// request.
24func (auth AuthOptions) ToTokenCreateMap() (map[string]interface{}, error) {
Ash Wilson1f110512014-10-02 15:43:47 -040025 // Error out if an unsupported auth option is present.
26 if auth.UserID != "" {
Ash Wilson40095f02014-10-07 15:46:40 -040027 return nil, ErrUserIDProvided
Ash Wilson1f110512014-10-02 15:43:47 -040028 }
Ash Wilson1cf4d5f2014-10-07 14:16:18 -040029 if auth.APIKey != "" {
Ash Wilson40095f02014-10-07 15:46:40 -040030 return nil, ErrAPIKeyProvided
Ash Wilson1cf4d5f2014-10-07 14:16:18 -040031 }
Ash Wilson1f110512014-10-02 15:43:47 -040032 if auth.DomainID != "" {
Ash Wilson40095f02014-10-07 15:46:40 -040033 return nil, ErrDomainIDProvided
Ash Wilson1f110512014-10-02 15:43:47 -040034 }
35 if auth.DomainName != "" {
Ash Wilson40095f02014-10-07 15:46:40 -040036 return nil, ErrDomainNameProvided
Ash Wilson1f110512014-10-02 15:43:47 -040037 }
38
Ash Wilson1cf4d5f2014-10-07 14:16:18 -040039 // Username and Password are always required.
Ash Wilson1f110512014-10-02 15:43:47 -040040 if auth.Username == "" {
Ash Wilson40095f02014-10-07 15:46:40 -040041 return nil, ErrUsernameRequired
Ash Wilson1f110512014-10-02 15:43:47 -040042 }
Ash Wilson1cf4d5f2014-10-07 14:16:18 -040043 if auth.Password == "" {
Ash Wilson40095f02014-10-07 15:46:40 -040044 return nil, ErrPasswordRequired
Ash Wilson1f110512014-10-02 15:43:47 -040045 }
46
Ash Wilson40095f02014-10-07 15:46:40 -040047 // Populate the request map.
48 authMap := make(map[string]interface{})
49
50 authMap["passwordCredentials"] = map[string]interface{}{
51 "username": auth.Username,
52 "password": auth.Password,
Ash Wilson1cf4d5f2014-10-07 14:16:18 -040053 }
Ash Wilson40095f02014-10-07 15:46:40 -040054
55 if auth.TenantID != "" {
56 authMap["tenantId"] = auth.TenantID
57 }
58 if auth.TenantName != "" {
59 authMap["tenantName"] = auth.TenantName
60 }
61
62 return map[string]interface{}{"auth": authMap}, nil
63}
64
65// Create authenticates to the identity service and attempts to acquire a Token.
66// If successful, the CreateResult
67// Generally, rather than interact with this call directly, end users should call openstack.AuthenticatedClient(),
68// which abstracts all of the gory details about navigating service catalogs and such.
69func Create(client *gophercloud.ServiceClient, auth AuthOptionsBuilder) CreateResult {
70 request, err := auth.ToTokenCreateMap()
71 if err != nil {
72 return CreateResult{gophercloud.CommonResult{Err: err}}
73 }
Ash Wilson1f110512014-10-02 15:43:47 -040074
75 var result CreateResult
Ash Wilson1cf4d5f2014-10-07 14:16:18 -040076 _, result.Err = perigee.Request("POST", CreateURL(client), perigee.Options{
Ash Wilson1f110512014-10-02 15:43:47 -040077 ReqBody: &request,
78 Results: &result.Resp,
79 OkCodes: []int{200, 203},
80 })
81 return result
82}