forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
131 lines (111 loc) · 4.19 KB
/
config.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2011, 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package environs
import (
"context"
"fmt"
"github.com/juju/errors"
"github.com/juju/juju/cloud"
internallogger "github.com/juju/juju/internal/logger"
)
var logger = internallogger.GetLogger("juju.environs")
// ProviderRegistry is an interface that provides methods for registering
// and obtaining environment providers by provider name.
type ProviderRegistry interface {
// RegisterProvider registers a new environment provider with the given
// name, and zero or more aliases. If a provider already exists with the
// given name or alias, an error will be returned.
RegisterProvider(p EnvironProvider, providerType string, providerTypeAliases ...string) error
// UnregisterProvider unregisters the environment provider with the given name.
UnregisterProvider(providerType string)
// RegisteredProviders returns the names of the registered environment
// providers.
RegisteredProviders() []string
// Provider returns the environment provider with the specified name. If no
// provider has been registered with the supplied name then an error
// satisfying errors.NotFound is returned.
Provider(providerType string) (EnvironProvider, error)
}
// GlobalProviderRegistry returns the global provider registry.
func GlobalProviderRegistry() ProviderRegistry {
return globalProviders
}
type globalProviderRegistry struct {
// providers maps from provider type to EnvironProvider for
// each registered provider type.
providers map[string]EnvironProvider
// providerAliases is a map of provider type aliases.
aliases map[string]string
}
var globalProviders = &globalProviderRegistry{
providers: map[string]EnvironProvider{},
aliases: map[string]string{},
}
func (r *globalProviderRegistry) RegisterProvider(p EnvironProvider, providerType string, providerTypeAliases ...string) error {
if r.providers[providerType] != nil || r.aliases[providerType] != "" {
return errors.Errorf("duplicate provider name %q", providerType)
}
r.providers[providerType] = p
for _, alias := range providerTypeAliases {
if r.providers[alias] != nil || r.aliases[alias] != "" {
return errors.Errorf("duplicate provider alias %q", alias)
}
r.aliases[alias] = providerType
}
return nil
}
// UnregisterProvider removes the named provider from the list of available providers.
func (r *globalProviderRegistry) UnregisterProvider(providerType string) {
delete(r.providers, providerType)
for a, p := range r.aliases {
if p == providerType {
delete(r.aliases, a)
}
}
}
func (r *globalProviderRegistry) RegisteredProviders() []string {
var p []string
for k := range r.providers {
p = append(p, k)
}
return p
}
// Provider implements ProviderRegistry.Provider()
func (r *globalProviderRegistry) Provider(providerType string) (EnvironProvider, error) {
if alias, ok := r.aliases[providerType]; ok {
providerType = alias
}
p, ok := r.providers[providerType]
if !ok {
return nil, errors.NewNotFound(
nil, fmt.Sprintf("no registered provider for %q", providerType),
)
}
return p, nil
}
// RegisterProvider registers a new environment provider. Name gives the name
// of the provider, and p the interface to that provider.
//
// RegisterProvider will panic if the provider name or any of the aliases
// are registered more than once.
// The return function can be used to unregister the provider and is used by tests.
func RegisterProvider(name string, p CloudEnvironProvider, alias ...string) (unregister func()) {
if err := GlobalProviderRegistry().RegisterProvider(p, name, alias...); err != nil {
panic(fmt.Errorf("juju: %v", err))
}
return func() {
GlobalProviderRegistry().UnregisterProvider(name)
}
}
// RegisteredProviders enumerate all the environ providers which have been registered.
func RegisteredProviders() []string {
return GlobalProviderRegistry().RegisteredProviders()
}
// Provider returns the previously registered provider with the given type.
func Provider(providerType string) (EnvironProvider, error) {
return GlobalProviderRegistry().Provider(providerType)
}
// CloudService provides access to clouds.
type CloudService interface {
Cloud(ctx context.Context, name string) (*cloud.Cloud, error)
}