-
Notifications
You must be signed in to change notification settings - Fork 495
/
policy.go
177 lines (158 loc) · 5.21 KB
/
policy.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2014, 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package stateenvirons
import (
"sync"
"github.com/juju/errors"
"github.com/juju/juju/caas"
"github.com/juju/juju/core/constraints"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/state"
"github.com/juju/juju/storage"
"github.com/juju/juju/storage/provider"
)
// environStatePolicy implements state.Policy in
// terms of environs.Environ and related types.
type environStatePolicy struct {
st *state.State
getEnviron NewEnvironFunc
getBroker NewCAASBrokerFunc
checkerMu sync.Mutex
checker deployChecker
}
// deployChecker is the subset of the Environ interface (common to Environ and
// Broker) that we need for pre-checking instances and validating constraints.
type deployChecker interface {
environs.InstancePrechecker
environs.ConstraintsChecker
}
// GetNewPolicyFunc returns a state.NewPolicyFunc that will return
// a state.Policy implemented in terms of either environs.Environ
// or caas.Broker and related types.
func GetNewPolicyFunc() state.NewPolicyFunc {
return func(st *state.State) state.Policy {
return &environStatePolicy{
st: st,
getEnviron: GetNewEnvironFunc(environs.New),
getBroker: GetNewCAASBrokerFunc(caas.New),
}
}
}
// getDeployChecker returns the cached deployChecker instance, or creates a
// new one if it hasn't yet been created and cached.
func (p *environStatePolicy) getDeployChecker() (deployChecker, error) {
p.checkerMu.Lock()
defer p.checkerMu.Unlock()
if p.checker != nil {
return p.checker, nil
}
model, err := p.st.Model()
if err != nil {
return nil, errors.Trace(err)
}
if model.Type() == state.ModelTypeIAAS {
p.checker, err = p.getEnviron(model)
} else {
p.checker, err = p.getBroker(model)
}
return p.checker, err
}
// Prechecker implements state.Policy.
func (p *environStatePolicy) Prechecker() (environs.InstancePrechecker, error) {
return p.getDeployChecker()
}
// ConfigValidator implements state.Policy.
func (p *environStatePolicy) ConfigValidator() (config.Validator, error) {
model, err := p.st.Model()
if err != nil {
return nil, errors.Trace(err)
}
cloud, err := p.st.Cloud(model.CloudName())
if err != nil {
return nil, errors.Annotate(err, "getting cloud")
}
return environProvider(cloud.Type)
}
// ProviderConfigSchemaSource implements state.Policy.
func (p *environStatePolicy) ProviderConfigSchemaSource(cloudName string) (config.ConfigSchemaSource, error) {
cloud, err := p.st.Cloud(cloudName)
if err != nil {
return nil, errors.Trace(err)
}
provider, err := environProvider(cloud.Type)
if err != nil {
return nil, errors.Trace(err)
}
if cs, ok := provider.(config.ConfigSchemaSource); ok {
return cs, nil
}
return nil, errors.NotImplementedf("config.ConfigSource")
}
// ConstraintsValidator implements state.Policy.
func (p *environStatePolicy) ConstraintsValidator(ctx context.ProviderCallContext) (constraints.Validator, error) {
checker, err := p.getDeployChecker()
if err != nil {
return nil, errors.Trace(err)
}
return checker.ConstraintsValidator(ctx)
}
// InstanceDistributor implements state.Policy.
func (p *environStatePolicy) InstanceDistributor() (context.Distributor, error) {
model, err := p.st.Model()
if err != nil {
return nil, errors.Trace(err)
}
if model.Type() != state.ModelTypeIAAS {
// Only IAAS models support machines, hence distribution.
return nil, errors.NotImplementedf("InstanceDistributor")
}
// DistributeInstances doesn't make any calls to fetch instance types,
// so it doesn't help to use getDeployChecker() here.
env, err := p.getEnviron(model)
if err != nil {
return nil, err
}
if d, ok := env.(context.Distributor); ok {
return d, nil
}
return nil, errors.NotImplementedf("InstanceDistributor")
}
// StorageProviderRegistry implements state.Policy.
func (p *environStatePolicy) StorageProviderRegistry() (storage.ProviderRegistry, error) {
model, err := p.st.Model()
if err != nil {
return nil, errors.Trace(err)
}
// ProviderRegistry doesn't make any calls to fetch instance types,
// so it doesn't help to use getDeployChecker() here.
return NewStorageProviderRegistryForModel(model, p.getEnviron, p.getBroker)
}
// NewStorageProviderRegistryForModel returns a storage provider registry
// for the specified model.
func NewStorageProviderRegistryForModel(
model *state.Model,
newEnv NewEnvironFunc,
newBroker NewCAASBrokerFunc,
) (_ storage.ProviderRegistry, err error) {
var reg storage.ProviderRegistry
if model.Type() == state.ModelTypeIAAS {
if reg, err = newEnv(model); err != nil {
return nil, errors.Trace(err)
}
} else {
if reg, err = newBroker(model); err != nil {
return nil, errors.Trace(err)
}
}
return NewStorageProviderRegistry(reg), nil
}
// NewStorageProviderRegistry returns a storage.ProviderRegistry that chains
// the provided registry with the common storage providers.
func NewStorageProviderRegistry(reg storage.ProviderRegistry) storage.ProviderRegistry {
return storage.ChainedProviderRegistry{reg, provider.CommonStorageProviders()}
}
func environProvider(cloudType string) (environs.EnvironProvider, error) {
return environs.Provider(cloudType)
}