~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/modelcmd/modelcommand.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
import (
7
7
        "fmt"
8
 
        "io"
9
8
        "os"
10
9
        "strings"
11
10
 
12
11
        "github.com/juju/cmd"
13
12
        "github.com/juju/errors"
14
13
        "github.com/juju/loggo"
15
 
 
16
14
        "launchpad.net/gnuflag"
17
15
 
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"
23
20
)
24
21
 
25
 
var logger = loggo.GetLogger("juju.cmd.envcmd")
 
22
var logger = loggo.GetLogger("juju.cmd.modelcmd")
26
23
 
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
 
28
 
 
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
using the "-m" flag.
 
33
`)
31
34
 
32
35
// GetCurrentModel returns the name of the current Juju model.
33
36
//
192
195
        // This is work in progress as we remove the ModelName from downstream code.
193
196
        // We want to be able to specify the environment in a number of ways, one of
194
197
        // which is the connection name on the client machine.
 
198
        if c.controllerName == "" {
 
199
                return nil, errors.Trace(ErrNoControllerSpecified)
 
200
        }
195
201
        if c.modelName == "" {
196
202
                return nil, errors.Trace(ErrNoModelSpecified)
197
203
        }
199
205
        if opener == nil {
200
206
                opener = OpenFunc(c.JujuCommandBase.NewAPIRoot)
201
207
        }
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)
206
 
                if err != nil {
207
 
                        if !errors.IsNotFound(err) {
208
 
                                return nil, errors.Trace(err)
209
 
                        }
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")
214
 
                        }
 
208
        _, err := c.store.ModelByName(c.controllerName, c.accountName, c.modelName)
 
209
        if err != nil {
 
210
                if !errors.IsNotFound(err) {
 
211
                        return nil, errors.Trace(err)
 
212
                }
 
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")
215
217
                }
216
218
        }
217
219
        return opener.Open(c.store, c.controllerName, c.accountName, c.modelName)
218
220
}
219
221
 
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)
228
 
        }
229
 
        info, err := connectionInfoForName(c.controllerName, c.modelName)
230
 
        if err != nil {
231
 
                return emptyCreds, errors.Trace(err)
232
 
        }
233
 
        return info.APICredentials(), nil
234
 
}
235
 
 
236
 
var endpointRefresher = func(c *ModelCommandBase) (io.Closer, error) {
237
 
        return c.NewAPIRoot()
238
 
}
239
 
 
240
 
var getConfigStore = func() (configstore.Storage, error) {
241
 
        store, err := configstore.Default()
242
 
        if err != nil {
243
 
                return nil, errors.Trace(err)
244
 
        }
245
 
        return store, nil
246
 
}
247
 
 
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()
252
 
        if err != nil {
253
 
                return nil, errors.Trace(err)
254
 
        }
255
 
        info, err := store.ReadInfo(configstore.EnvironInfoName(controllerName, modelName))
256
 
        if err != nil {
257
 
                return nil, errors.Trace(err)
258
 
        }
259
 
        return info, nil
260
 
}
261
 
 
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
309
269
        modelName       string
310
270
}
311
271
 
 
272
func (w *modelCommandWrapper) Run(ctx *cmd.Context) error {
 
273
        return w.ModelCommand.Run(ctx)
 
274
}
 
275
 
312
276
func (w *modelCommandWrapper) SetFlags(f *gnuflag.FlagSet) {
313
277
        if !w.skipFlags {
314
278
                f.StringVar(&w.modelName, "m", "", "juju model to operate in")