Ash Wilson | 31844f2 | 2014-09-08 15:32:58 -0400 | [diff] [blame] | 1 | package v2 |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "github.com/racker/perigee" |
Ash Wilson | 12b3b6c | 2014-09-08 14:54:38 -0400 | [diff] [blame] | 5 | "github.com/rackspace/gophercloud" |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 6 | ) |
| 7 | |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame] | 8 | // 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 II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 13 | type AuthResults map[string]interface{} |
| 14 | |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame] | 15 | // 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 Wilson | 9c6c9e6 | 2014-09-08 15:27:38 -0400 | [diff] [blame] | 18 | func Authenticate(c *gophercloud.ServiceClient, options gophercloud.AuthOptions) (AuthResults, error) { |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame] | 19 | type AuthContainer struct { |
| 20 | Auth auth `json:"auth"` |
| 21 | } |
| 22 | |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 23 | var ar AuthResults |
| 24 | |
Ash Wilson | 9c6c9e6 | 2014-09-08 15:27:38 -0400 | [diff] [blame] | 25 | if c.Endpoint == "" { |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 26 | return nil, ErrEndpoint |
| 27 | } |
| 28 | |
Ash Wilson | 12b3b6c | 2014-09-08 14:54:38 -0400 | [diff] [blame] | 29 | if (options.Username == "") || (options.Password == "" && options.APIKey == "") { |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 30 | return nil, ErrCredentials |
| 31 | } |
| 32 | |
Ash Wilson | 11c9828 | 2014-09-08 16:05:10 -0400 | [diff] [blame] | 33 | url := c.Endpoint + "tokens" |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame] | 34 | err := perigee.Post(url, perigee.Options{ |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 35 | ReqBody: &AuthContainer{ |
| 36 | Auth: getAuthCredentials(options), |
| 37 | }, |
| 38 | Results: &ar, |
| 39 | }) |
| 40 | return ar, err |
| 41 | } |
| 42 | |
Ash Wilson | 12b3b6c | 2014-09-08 14:54:38 -0400 | [diff] [blame] | 43 | func getAuthCredentials(options gophercloud.AuthOptions) auth { |
| 44 | if options.APIKey == "" { |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame] | 45 | return auth{ |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 46 | PasswordCredentials: &struct { |
| 47 | Username string `json:"username"` |
| 48 | Password string `json:"password"` |
| 49 | }{ |
| 50 | Username: options.Username, |
| 51 | Password: options.Password, |
| 52 | }, |
Ash Wilson | 12b3b6c | 2014-09-08 14:54:38 -0400 | [diff] [blame] | 53 | TenantID: options.TenantID, |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 54 | TenantName: options.TenantName, |
| 55 | } |
Ash Wilson | 12b3b6c | 2014-09-08 14:54:38 -0400 | [diff] [blame] | 56 | } |
| 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 II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame] | 70 | type auth struct { |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 71 | PasswordCredentials interface{} `json:"passwordCredentials,omitempty"` |
Ash Wilson | 12b3b6c | 2014-09-08 14:54:38 -0400 | [diff] [blame] | 72 | APIKeyCredentials interface{} `json:"RAX-KSKEY:apiKeyCredentials,omitempty"` |
| 73 | TenantID string `json:"tenantId,omitempty"` |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 74 | TenantName string `json:"tenantName,omitempty"` |
| 75 | } |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame] | 76 | |
Ash Wilson | 12b3b6c | 2014-09-08 14:54:38 -0400 | [diff] [blame] | 77 | // GetExtensions returns the OpenStack extensions available from this service. |
Ash Wilson | 9c6c9e6 | 2014-09-08 15:27:38 -0400 | [diff] [blame] | 78 | func GetExtensions(c *gophercloud.ServiceClient, options gophercloud.AuthOptions) (ExtensionsResult, error) { |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame] | 79 | var exts ExtensionsResult |
| 80 | |
Ash Wilson | 11c9828 | 2014-09-08 16:05:10 -0400 | [diff] [blame] | 81 | url := c.Endpoint + "extensions" |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame] | 82 | err := perigee.Get(url, perigee.Options{ |
| 83 | Results: &exts, |
| 84 | }) |
| 85 | return exts, err |
| 86 | } |