github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/common/block.go (about) 1 // Copyright 2014 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/state" 10 ) 11 12 type BlockGetter interface { 13 GetBlockForType(t state.BlockType) (state.Block, bool, error) 14 } 15 16 // BlockChecker checks for current blocks if any. 17 type BlockChecker struct { 18 getter BlockGetter 19 } 20 21 func NewBlockChecker(s BlockGetter) *BlockChecker { 22 return &BlockChecker{s} 23 } 24 25 // ChangeAllowed checks if change block is in place. 26 // Change block prevents all operations that may change 27 // current model in any way from running successfully. 28 func (c *BlockChecker) ChangeAllowed() error { 29 return c.checkBlock(state.ChangeBlock) 30 } 31 32 // RemoveAllowed checks if remove block is in place. 33 // Remove block prevents removal of machine, service, unit 34 // and relation from current model. 35 func (c *BlockChecker) RemoveAllowed() error { 36 if err := c.checkBlock(state.RemoveBlock); err != nil { 37 return err 38 } 39 // Check if change block has been enabled 40 return c.checkBlock(state.ChangeBlock) 41 } 42 43 // DestroyAllowed checks if destroy block is in place. 44 // Destroy block prevents destruction of current model. 45 func (c *BlockChecker) DestroyAllowed() error { 46 if err := c.checkBlock(state.DestroyBlock); err != nil { 47 return err 48 } 49 // Check if remove block has been enabled 50 if err := c.checkBlock(state.RemoveBlock); err != nil { 51 return err 52 } 53 // Check if change block has been enabled 54 return c.checkBlock(state.ChangeBlock) 55 } 56 57 // checkBlock checks if specified operation must be blocked. 58 // If it does, the method throws specific error that can be examined 59 // to stop operation execution. 60 func (c *BlockChecker) checkBlock(blockType state.BlockType) error { 61 aBlock, isEnabled, err := c.getter.GetBlockForType(blockType) 62 if err != nil { 63 return errors.Trace(err) 64 } 65 if isEnabled { 66 return OperationBlockedError(aBlock.Message()) 67 } 68 return nil 69 }