blob: a2a068514b9edcfda6363467ade473112a1731fe [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 Wilson40095f02014-10-07 15:46:40 -04007 // ToTokenCreateMap assembles the Create request body, returning an error if parameters are
8 // missing or inconsistent.
Jon Perrittdb0ae142016-03-13 00:33:41 -06009 ToTokenV2CreateMap() (map[string]interface{}, error)
Ash Wilson40095f02014-10-07 15:46:40 -040010}
11
12// Create authenticates to the identity service and attempts to acquire a Token.
13// If successful, the CreateResult
14// Generally, rather than interact with this call directly, end users should call openstack.AuthenticatedClient(),
15// which abstracts all of the gory details about navigating service catalogs and such.
16func Create(client *gophercloud.ServiceClient, auth AuthOptionsBuilder) CreateResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060017 var r CreateResult
18 b, err := auth.ToTokenV2CreateMap()
Ash Wilson40095f02014-10-07 15:46:40 -040019 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060020 r.Err = err
21 return r
Ash Wilson40095f02014-10-07 15:46:40 -040022 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060023 _, r.Err = client.Post(CreateURL(client), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford562a7d52015-03-24 16:20:16 +010024 OkCodes: []int{200, 203},
Ash Wilson1f110512014-10-02 15:43:47 -040025 })
Jon Perrittdb0ae142016-03-13 00:33:41 -060026 return r
Ash Wilson1f110512014-10-02 15:43:47 -040027}
hzlouchaof6e29262015-10-27 12:51:08 +080028
Jon Perritta3302e12016-03-07 03:48:59 -060029// Get validates and retrieves information for user's token.
hzlouchaof6e29262015-10-27 12:51:08 +080030func Get(client *gophercloud.ServiceClient, token string) GetResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060031 var r GetResult
32 _, r.Err = client.Get(GetURL(client, token), &r.Body, &gophercloud.RequestOpts{
hzlouchaob7640892015-11-04 21:37:20 +080033 OkCodes: []int{200, 203},
34 })
Jon Perrittdb0ae142016-03-13 00:33:41 -060035 return r
hzlouchaob7640892015-11-04 21:37:20 +080036}