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