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