blob: bb068b632062823bdbbe9796d6e84e90417d50ee [file] [log] [blame]
Ash Wilson31844f22014-09-08 15:32:58 -04001package v2
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -08002
3import (
4 "github.com/racker/perigee"
Ash Wilson12b3b6c2014-09-08 14:54:38 -04005 "github.com/rackspace/gophercloud"
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -08006)
7
Samuel A. Falvo II2b963212014-02-09 02:12:30 -08008// AuthResults encapsulates the raw results from an authentication request.
9// As OpenStack allows extensions to influence the structure returned in
10// ways that Gophercloud cannot predict at compile-time, you should use
11// type-safe accessors to work with the data represented by this type,
12// such as ServiceCatalog() and Token().
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080013type AuthResults map[string]interface{}
14
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080015// Authenticate passes the supplied credentials to the OpenStack provider for authentication.
16// If successful, the caller may use Token() to retrieve the authentication token,
17// and ServiceCatalog() to retrieve the set of services available to the API user.
Ash Wilson9c6c9e62014-09-08 15:27:38 -040018func Authenticate(c *gophercloud.ServiceClient, options gophercloud.AuthOptions) (AuthResults, error) {
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080019 type AuthContainer struct {
20 Auth auth `json:"auth"`
21 }
22
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080023 var ar AuthResults
24
Ash Wilson9c6c9e62014-09-08 15:27:38 -040025 if c.Endpoint == "" {
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080026 return nil, ErrEndpoint
27 }
28
Ash Wilson12b3b6c2014-09-08 14:54:38 -040029 if (options.Username == "") || (options.Password == "" && options.APIKey == "") {
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080030 return nil, ErrCredentials
31 }
32
Ash Wilson11c98282014-09-08 16:05:10 -040033 url := c.Endpoint + "tokens"
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080034 err := perigee.Post(url, perigee.Options{
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080035 ReqBody: &AuthContainer{
36 Auth: getAuthCredentials(options),
37 },
38 Results: &ar,
39 })
40 return ar, err
41}
42
Ash Wilson12b3b6c2014-09-08 14:54:38 -040043func getAuthCredentials(options gophercloud.AuthOptions) auth {
44 if options.APIKey == "" {
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080045 return auth{
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080046 PasswordCredentials: &struct {
47 Username string `json:"username"`
48 Password string `json:"password"`
49 }{
50 Username: options.Username,
51 Password: options.Password,
52 },
Ash Wilson12b3b6c2014-09-08 14:54:38 -040053 TenantID: options.TenantID,
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080054 TenantName: options.TenantName,
55 }
Ash Wilson12b3b6c2014-09-08 14:54:38 -040056 }
57 return auth{
58 APIKeyCredentials: &struct {
59 Username string `json:"username"`
60 APIKey string `json:"apiKey"`
61 }{
62 Username: options.Username,
63 APIKey: options.APIKey,
64 },
65 TenantID: options.TenantID,
66 TenantName: options.TenantName,
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080067 }
68}
69
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080070type auth struct {
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080071 PasswordCredentials interface{} `json:"passwordCredentials,omitempty"`
Ash Wilson12b3b6c2014-09-08 14:54:38 -040072 APIKeyCredentials interface{} `json:"RAX-KSKEY:apiKeyCredentials,omitempty"`
73 TenantID string `json:"tenantId,omitempty"`
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080074 TenantName string `json:"tenantName,omitempty"`
75}
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080076
Ash Wilson12b3b6c2014-09-08 14:54:38 -040077// GetExtensions returns the OpenStack extensions available from this service.
Ash Wilson9c6c9e62014-09-08 15:27:38 -040078func GetExtensions(c *gophercloud.ServiceClient, options gophercloud.AuthOptions) (ExtensionsResult, error) {
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080079 var exts ExtensionsResult
80
Ash Wilson11c98282014-09-08 16:05:10 -040081 url := c.Endpoint + "extensions"
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080082 err := perigee.Get(url, perigee.Options{
83 Results: &exts,
84 })
85 return exts, err
86}