blob: 89d283b1bc302b9f194eb2460fbb7265a471cb25 [file] [log] [blame]
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -07001package gophercloud
2
Samuel A. Falvo II2d0f6da2013-07-15 16:41:52 -07003import (
4 "github.com/racker/perigee"
5)
6
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -07007// 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.
11var globalContext *Context
12
13// providers is the set of supported providers.
14var providers = map[string]Provider{
Samuel A. Falvo IIc0b07aa2013-08-19 17:13:06 -070015 "rackspace-us": {
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070016 AuthEndpoint: "https://identity.api.rackspacecloud.com/v2.0/tokens",
17 },
Samuel A. Falvo IIc0b07aa2013-08-19 17:13:06 -070018 "rackspace-uk": {
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070019 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.
26func 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 II18087f42013-10-24 16:45:58 -070037// 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 II2e2b8772013-07-04 15:40:15 -070040//
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 "".
48func 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 II1dd740a2013-07-08 15:48:40 -070053func ServersApi(acc AccessProvider, criteria ApiCriteria) (CloudServersProvider, error) {
54 return globalContext.ServersApi(acc, criteria)
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070055}
Samuel A. Falvo II2d0f6da2013-07-15 16:41:52 -070056
57// ActualResponseCode inspects a returned error, and discovers the actual response actual
58// response code that caused the error to be raised.
59func ActualResponseCode(e error) (int, error) {
John Hoppere2bc7022014-06-14 11:30:20 -050060 if err, typeOk := e.(*perigee.UnexpectedResponseCodeError); typeOk {
61 return err.Actual, nil
62 } else if err, typeOk := e.(*AuthError); typeOk{
63 return err.StatusCode, nil
Samuel A. Falvo II2d0f6da2013-07-15 16:41:52 -070064 }
John Hoppere2bc7022014-06-14 11:30:20 -050065
66 return 0, ErrError
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -070067}