github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/api/storage/client.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package storage
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/loggo"
     9  	"github.com/juju/names"
    10  
    11  	"github.com/juju/juju/api/base"
    12  	"github.com/juju/juju/apiserver/params"
    13  )
    14  
    15  var logger = loggo.GetLogger("juju.api.storage")
    16  
    17  // Client allows access to the storage API end point.
    18  type Client struct {
    19  	base.ClientFacade
    20  	facade base.FacadeCaller
    21  }
    22  
    23  // NewClient creates a new client for accessing the storage API.
    24  func NewClient(st base.APICallCloser) *Client {
    25  	frontend, backend := base.NewClientFacade(st, "Storage")
    26  	logger.Debugf("\nSTORAGE FRONT-END: %#v", frontend)
    27  	logger.Debugf("\nSTORAGE BACK-END: %#v", backend)
    28  	return &Client{ClientFacade: frontend, facade: backend}
    29  }
    30  
    31  // Show retrieves information about desired storage instances.
    32  func (c *Client) Show(tags []names.StorageTag) ([]params.StorageDetails, error) {
    33  	found := params.StorageDetailsResults{}
    34  	entities := make([]params.Entity, len(tags))
    35  	for i, tag := range tags {
    36  		entities[i] = params.Entity{Tag: tag.String()}
    37  	}
    38  	if err := c.facade.FacadeCall("Show", params.Entities{Entities: entities}, &found); err != nil {
    39  		return nil, errors.Trace(err)
    40  	}
    41  	return c.convert(found.Results)
    42  }
    43  
    44  func (c *Client) convert(found []params.StorageDetailsResult) ([]params.StorageDetails, error) {
    45  	var storages []params.StorageDetails
    46  	var allErr params.ErrorResults
    47  	for _, result := range found {
    48  		if result.Error != nil {
    49  			allErr.Results = append(allErr.Results, params.ErrorResult{result.Error})
    50  			continue
    51  		}
    52  		storages = append(storages, result.Result)
    53  	}
    54  	return storages, allErr.Combine()
    55  }
    56  
    57  // List lists all storage.
    58  func (c *Client) List() ([]params.StorageInfo, error) {
    59  	found := params.StorageInfosResult{}
    60  	if err := c.facade.FacadeCall("List", nil, &found); err != nil {
    61  		return nil, errors.Trace(err)
    62  	}
    63  	return found.Results, nil
    64  }
    65  
    66  // ListPools returns a list of pools that matches given filter.
    67  // If no filter was provided, a list of all pools is returned.
    68  func (c *Client) ListPools(providers, names []string) ([]params.StoragePool, error) {
    69  	args := params.StoragePoolFilter{
    70  		Names:     names,
    71  		Providers: providers,
    72  	}
    73  	found := params.StoragePoolsResult{}
    74  	if err := c.facade.FacadeCall("ListPools", args, &found); err != nil {
    75  		return nil, errors.Trace(err)
    76  	}
    77  	return found.Results, nil
    78  }
    79  
    80  // CreatePool creates pool with specified parameters.
    81  func (c *Client) CreatePool(pname, provider string, attrs map[string]interface{}) error {
    82  	args := params.StoragePool{
    83  		Name:     pname,
    84  		Provider: provider,
    85  		Attrs:    attrs,
    86  	}
    87  	return c.facade.FacadeCall("CreatePool", args, nil)
    88  }
    89  
    90  // ListVolumes lists volumes for desired machines.
    91  // If no machines provided, a list of all volumes is returned.
    92  func (c *Client) ListVolumes(machines []string) ([]params.VolumeItem, error) {
    93  	tags := make([]string, len(machines))
    94  	for i, one := range machines {
    95  		tags[i] = names.NewMachineTag(one).String()
    96  	}
    97  	args := params.VolumeFilter{Machines: tags}
    98  	found := params.VolumeItemsResult{}
    99  	if err := c.facade.FacadeCall("ListVolumes", args, &found); err != nil {
   100  		return nil, errors.Trace(err)
   101  	}
   102  	return found.Results, nil
   103  }
   104  
   105  // AddToUnit adds specified storage to desired units.
   106  func (c *Client) AddToUnit(storages []params.StorageAddParams) ([]params.ErrorResult, error) {
   107  	out := params.ErrorResults{}
   108  	in := params.StoragesAddParams{Storages: storages}
   109  	err := c.facade.FacadeCall("AddToUnit", in, &out)
   110  	if err != nil {
   111  		return nil, errors.Trace(err)
   112  	}
   113  	return out.Results, nil
   114  }