github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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/apiserver/params"
    12  	"github.com/juju/juju/payload"
    13  	"github.com/juju/juju/payload/api"
    14  )
    15  
    16  type facadeCaller interface {
    17  	FacadeCall(request string, params, response interface{}) error
    18  }
    19  
    20  type rawAPI interface {
    21  	facadeCaller
    22  	io.Closer
    23  }
    24  
    25  // PublicClient provides methods for interacting with Juju's public
    26  // RPC API, relative to payloads.
    27  type PublicClient struct {
    28  	rawAPI
    29  }
    30  
    31  // NewPublicClient builds a new payload API client.
    32  func NewPublicClient(raw rawAPI) PublicClient {
    33  	return PublicClient{
    34  		rawAPI: raw,
    35  	}
    36  }
    37  
    38  // ListFull calls the List API server method.
    39  func (c PublicClient) ListFull(patterns ...string) ([]payload.FullPayloadInfo, error) {
    40  	var result params.PayloadListResults
    41  
    42  	args := params.PayloadListArgs{
    43  		Patterns: patterns,
    44  	}
    45  	if err := c.FacadeCall("List", &args, &result); err != nil {
    46  		return nil, errors.Trace(err)
    47  	}
    48  
    49  	payloads := make([]payload.FullPayloadInfo, len(result.Results))
    50  	for i, apiInfo := range result.Results {
    51  		payload, err := api.API2Payload(apiInfo)
    52  		if err != nil {
    53  			// We should never see this happen; we control the input safely.
    54  			return nil, errors.Trace(err)
    55  		}
    56  		payloads[i] = payload
    57  	}
    58  	return payloads, nil
    59  }