12
11
"github.com/juju/cmd"
13
12
"github.com/juju/errors"
14
13
"github.com/juju/loggo"
16
14
"launchpad.net/gnuflag"
18
16
"github.com/juju/juju/api"
19
17
"github.com/juju/juju/environs"
20
"github.com/juju/juju/environs/configstore"
21
18
"github.com/juju/juju/juju/osenv"
22
19
"github.com/juju/juju/jujuclient"
25
var logger = loggo.GetLogger("juju.cmd.envcmd")
22
var logger = loggo.GetLogger("juju.cmd.modelcmd")
27
24
// ErrNoModelSpecified is returned by commands that operate on
28
25
// an environment if there is no current model, no model
29
26
// has been explicitly specified, and there is no default model.
30
var ErrNoModelSpecified = errors.New("no model specified")
27
var ErrNoModelSpecified = errors.New(`no model specified
29
There is no current model specified for the current controller,
30
and none specified on the command line. Please use "juju switch"
31
to set the current model, or specify a model on the command line
32
35
// GetCurrentModel returns the name of the current Juju model.
199
205
if opener == nil {
200
206
opener = OpenFunc(c.JujuCommandBase.NewAPIRoot)
202
// TODO(axw) stop checking c.store != nil once we've updated all the
203
// tests, and have excised configstore.
204
if c.modelName != "" && c.controllerName != "" && c.store != nil {
205
_, err := c.store.ModelByName(c.controllerName, c.accountName, c.modelName)
207
if !errors.IsNotFound(err) {
208
return nil, errors.Trace(err)
210
// The model isn't known locally, so query the models
211
// available in the controller, and cache them locally.
212
if err := c.RefreshModels(c.store, c.controllerName, c.accountName); err != nil {
213
return nil, errors.Annotate(err, "refreshing models")
208
_, err := c.store.ModelByName(c.controllerName, c.accountName, c.modelName)
210
if !errors.IsNotFound(err) {
211
return nil, errors.Trace(err)
213
// The model isn't known locally, so query the models
214
// available in the controller, and cache them locally.
215
if err := c.RefreshModels(c.store, c.controllerName, c.accountName); err != nil {
216
return nil, errors.Annotate(err, "refreshing models")
217
219
return opener.Open(c.store, c.controllerName, c.accountName, c.modelName)
220
// ConnectionCredentials returns the credentials used to connect to the API for
221
// the specified environment.
222
func (c *ModelCommandBase) ConnectionCredentials() (configstore.APICredentials, error) {
223
// TODO: the user may soon be specified through the command line
224
// or through an environment setting, so return these when they are ready.
225
var emptyCreds configstore.APICredentials
226
if c.modelName == "" {
227
return emptyCreds, errors.Trace(ErrNoModelSpecified)
229
info, err := connectionInfoForName(c.controllerName, c.modelName)
231
return emptyCreds, errors.Trace(err)
233
return info.APICredentials(), nil
236
var endpointRefresher = func(c *ModelCommandBase) (io.Closer, error) {
237
return c.NewAPIRoot()
240
var getConfigStore = func() (configstore.Storage, error) {
241
store, err := configstore.Default()
243
return nil, errors.Trace(err)
248
// connectionInfoForName reads the environment information for the named
249
// environment (modelName) and returns it.
250
func connectionInfoForName(controllerName, modelName string) (configstore.EnvironInfo, error) {
251
store, err := getConfigStore()
253
return nil, errors.Trace(err)
255
info, err := store.ReadInfo(configstore.EnvironInfoName(controllerName, modelName))
257
return nil, errors.Trace(err)
262
222
// ConnectionName returns the name of the connection if there is one.
263
223
// It is possible that the name of the connection is empty if the
264
224
// connection information is supplied through command line arguments