blob: c25a72b1abcd7b67acf37d948db92b8b69372c6f [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
Ash Wilson22397242014-10-07 16:10:21 -040022// WrapOptions embeds a root AuthOptions struct in a package-specific one.
23func WrapOptions(original gophercloud.AuthOptions) AuthOptions {
24 return AuthOptions{AuthOptions: original}
25}
26
Ash Wilson40095f02014-10-07 15:46:40 -040027// ToTokenCreateMap converts AuthOptions into nested maps that can be serialized into a JSON
28// request.
29func (auth AuthOptions) ToTokenCreateMap() (map[string]interface{}, error) {
Ash Wilson1f110512014-10-02 15:43:47 -040030 // Error out if an unsupported auth option is present.
31 if auth.UserID != "" {
Ash Wilson40095f02014-10-07 15:46:40 -040032 return nil, ErrUserIDProvided
Ash Wilson1f110512014-10-02 15:43:47 -040033 }
Ash Wilson1cf4d5f2014-10-07 14:16:18 -040034 if auth.APIKey != "" {
Ash Wilson40095f02014-10-07 15:46:40 -040035 return nil, ErrAPIKeyProvided
Ash Wilson1cf4d5f2014-10-07 14:16:18 -040036 }
Ash Wilson1f110512014-10-02 15:43:47 -040037 if auth.DomainID != "" {
Ash Wilson40095f02014-10-07 15:46:40 -040038 return nil, ErrDomainIDProvided
Ash Wilson1f110512014-10-02 15:43:47 -040039 }
40 if auth.DomainName != "" {
Ash Wilson40095f02014-10-07 15:46:40 -040041 return nil, ErrDomainNameProvided
Ash Wilson1f110512014-10-02 15:43:47 -040042 }
43
Ash Wilson1cf4d5f2014-10-07 14:16:18 -040044 // Username and Password are always required.
Ash Wilson1f110512014-10-02 15:43:47 -040045 if auth.Username == "" {
Ash Wilson40095f02014-10-07 15:46:40 -040046 return nil, ErrUsernameRequired
Ash Wilson1f110512014-10-02 15:43:47 -040047 }
Ash Wilson1cf4d5f2014-10-07 14:16:18 -040048 if auth.Password == "" {
Ash Wilson40095f02014-10-07 15:46:40 -040049 return nil, ErrPasswordRequired
Ash Wilson1f110512014-10-02 15:43:47 -040050 }
51
Ash Wilson40095f02014-10-07 15:46:40 -040052 // Populate the request map.
53 authMap := make(map[string]interface{})
54
55 authMap["passwordCredentials"] = map[string]interface{}{
56 "username": auth.Username,
57 "password": auth.Password,
Ash Wilson1cf4d5f2014-10-07 14:16:18 -040058 }
Ash Wilson40095f02014-10-07 15:46:40 -040059
60 if auth.TenantID != "" {
61 authMap["tenantId"] = auth.TenantID
62 }
63 if auth.TenantName != "" {
64 authMap["tenantName"] = auth.TenantName
65 }
66
67 return map[string]interface{}{"auth": authMap}, nil
68}
69
70// Create authenticates to the identity service and attempts to acquire a Token.
71// If successful, the CreateResult
72// Generally, rather than interact with this call directly, end users should call openstack.AuthenticatedClient(),
73// which abstracts all of the gory details about navigating service catalogs and such.
74func Create(client *gophercloud.ServiceClient, auth AuthOptionsBuilder) CreateResult {
75 request, err := auth.ToTokenCreateMap()
76 if err != nil {
77 return CreateResult{gophercloud.CommonResult{Err: err}}
78 }
Ash Wilson1f110512014-10-02 15:43:47 -040079
80 var result CreateResult
Ash Wilson1cf4d5f2014-10-07 14:16:18 -040081 _, result.Err = perigee.Request("POST", CreateURL(client), perigee.Options{
Ash Wilson1f110512014-10-02 15:43:47 -040082 ReqBody: &request,
83 Results: &result.Resp,
84 OkCodes: []int{200, 203},
85 })
86 return result
87}