-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
103 lines (87 loc) · 3.23 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
// Copyright 2011, 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package environs
import (
"fmt"
"github.com/juju/errors"
"github.com/juju/loggo"
)
var logger = loggo.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
// RegisteredProviders returns the names of the registered environment
// providers.
RegisteredProviders() []string
// Provider returns the environment provider with the specified name.
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
}
func (r *globalProviderRegistry) RegisteredProviders() []string {
var p []string
for k := range r.providers {
p = append(p, k)
}
return p
}
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.
func RegisterProvider(name string, p EnvironProvider, alias ...string) {
if err := GlobalProviderRegistry().RegisterProvider(p, name, alias...); err != nil {
panic(fmt.Errorf("juju: %v", err))
}
}
// 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)
}