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