github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/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  func (c *Client) Show(tags []names.StorageTag) ([]params.StorageInstance, error) {
    32  	found := params.StorageShowResults{}
    33  	entities := make([]params.Entity, len(tags))
    34  	for i, tag := range tags {
    35  		entities[i] = params.Entity{Tag: tag.String()}
    36  	}
    37  	if err := c.facade.FacadeCall("Show", params.Entities{Entities: entities}, &found); err != nil {
    38  		return nil, errors.Trace(err)
    39  	}
    40  	all := []params.StorageInstance{}
    41  	allErr := params.ErrorResults{}
    42  	for _, result := range found.Results {
    43  		if result.Error.Error != nil {
    44  			allErr.Results = append(allErr.Results, result.Error)
    45  			continue
    46  		}
    47  		all = append(all, result.Result)
    48  	}
    49  	return all, allErr.Combine()
    50  }