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

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package block
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/loggo"
     9  
    10  	"github.com/juju/juju/api"
    11  	apiblock "github.com/juju/juju/api/block"
    12  	"github.com/juju/juju/apiserver/params"
    13  )
    14  
    15  var logger = loggo.GetLogger("juju.cmd.juju.block")
    16  
    17  const (
    18  	cmdAll          = "all"
    19  	cmdDestroyModel = "destroy-model"
    20  	cmdRemoveObject = "remove-object"
    21  
    22  	apiAll          = "BlockChange"
    23  	apiDestroyModel = "BlockDestroy"
    24  	apiRemoveObject = "BlockRemove"
    25  )
    26  
    27  var (
    28  	toAPIValue = map[string]string{
    29  		cmdAll:          apiAll,
    30  		cmdDestroyModel: apiDestroyModel,
    31  		cmdRemoveObject: apiRemoveObject,
    32  	}
    33  
    34  	toCmdValue = map[string]string{
    35  		apiAll:          cmdAll,
    36  		apiDestroyModel: cmdDestroyModel,
    37  		apiRemoveObject: cmdRemoveObject,
    38  	}
    39  
    40  	validTargets = cmdAll + ", " + cmdDestroyModel + ", " + cmdRemoveObject
    41  )
    42  
    43  func operationFromType(blockType string) string {
    44  	value, ok := toCmdValue[blockType]
    45  	if !ok {
    46  		value = "<unknown>"
    47  	}
    48  	return value
    49  }
    50  
    51  type newAPIRoot interface {
    52  	NewAPIRoot() (api.Connection, error)
    53  }
    54  
    55  // getBlockAPI returns a block api for block manipulation.
    56  func getBlockAPI(c newAPIRoot) (*apiblock.Client, error) {
    57  	root, err := c.NewAPIRoot()
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	return apiblock.NewClient(root), nil
    62  }
    63  
    64  // Block describes block type
    65  type Block int8
    66  
    67  const (
    68  	// BlockDestroy describes the block that
    69  	// blocks destroy- commands
    70  	BlockDestroy Block = iota
    71  
    72  	// BlockRemove describes the block that
    73  	// blocks remove- commands
    74  	BlockRemove
    75  
    76  	// BlockChange describes the block that
    77  	// blocks change commands
    78  	BlockChange
    79  )
    80  
    81  var blockedMessages = map[Block]string{
    82  	BlockDestroy: destroyMsg,
    83  	BlockRemove:  removeMsg,
    84  	BlockChange:  changeMsg,
    85  }
    86  
    87  // ProcessBlockedError ensures that correct and user-friendly message is
    88  // displayed to the user based on the block type.
    89  func ProcessBlockedError(err error, block Block) error {
    90  	if err == nil {
    91  		return nil
    92  	}
    93  	if params.IsCodeOperationBlocked(err) {
    94  		msg := blockedMessages[block]
    95  		logger.Errorf("%v\n%v", err, msg)
    96  		return errors.New(msg)
    97  	}
    98  	return err
    99  }
   100  
   101  var removeMsg = `
   102  All operations that remove machines, applications, units or
   103  relations have been disabled for the current model.
   104  To enable removal, run
   105  
   106      juju enable-command remove-object
   107  
   108  `
   109  var destroyMsg = `
   110  destroy-model operation has been disabled for the current model.
   111  To enable the command run
   112  
   113      juju enable-command destroy-model
   114  
   115  `
   116  var changeMsg = `
   117  All operations that change model have been disabled for the current model.
   118  To enable changes, run
   119  
   120      juju enable-command all
   121  
   122  `