github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/cmd/juju/controller/unregister.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package controller
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    11  	"github.com/juju/gnuflag"
    12  
    13  	jujucmd "github.com/juju/juju/cmd"
    14  	"github.com/juju/juju/cmd/modelcmd"
    15  	"github.com/juju/juju/jujuclient"
    16  )
    17  
    18  // NewUnregisterCommand returns a command to allow the user to unregister a controller.
    19  func NewUnregisterCommand(store jujuclient.ClientStore) cmd.Command {
    20  	if store == nil {
    21  		panic("valid store must be specified")
    22  	}
    23  	cmd := &unregisterCommand{store: store}
    24  	return modelcmd.WrapBase(cmd)
    25  }
    26  
    27  // unregisterCommand removes a Juju controller from the local store.
    28  type unregisterCommand struct {
    29  	modelcmd.JujuCommandBase
    30  	controllerName string
    31  	assumeYes      bool
    32  	store          jujuclient.ClientStore
    33  }
    34  
    35  var usageUnregisterDetails = `
    36  Removes local connection information for the specified controller.  This
    37  command does not destroy the controller.  In order to regain access to an
    38  unregistered controller, it will need to be added again using the juju register
    39  command.
    40  
    41  Examples:
    42  
    43      juju unregister my-controller
    44  
    45  See also:
    46      destroy-controller
    47      kill-controller
    48      register`
    49  
    50  // Info implements Command.Info
    51  // `unregister` may seem generic as a command, but aligns with `register`.
    52  func (c *unregisterCommand) Info() *cmd.Info {
    53  	return &cmd.Info{
    54  		Name:    "unregister",
    55  		Args:    "<controller name>",
    56  		Purpose: "Unregisters a Juju controller",
    57  		Doc:     usageUnregisterDetails,
    58  	}
    59  }
    60  
    61  // SetFlags implements Command.SetFlags.
    62  func (c *unregisterCommand) SetFlags(f *gnuflag.FlagSet) {
    63  	f.BoolVar(&c.assumeYes, "y", false, "Do not prompt for confirmation")
    64  	f.BoolVar(&c.assumeYes, "yes", false, "")
    65  }
    66  
    67  // Init implements Command.Init.
    68  func (c *unregisterCommand) Init(args []string) error {
    69  	if len(args) < 1 {
    70  		return errors.New("controller name must be specified")
    71  	}
    72  	c.controllerName, args = args[0], args[1:]
    73  
    74  	if err := jujuclient.ValidateControllerName(c.controllerName); err != nil {
    75  		return err
    76  	}
    77  
    78  	if err := cmd.CheckEmpty(args); err != nil {
    79  		return err
    80  	}
    81  	return nil
    82  }
    83  
    84  var unregisterMsg = `
    85  This command will remove connection information for controller %q.
    86  Doing so will prevent you from accessing this controller until
    87  you register it again.
    88  
    89  Continue [y/N]?`[1:]
    90  
    91  func (c *unregisterCommand) Run(ctx *cmd.Context) error {
    92  
    93  	_, err := c.store.ControllerByName(c.controllerName)
    94  	if err != nil {
    95  		return errors.Trace(err)
    96  	}
    97  
    98  	if !c.assumeYes {
    99  		fmt.Fprintf(ctx.Stdout, unregisterMsg, c.controllerName)
   100  
   101  		if err := jujucmd.UserConfirmYes(ctx); err != nil {
   102  			return errors.Annotate(err, "unregistering controller")
   103  		}
   104  	}
   105  
   106  	return (c.store.RemoveController(c.controllerName))
   107  }