blob: 71ff757f82f156018b0c9f32015bc1f743b4895a [file] [log] [blame]
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -07001package gophercloud
2
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -07003import (
4 "net/http"
5)
6
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -07007// Provider structures exist for each tangible provider of OpenStack service.
8// For example, Rackspace, Hewlett-Packard, and NASA might have their own instance of this structure.
9//
10// At a minimum, a provider must expose an authentication endpoint.
11type Provider struct {
12 AuthEndpoint string
13}
14
Samuel A. Falvo II4e895182013-06-26 15:44:18 -070015// Context structures encapsulate Gophercloud-global state in a manner which
16// facilitates easier unit testing. As a user of this SDK, you'll never
17// have to use this structure, except when contributing new code to the SDK.
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -070018type Context struct {
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070019 // providerMap serves as a directory of supported providers.
Samuel A. Falvo II4e895182013-06-26 15:44:18 -070020 providerMap map[string]Provider
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070021
22 // httpClient refers to the current HTTP client interface to use.
23 httpClient *http.Client
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -070024}
25
Samuel A. Falvo II4e895182013-06-26 15:44:18 -070026// TestContext yields a new Context instance, pre-initialized with a barren
27// state suitable for per-unit-test customization. This configuration consists
28// of:
29//
30// * An empty provider map.
31//
32// * An HTTP client built by the net/http package (see http://godoc.org/net/http#Client).
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -070033func TestContext() *Context {
34 return &Context{
Samuel A. Falvo II4e895182013-06-26 15:44:18 -070035 providerMap: make(map[string]Provider),
Samuel A. Falvo II839428e2013-06-25 18:02:24 -070036 httpClient: &http.Client{},
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -070037 }
38}
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070039
Samuel A. Falvo II4e895182013-06-26 15:44:18 -070040// UseCustomClient configures the context to use a customized HTTP client
41// instance. By default, TestContext() will return a Context which uses
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -070042// the net/http package's default client instance.
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070043func (c *Context) UseCustomClient(hc *http.Client) *Context {
Samuel A. Falvo II839428e2013-06-25 18:02:24 -070044 c.httpClient = hc
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070045 return c
46}
47
48// RegisterProvider allows a unit test to register a mythical provider convenient for testing.
49// If the provider structure lacks adequate configuration, or the configuration given has some
50// detectable error, an ErrConfiguration error will result.
51func (c *Context) RegisterProvider(name string, p Provider) error {
52 if p.AuthEndpoint == "" {
53 return ErrConfiguration
54 }
55
56 c.providerMap[name] = p
57 return nil
58}
59
60// WithProvider offers convenience for unit tests.
61func (c *Context) WithProvider(name string, p Provider) *Context {
62 err := c.RegisterProvider(name, p)
63 if err != nil {
64 panic(err)
65 }
66 return c
67}
68
69// ProviderByName will locate a provider amongst those previously registered, if it exists.
70// If the named provider has not been registered, an ErrProvider error will result.
71func (c *Context) ProviderByName(name string) (p Provider, err error) {
72 for provider, descriptor := range c.providerMap {
73 if name == provider {
74 return descriptor, nil
75 }
76 }
77 return Provider{}, ErrProvider
78}
79
80// Instantiates a Cloud Servers object for the provider given.
81func (c *Context) ComputeApi(acc AccessProvider, criteria ApiCriteria) (ComputeProvider, error) {
82 url := acc.FirstEndpointUrlByCriteria(criteria)
83 if url == "" {
84 return nil, ErrEndpoint
85 }
86
87 gcp := &genericCloudProvider{
88 endpoint: url,
89 context: c,
90 }
91
92 return gcp, nil
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070093}