github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/block/disablecommand.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  	"strings"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    11  
    12  	jujucmd "github.com/juju/juju/cmd"
    13  	"github.com/juju/juju/cmd/modelcmd"
    14  )
    15  
    16  // NewDisableCommand returns a disable-command command instance
    17  // that will use the default API.
    18  func NewDisableCommand() cmd.Command {
    19  	return modelcmd.Wrap(&disableCommand{
    20  		apiFunc: func(c newAPIRoot) (blockClientAPI, error) {
    21  			return getBlockAPI(c)
    22  		},
    23  	})
    24  }
    25  
    26  type disableCommand struct {
    27  	modelcmd.ModelCommandBase
    28  	apiFunc func(newAPIRoot) (blockClientAPI, error)
    29  	target  string
    30  	message string
    31  }
    32  
    33  // Init implements Command.
    34  func (c *disableCommand) Init(args []string) error {
    35  	if len(args) < 1 {
    36  		return errors.Errorf("missing command set (%s)", validTargets)
    37  	}
    38  	c.target, args = args[0], args[1:]
    39  	target, ok := toAPIValue[c.target]
    40  	if !ok {
    41  		return errors.Errorf("bad command set, valid options: %s", validTargets)
    42  	}
    43  	c.target = target
    44  	c.message = strings.Join(args, " ")
    45  	return nil
    46  }
    47  
    48  // Info implements Command.
    49  func (c *disableCommand) Info() *cmd.Info {
    50  	return jujucmd.Info(&cmd.Info{
    51  		Name:    "disable-command",
    52  		Args:    "<command set> [message...]",
    53  		Purpose: "Disable commands for the model.",
    54  		Doc:     disableCommandDoc,
    55  	})
    56  }
    57  
    58  type blockClientAPI interface {
    59  	Close() error
    60  	SwitchBlockOn(blockType, msg string) error
    61  }
    62  
    63  // Run implements Command.Run
    64  func (c *disableCommand) Run(ctx *cmd.Context) error {
    65  	api, err := c.apiFunc(c)
    66  	if err != nil {
    67  		return errors.Annotate(err, "cannot connect to the API")
    68  	}
    69  	defer api.Close()
    70  
    71  	return api.SwitchBlockOn(c.target, c.message)
    72  }
    73  
    74  var disableCommandDoc = `
    75  Juju allows to safeguard deployed models from unintentional damage by preventing
    76  execution of operations that could alter model.
    77  
    78  This is done by disabling certain sets of commands from successful execution.
    79  Disabled commands must be manually enabled to proceed.
    80  
    81  Some commands offer a --force option that can be used to bypass the disabling.
    82  ` + commandSets + `
    83  Examples:
    84      # To prevent the model from being destroyed:
    85      juju disable-command destroy-model "Check with SA before destruction."
    86  
    87      # To prevent the machines, applications, units and relations from being removed:
    88      juju disable-command remove-object
    89  
    90      # To prevent changes to the model:
    91      juju disable-command all "Model locked down"
    92  
    93  See also:
    94      disabled-commands
    95      enable-command
    96  `