github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/payload/api/client/public.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package client
     5  
     6  import (
     7  	"io"
     8  
     9  	"github.com/juju/errors"
    10  
    11  	"github.com/juju/juju/payload"
    12  	"github.com/juju/juju/payload/api"
    13  )
    14  
    15  type facadeCaller interface {
    16  	FacadeCall(request string, params, response interface{}) error
    17  }
    18  
    19  type rawAPI interface {
    20  	facadeCaller
    21  	io.Closer
    22  }
    23  
    24  // PublicClient provides methods for interacting with Juju's public
    25  // RPC API, relative to payloads.
    26  type PublicClient struct {
    27  	rawAPI
    28  }
    29  
    30  // NewPublicClient builds a new payload API client.
    31  func NewPublicClient(raw rawAPI) PublicClient {
    32  	return PublicClient{
    33  		rawAPI: raw,
    34  	}
    35  }
    36  
    37  // ListFull calls the List API server method.
    38  func (c PublicClient) ListFull(patterns ...string) ([]payload.FullPayloadInfo, error) {
    39  	var result api.EnvListResults
    40  
    41  	args := api.EnvListArgs{
    42  		Patterns: patterns,
    43  	}
    44  	if err := c.FacadeCall("List", &args, &result); err != nil {
    45  		return nil, errors.Trace(err)
    46  	}
    47  
    48  	payloads := make([]payload.FullPayloadInfo, len(result.Results))
    49  	for i, apiInfo := range result.Results {
    50  		payload, err := api.API2Payload(apiInfo)
    51  		if err != nil {
    52  			// We should never see this happen; we control the input safely.
    53  			return nil, errors.Trace(err)
    54  		}
    55  		payloads[i] = payload
    56  	}
    57  	return payloads, nil
    58  }