Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
Samuel A. Falvo II | 2d0f6da | 2013-07-15 16:41:52 -0700 | [diff] [blame] | 3 | import ( |
| 4 | "github.com/racker/perigee" |
| 5 | ) |
| 6 | |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 7 | // globalContext is the, well, "global context." |
| 8 | // Most of this SDK is written in a manner to facilitate easier testing, |
| 9 | // which doesn't require all the configuration a real-world application would require. |
| 10 | // However, for real-world deployments, applications should be able to rely on a consistent configuration of providers, etc. |
| 11 | var globalContext *Context |
| 12 | |
| 13 | // providers is the set of supported providers. |
| 14 | var providers = map[string]Provider{ |
Samuel A. Falvo II | c0b07aa | 2013-08-19 17:13:06 -0700 | [diff] [blame] | 15 | "rackspace-us": { |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 16 | AuthEndpoint: "https://identity.api.rackspacecloud.com/v2.0/tokens", |
| 17 | }, |
Samuel A. Falvo II | c0b07aa | 2013-08-19 17:13:06 -0700 | [diff] [blame] | 18 | "rackspace-uk": { |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 19 | AuthEndpoint: "https://lon.identity.api.rackspacecloud.com/v2.0/tokens", |
| 20 | }, |
| 21 | } |
| 22 | |
| 23 | // Initialize the global context to sane configuration. |
| 24 | // The Go runtime ensures this function is called before main(), |
| 25 | // thus guaranteeing proper configuration before your application ever runs. |
| 26 | func init() { |
| 27 | globalContext = TestContext() |
| 28 | for name, descriptor := range providers { |
| 29 | globalContext.RegisterProvider(name, descriptor) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | // Authenticate() grants access to the OpenStack-compatible provider API. |
| 34 | // |
| 35 | // Providers are identified through a unique key string. |
| 36 | // Specifying an unsupported provider will result in an ErrProvider error. |
Samuel A. Falvo II | 18087f4 | 2013-10-24 16:45:58 -0700 | [diff] [blame] | 37 | // However, you may also specify a custom Identity API URL. |
| 38 | // Any provider name that contains the characters "://", in that order, will be treated as a custom Identity API URL. |
| 39 | // Custom URLs, important for private cloud deployments, overrides all provider configurations. |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 40 | // |
| 41 | // The supplied AuthOptions instance allows the client to specify only those credentials |
| 42 | // relevant for the authentication request. At present, support exists for OpenStack |
| 43 | // Identity V2 API only; support for V3 will become available as soon as documentation for it |
| 44 | // becomes readily available. |
| 45 | // |
| 46 | // For Identity V2 API requirements, you must provide at least the Username and Password |
| 47 | // options. The TenantId field is optional, and defaults to "". |
| 48 | func Authenticate(provider string, options AuthOptions) (*Access, error) { |
| 49 | return globalContext.Authenticate(provider, options) |
| 50 | } |
| 51 | |
| 52 | // Instantiates a Cloud Servers object for the provider given. |
Samuel A. Falvo II | 1dd740a | 2013-07-08 15:48:40 -0700 | [diff] [blame] | 53 | func ServersApi(acc AccessProvider, criteria ApiCriteria) (CloudServersProvider, error) { |
| 54 | return globalContext.ServersApi(acc, criteria) |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 55 | } |
Samuel A. Falvo II | 2d0f6da | 2013-07-15 16:41:52 -0700 | [diff] [blame] | 56 | |
| 57 | // ActualResponseCode inspects a returned error, and discovers the actual response actual |
| 58 | // response code that caused the error to be raised. |
| 59 | func ActualResponseCode(e error) (int, error) { |
| 60 | err, ok := e.(*perigee.UnexpectedResponseCodeError) |
| 61 | if !ok { |
| 62 | return 0, ErrError |
| 63 | } |
| 64 | return err.Actual, nil |
Samuel A. Falvo II | 20f1aa4 | 2013-07-31 14:32:03 -0700 | [diff] [blame] | 65 | } |