github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/apiserver/common/block.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package common
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  
     9  	"github.com/juju/juju/environs/config"
    10  )
    11  
    12  // isOperationBlocked determines if the operation should proceed
    13  // based on configuration parameters that prevent destroy, remove or change
    14  // operations.
    15  func isOperationBlocked(operation Operation, cfg *config.Config) bool {
    16  	allChanges := cfg.PreventAllChanges()
    17  	// If all changes are blocked, requesting operation makes no difference
    18  	if allChanges {
    19  		return true
    20  	}
    21  
    22  	allRemoves := cfg.PreventRemoveObject()
    23  	// This only matters for Destroy and Remove operations
    24  	if allRemoves && operation != ChangeOperation {
    25  		return true
    26  	}
    27  
    28  	allDestroys := cfg.PreventDestroyEnvironment()
    29  	if allDestroys && operation == DestroyOperation {
    30  		return true
    31  	}
    32  	return false
    33  }
    34  
    35  // Operation specifies operation type for enum benefit.
    36  // Operation type may be relevant for a group of commands.
    37  type Operation int8
    38  
    39  const (
    40  	// DestroyOperation type groups commands that destroy environment.
    41  	DestroyOperation Operation = iota
    42  
    43  	// RemoveOperation type groups commands
    44  	// that removes machine, service, unit or relation.
    45  	RemoveOperation
    46  
    47  	// ChangeOperation type groups commands that change environments -
    48  	// all adds, modifies, removes, etc.
    49  	ChangeOperation
    50  )
    51  
    52  // BlockChecker checks for current blocks if any.
    53  type BlockChecker struct {
    54  	getter EnvironConfigGetter
    55  }
    56  
    57  func NewBlockChecker(s EnvironConfigGetter) *BlockChecker {
    58  	return &BlockChecker{s}
    59  }
    60  
    61  // ChangeAllowed checks if change block is in place.
    62  // Change block prevents all operations that may change
    63  // current environment in any way from running successfully.
    64  func (c *BlockChecker) ChangeAllowed() error {
    65  	return c.checkBlock(ChangeOperation)
    66  }
    67  
    68  // RemoveAllowed checks if remove block is in place.
    69  // Remove block prevents removal of machine, service, unit
    70  // and relation from current environment.
    71  func (c *BlockChecker) RemoveAllowed() error {
    72  	return c.checkBlock(RemoveOperation)
    73  }
    74  
    75  // DestroyAllowed checks if destroy block is in place.
    76  // Destroy block prevents destruction of current environment.
    77  func (c *BlockChecker) DestroyAllowed() error {
    78  	return c.checkBlock(DestroyOperation)
    79  }
    80  
    81  // checkBlock checks if specified operation must be blocked.
    82  // If it does, the method throws specific error that can be examined
    83  // to stop operation execution.
    84  func (c *BlockChecker) checkBlock(operation Operation) error {
    85  	cfg, err := c.getter.EnvironConfig()
    86  	if err != nil {
    87  		return errors.Trace(err)
    88  	}
    89  	if isOperationBlocked(operation, cfg) {
    90  		return ErrOperationBlocked
    91  	}
    92  	return nil
    93  }