Permalink
Browse files

fix up error handling when no current controller

  • Loading branch information...
1 parent bad2931 commit a5f95c441ac29ed1a98c8efafbb71869ed89e7b6 @natefinch natefinch committed Sep 22, 2016
@@ -82,6 +82,10 @@ func (c *listControllersCommand) Run(ctx *cmd.Context) error {
if err != nil {
return errors.Annotate(err, "failed to list controllers")
}
+ if len(controllers) == 0 && c.out.Name() == "tabular" {
+ ctx.Infof("%s", modelcmd.ErrNoControllersDefined)
+ return nil
+ }
if c.refresh && len(controllers) > 0 {
var wg sync.WaitGroup
wg.Add(len(controllers))
@@ -14,6 +14,7 @@ import (
"github.com/juju/juju/api/base"
"github.com/juju/juju/cmd/juju/controller"
+ "github.com/juju/juju/cmd/modelcmd"
"github.com/juju/juju/jujuclient/jujuclienttesting"
"github.com/juju/juju/testing"
)
@@ -26,13 +27,11 @@ type ListControllersSuite struct {
var _ = gc.Suite(&ListControllersSuite{})
func (s *ListControllersSuite) TestListControllersEmptyStore(c *gc.C) {
- s.expectedOutput = `
-CONTROLLER MODEL USER ACCESS CLOUD/REGION MODELS MACHINES HA VERSION
-
-`[1:]
-
s.store = jujuclienttesting.NewMemStore()
- s.assertListControllers(c)
+ context, err := s.runListControllers(c)
+ c.Assert(err, jc.ErrorIsNil)
+ c.Check(testing.Stdout(context), gc.Equals, "")
+ c.Check(testing.Stderr(context), gc.Equals, modelcmd.ErrNoControllersDefined.Error())
}
func (s *ListControllersSuite) TestListControllers(c *gc.C) {
View
@@ -19,19 +19,18 @@ 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 controller
+ ErrNoControllersDefined = errors.New(`No controllers registered.
-Please either create your own new controller using "juju bootstrap" or
-connect to another controller that you have been given access to using "juju register".
+Please either create a new controller using "juju bootstrap" or connect to
+another controller that you have been given access to using "juju register".
`)
- // ErrNotLoggedInToController is returned by commands that operate on
+ // 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, i.e. the user needs to log in to one of them.
- ErrNotLoggedInToController = errors.New(`not logged in
+ // controllers that client knows about.
+ ErrNoCurrentController = errors.New(`No selected controller.
-Please use "juju controllers" to view all controllers available to you.
-You can login into an existing controller using "juju login -c <controller>".
+Please use "juju switch" to select a controller.
`)
)
@@ -169,7 +168,7 @@ func (c *ControllerCommandBase) newAPIRoot(modelName string) (api.Connection, er
if len(controllers) == 0 {
return nil, errors.Trace(ErrNoControllersDefined)
}
- return nil, errors.Trace(ErrNotLoggedInToController)
+ return nil, errors.Trace(ErrNoCurrentController)
}
opener := c.opener
if opener == nil {
@@ -270,14 +269,17 @@ func (w *sysCommandWrapper) Init(args []string) error {
}
store = QualifyingClientStore{store}
w.SetClientStore(store)
+
+ return w.ControllerCommand.Init(args)
+}
+
+func (w *sysCommandWrapper) Run(ctx *cmd.Context) error {
if w.setControllerFlags {
if w.controllerName == "" && w.useDefaultController {
+ store := w.ClientStore()
currentController, err := store.CurrentController()
- if errors.IsNotFound(err) {
- return ErrNoControllersDefined
- }
if err != nil {
- return errors.Trace(err)
+ return translateControllerError(store, err)
}
w.controllerName = currentController
}
@@ -287,8 +289,22 @@ func (w *sysCommandWrapper) Init(args []string) error {
}
if w.controllerName != "" {
if err := w.SetControllerName(w.controllerName); err != nil {
- return errors.Trace(err)
+ return translateControllerError(w.ClientStore(), err)
}
}
- return w.ControllerCommand.Init(args)
+ return w.ControllerCommand.Run(ctx)
+}
+
+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)
}
@@ -6,6 +6,7 @@ package modelcmd_test
import (
"github.com/juju/cmd"
"github.com/juju/cmd/cmdtesting"
+ "github.com/juju/errors"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
@@ -22,8 +23,10 @@ type ControllerCommandSuite struct {
var _ = gc.Suite(&ControllerCommandSuite{})
func (s *ControllerCommandSuite) TestControllerCommandNoneSpecified(c *gc.C) {
- _, err := initTestControllerCommand(c, nil)
- c.Assert(err, gc.ErrorMatches, "no controller(.|\n)*")
+ cmd, _, err := initTestControllerCommand(c, nil)
+ c.Assert(err, jc.ErrorIsNil)
+ err = cmd.Run(nil)
+ c.Assert(errors.Cause(err), gc.Equals, modelcmd.ErrNoControllersDefined)
}
func (s *ControllerCommandSuite) TestControllerCommandInitCurrentController(c *gc.C) {
@@ -65,18 +68,23 @@ func (c *testControllerCommand) Info() *cmd.Info {
}
func (c *testControllerCommand) Run(ctx *cmd.Context) error {
- panic("should not be called")
+ return nil
}
-func initTestControllerCommand(c *gc.C, store jujuclient.ClientStore, args ...string) (*testControllerCommand, error) {
+func initTestControllerCommand(c *gc.C, store jujuclient.ClientStore, args ...string) (cmd.Command, *testControllerCommand, error) {
cmd := new(testControllerCommand)
cmd.SetClientStore(store)
wrapped := modelcmd.WrapController(cmd)
- return cmd, cmdtesting.InitCommand(wrapped, args)
+ if err := cmdtesting.InitCommand(wrapped, args); err != nil {
+ return nil, nil, err
+ }
+ return wrapped, cmd, nil
}
func testEnsureControllerName(c *gc.C, store jujuclient.ClientStore, expect string, args ...string) {
- cmd, err := initTestControllerCommand(c, store, args...)
+ cmd, controllerCmd, err := initTestControllerCommand(c, store, args...)
+ c.Assert(err, jc.ErrorIsNil)
+ err = cmd.Run(nil)
c.Assert(err, jc.ErrorIsNil)
- c.Assert(cmd.ControllerName(), gc.Equals, expect)
+ c.Assert(controllerCmd.ControllerName(), gc.Equals, expect)
}
@@ -24,7 +24,7 @@ var logger = loggo.GetLogger("juju.cmd.modelcmd")
// ErrNoModelSpecified is returned by commands that operate on
// an environment if there is no current model, no model
// has been explicitly specified, and there is no default model.
-var ErrNoModelSpecified = errors.New(`no model in focus
+var ErrNoModelSpecified = errors.New(`No model in focus.
Please use "juju models" to see models available to you.
You can set current model by running "juju switch"
@@ -215,7 +215,7 @@ func (c *ModelCommandBase) newAPIRoot(modelName string) (api.Connection, error)
if len(controllers) == 0 {
return nil, errors.Trace(ErrNoControllersDefined)
}
- return nil, errors.Trace(ErrNotLoggedInToController)
+ return nil, errors.Trace(ErrNoCurrentController)
}
opener := c.opener
if opener == nil {
@@ -312,7 +312,7 @@ func (w *modelCommandWrapper) Init(args []string) error {
}
if w.modelName != "" {
if err := w.SetModelName(w.modelName); err != nil {
- return errors.Annotate(err, "setting model name")
+ return translateControllerError(store, err)
}
}
return w.ModelCommand.Init(args)

0 comments on commit a5f95c4

Please sign in to comment.