github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/api/block/client.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/base" 11 "github.com/juju/juju/apiserver/params" 12 ) 13 14 var logger = loggo.GetLogger("juju.api.block") 15 16 // Client allows access to the block API end point. 17 type Client struct { 18 base.ClientFacade 19 facade base.FacadeCaller 20 } 21 22 // NewClient creates a new client for accessing the block API. 23 func NewClient(st base.APICallCloser) *Client { 24 frontend, backend := base.NewClientFacade(st, "Block") 25 logger.Debugf("\nSTORAGE FRONT-END: %#v", frontend) 26 logger.Debugf("\nSTORAGE BACK-END: %#v", backend) 27 return &Client{ClientFacade: frontend, facade: backend} 28 } 29 30 // List returns blocks that are switched on for current environment. 31 func (c *Client) List() ([]params.Block, error) { 32 blocks := params.BlockResults{} 33 if err := c.facade.FacadeCall("List", nil, &blocks); err != nil { 34 return nil, errors.Trace(err) 35 } 36 37 all := []params.Block{} 38 allErr := params.ErrorResults{} 39 for _, result := range blocks.Results { 40 if result.Error != nil { 41 allErr.Results = append(allErr.Results, params.ErrorResult{result.Error}) 42 continue 43 } 44 all = append(all, result.Result) 45 } 46 return all, allErr.Combine() 47 } 48 49 // SwitchBlockOn switches desired block on for the current environment. 50 // Valid block types are "BlockDestroy", "BlockRemove" and "BlockChange". 51 func (c *Client) SwitchBlockOn(blockType, msg string) error { 52 args := params.BlockSwitchParams{ 53 Type: blockType, 54 Message: msg, 55 } 56 result := params.ErrorResult{} 57 if err := c.facade.FacadeCall("SwitchBlockOn", args, &result); err != nil { 58 return errors.Trace(err) 59 } 60 if result.Error != nil { 61 return result.Error 62 } 63 return nil 64 } 65 66 // SwitchBlockOff switches desired block off for the current environment. 67 // Valid block types are "BlockDestroy", "BlockRemove" and "BlockChange". 68 func (c *Client) SwitchBlockOff(blockType string) error { 69 args := params.BlockSwitchParams{ 70 Type: blockType, 71 } 72 result := params.ErrorResult{} 73 if err := c.facade.FacadeCall("SwitchBlockOff", args, &result); err != nil { 74 return errors.Trace(err) 75 } 76 if result.Error != nil { 77 return result.Error 78 } 79 return nil 80 }