github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/block/enablecommand.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package block
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/errors"
     9  
    10  	"github.com/juju/juju/cmd/modelcmd"
    11  )
    12  
    13  // NewEnableCommand returns a new command that eanbles previously disabled
    14  // command sets.
    15  func NewEnableCommand() cmd.Command {
    16  	return modelcmd.Wrap(&enableCommand{
    17  		apiFunc: func(c newAPIRoot) (unblockClientAPI, error) {
    18  			return getBlockAPI(c)
    19  		},
    20  	})
    21  }
    22  
    23  // enableCommand removes the block from desired operation.
    24  type enableCommand struct {
    25  	modelcmd.ModelCommandBase
    26  	apiFunc func(newAPIRoot) (unblockClientAPI, error)
    27  	target  string
    28  }
    29  
    30  // Init implements Command.
    31  func (c *enableCommand) Init(args []string) error {
    32  	if len(args) < 1 {
    33  		return errors.Errorf("missing command set (%s)", validTargets)
    34  	}
    35  	c.target, args = args[0], args[1:]
    36  	target, ok := toAPIValue[c.target]
    37  	if !ok {
    38  		return errors.Errorf("bad command set, valid options: %s", validTargets)
    39  	}
    40  	c.target = target
    41  	return cmd.CheckEmpty(args)
    42  }
    43  
    44  // Info implementsCommand.
    45  func (c *enableCommand) Info() *cmd.Info {
    46  	return &cmd.Info{
    47  		Name:    "enable-command",
    48  		Args:    "<command set>",
    49  		Purpose: "Enable commands that had been previously disabled.",
    50  		Doc:     enableDoc,
    51  	}
    52  }
    53  
    54  // unblockClientAPI defines the client API methods that unblock command uses.
    55  type unblockClientAPI interface {
    56  	Close() error
    57  	SwitchBlockOff(blockType string) error
    58  }
    59  
    60  // Run implements Command.
    61  func (c *enableCommand) Run(_ *cmd.Context) error {
    62  	api, err := c.apiFunc(c)
    63  	if err != nil {
    64  		return errors.Annotate(err, "cannot connect to the API")
    65  	}
    66  	defer api.Close()
    67  
    68  	return api.SwitchBlockOff(c.target)
    69  }
    70  
    71  const enableDoc = `
    72  Juju allows to safeguard deployed models from unintentional damage by preventing
    73  execution of operations that could alter model.
    74  
    75  This is done by disabling certain sets of commands from successful execution.
    76  Disabled commands must be manually enabled to proceed.
    77  
    78  Some commands offer a --force option that can be used to bypass a block.
    79  ` + commandSets + `
    80  Examples:
    81      # To allow the model to be destroyed:
    82      juju enable-command destroy-model
    83  
    84      # To allow the machines, applications, units and relations to be removed:
    85      juju enable-command remove-object
    86  
    87      # To allow changes to the model:
    88      juju enable-command all
    89  
    90  See also:
    91      disable-command
    92      disabled-commands
    93  `