forked from juju/juju
/
controller.go
300 lines (261 loc) · 9.74 KB
/
controller.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package modelcmd
import (
"github.com/juju/cmd"
"github.com/juju/errors"
"github.com/juju/gnuflag"
"github.com/juju/juju/api"
"github.com/juju/juju/api/controller"
"github.com/juju/juju/api/modelmanager"
"github.com/juju/juju/api/usermanager"
"github.com/juju/juju/jujuclient"
)
var (
// ErrNoControllersDefined is returned by commands that operate on
// a controller if there is no current controller, no controller has been
// explicitly specified, and there is no default controller.
ErrNoControllersDefined = errors.New(`No controllers registered.
Please either create a new controller using "juju bootstrap" or connect to
another controller that you have been given access to using "juju register".
`)
// ErrNoCurrentController is returned by commands that operate on
// a controller if there is no current controller, no controller has been
// explicitly specified, and there is no default controller but there are
// controllers that client knows about.
ErrNoCurrentController = errors.New(`No selected controller.
Please use "juju switch" to select a controller.
`)
)
// ControllerCommand is intended to be a base for all commands
// that need to operate on controllers as opposed to models.
type ControllerCommand interface {
CommandBase
// SetClientStore is called prior to the wrapped command's Init method
// with the default controller store. It may also be called to override the
// default controller store for testing.
SetClientStore(jujuclient.ClientStore)
// ClientStore returns the controller store that the command is
// associated with.
ClientStore() jujuclient.ClientStore
// SetControllerName sets the value returned by ControllerName.
// It returns an error if the controller with the given name
// is not found, unless the name is empty and allowDefault is true,
// in which case the name of the current controller will be used.
SetControllerName(controllerName string, allowDefault bool) error
// ControllerName returns the name of the controller
// that the command should use.
ControllerName() string
// SetAPIOpener allows the replacement of the default API opener,
// which ends up calling NewAPIRoot
SetAPIOpener(opener APIOpener)
}
// ControllerCommandBase is a convenience type for embedding in commands
// that wish to implement ControllerCommand.
type ControllerCommandBase struct {
JujuCommandBase
store jujuclient.ClientStore
controllerName string
// opener is the strategy used to open the API connection.
opener APIOpener
}
// SetClientStore implements the ControllerCommand interface.
func (c *ControllerCommandBase) SetClientStore(store jujuclient.ClientStore) {
c.store = store
}
// ClientStore implements the ControllerCommand interface.
func (c *ControllerCommandBase) ClientStore() jujuclient.ClientStore {
return c.store
}
// SetControllerName implements ControllerCommand.SetControllerName.
func (c *ControllerCommandBase) SetControllerName(controllerName string, allowDefault bool) error {
store := c.ClientStore()
if controllerName == "" && allowDefault {
currentController, err := store.CurrentController()
if err != nil {
return translateControllerError(store, err)
}
controllerName = currentController
} else if _, err := store.ControllerByName(controllerName); err != nil {
return errors.Trace(err)
}
c.controllerName = controllerName
return nil
}
// ControllerName implements the ControllerCommand interface.
func (c *ControllerCommandBase) ControllerName() string {
return c.controllerName
}
// SetAPIOpener specifies the strategy used by the command to open
// the API connection.
func (c *ControllerCommandBase) SetAPIOpener(opener APIOpener) {
c.opener = opener
}
// NewModelManagerAPIClient returns an API client for the
// ModelManager on the current controller using the current credentials.
func (c *ControllerCommandBase) NewModelManagerAPIClient() (*modelmanager.Client, error) {
root, err := c.NewAPIRoot()
if err != nil {
return nil, errors.Trace(err)
}
return modelmanager.NewClient(root), nil
}
// NewControllerAPIClient returns an API client for the Controller on
// the current controller using the current credentials.
func (c *ControllerCommandBase) NewControllerAPIClient() (*controller.Client, error) {
root, err := c.NewAPIRoot()
if err != nil {
return nil, errors.Trace(err)
}
return controller.NewClient(root), nil
}
// NewUserManagerAPIClient returns an API client for the UserManager on the
// current controller using the current credentials.
func (c *ControllerCommandBase) NewUserManagerAPIClient() (*usermanager.Client, error) {
root, err := c.NewAPIRoot()
if err != nil {
return nil, errors.Trace(err)
}
return usermanager.NewClient(root), nil
}
// NewAPIRoot returns a restricted API for the current controller using the current
// credentials. Only the UserManager and ModelManager may be accessed
// through this API connection.
func (c *ControllerCommandBase) NewAPIRoot() (api.Connection, error) {
return c.newAPIRoot("")
}
// NewAPIRoot returns a new connection to the API server for the named model
// in the specified controller.
func (c *ControllerCommandBase) NewModelAPIRoot(modelName string) (api.Connection, error) {
_, err := c.store.ModelByName(c.controllerName, modelName)
if err != nil {
if !errors.IsNotFound(err) {
return nil, errors.Trace(err)
}
// The model isn't known locally, so query the models
// available in the controller, and cache them locally.
if err := c.RefreshModels(c.store, c.controllerName); err != nil {
return nil, errors.Annotate(err, "refreshing models")
}
}
return c.newAPIRoot(modelName)
}
func (c *ControllerCommandBase) newAPIRoot(modelName string) (api.Connection, error) {
if c.controllerName == "" {
controllers, err := c.store.AllControllers()
if err != nil {
return nil, errors.Trace(err)
}
if len(controllers) == 0 {
return nil, errors.Trace(ErrNoControllersDefined)
}
return nil, errors.Trace(ErrNoCurrentController)
}
opener := c.opener
if opener == nil {
opener = OpenFunc(c.JujuCommandBase.NewAPIRoot)
}
return opener.Open(c.store, c.controllerName, modelName)
}
// ModelUUIDs returns the model UUIDs for the given model names.
func (c *ControllerCommandBase) ModelUUIDs(modelNames []string) ([]string, error) {
var result []string
store := c.ClientStore()
controllerName := c.ControllerName()
for _, modelName := range modelNames {
model, err := store.ModelByName(controllerName, modelName)
if errors.IsNotFound(err) {
// The model isn't known locally, so query the models available in the controller.
logger.Infof("model %q not cached locally, refreshing models from controller", modelName)
if err := c.RefreshModels(store, controllerName); err != nil {
return nil, errors.Annotatef(err, "refreshing model %q", modelName)
}
model, err = store.ModelByName(controllerName, modelName)
}
if err != nil {
return nil, errors.Annotatef(err, "model %q not found", modelName)
}
result = append(result, model.ModelUUID)
}
return result, nil
}
// WrapControllerOption specifies an option to the WrapController function.
type WrapControllerOption func(*sysCommandWrapper)
// Options for the WrapController call.
var (
// WrapControllerSkipControllerFlags specifies that the -c
// and --controller flag flags should not be defined.
WrapControllerSkipControllerFlags WrapControllerOption = wrapControllerSkipControllerFlags
// WrapSkipDefaultModel specifies that no default controller should
// be used.
WrapControllerSkipDefaultController WrapControllerOption = wrapControllerSkipDefaultController
)
func wrapControllerSkipControllerFlags(w *sysCommandWrapper) {
w.setControllerFlags = false
}
func wrapControllerSkipDefaultController(w *sysCommandWrapper) {
w.useDefaultController = false
}
// WrapControllerAPIOpener specifies that the given APIOpener
// should should be used to open the API connection when
// NewAPIRoot or NewControllerAPIRoot are called.
func WrapControllerAPIOpener(opener APIOpener) WrapControllerOption {
return func(w *sysCommandWrapper) {
w.ControllerCommand.SetAPIOpener(opener)
}
}
// WrapController wraps the specified ControllerCommand, returning a Command
// that proxies to each of the ControllerCommand methods.
func WrapController(c ControllerCommand, options ...WrapControllerOption) cmd.Command {
wrapper := &sysCommandWrapper{
ControllerCommand: c,
setControllerFlags: true,
useDefaultController: true,
}
for _, option := range options {
option(wrapper)
}
return WrapBase(wrapper)
}
type sysCommandWrapper struct {
ControllerCommand
setControllerFlags bool
useDefaultController bool
controllerName string
}
// SetFlags implements Command.SetFlags, then calls the wrapped command's SetFlags.
func (w *sysCommandWrapper) SetFlags(f *gnuflag.FlagSet) {
if w.setControllerFlags {
f.StringVar(&w.controllerName, "c", "", "Controller to operate in")
f.StringVar(&w.controllerName, "controller", "", "")
}
w.ControllerCommand.SetFlags(f)
}
// Init implements Command.Init, then calls the wrapped command's Init.
func (w *sysCommandWrapper) Init(args []string) error {
store := w.ClientStore()
if store == nil {
store = jujuclient.NewFileClientStore()
}
store = QualifyingClientStore{store}
w.SetClientStore(store)
if w.setControllerFlags {
if err := w.SetControllerName(w.controllerName, w.useDefaultController); err != nil {
return errors.Trace(err)
}
}
return w.ControllerCommand.Init(args)
}
func translateControllerError(store jujuclient.ClientStore, err error) error {
if !errors.IsNotFound(err) {
return err
}
controllers, err2 := store.AllControllers()
if err2 != nil {
return err2
}
if len(controllers) == 0 {
return errors.Wrap(err, ErrNoControllersDefined)
}
return errors.Wrap(err, ErrNoCurrentController)
}