github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/api/charms/client.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package charms
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"gopkg.in/juju/charm.v6-unstable"
     9  
    10  	"github.com/juju/juju/api/base"
    11  	"github.com/juju/juju/apiserver/params"
    12  )
    13  
    14  // Client allows access to the charms API end point.
    15  type Client struct {
    16  	base.ClientFacade
    17  	facade base.FacadeCaller
    18  }
    19  
    20  // NewClient creates a new client for accessing the charms API.
    21  func NewClient(st base.APICallCloser) *Client {
    22  	frontend, backend := base.NewClientFacade(st, "Charms")
    23  	return &Client{ClientFacade: frontend, facade: backend}
    24  }
    25  
    26  // CharmInfo holds information about a charm.
    27  type CharmInfo struct {
    28  	Revision int
    29  	URL      string
    30  	Config   *charm.Config
    31  	Meta     *charm.Meta
    32  	Actions  *charm.Actions
    33  }
    34  
    35  // CharmInfo returns information about the requested charm.
    36  func (c *Client) CharmInfo(charmURL string) (*CharmInfo, error) {
    37  	args := params.CharmInfo{CharmURL: charmURL}
    38  	info := &CharmInfo{}
    39  	if err := c.facade.FacadeCall("CharmInfo", args, info); err != nil {
    40  		return nil, err
    41  	}
    42  	return info, nil
    43  }
    44  
    45  // List returns a list of charm URLs currently in the state.
    46  // If supplied parameter contains any names, the result will be filtered
    47  // to return only the charms with supplied names.
    48  func (c *Client) List(names []string) ([]string, error) {
    49  	charms := &params.CharmsListResult{}
    50  	args := params.CharmsList{Names: names}
    51  	if err := c.facade.FacadeCall("List", args, charms); err != nil {
    52  		return nil, errors.Trace(err)
    53  	}
    54  	return charms.CharmURLs, nil
    55  }
    56  
    57  // IsMetered returns whether or not the charm is metered.
    58  func (c *Client) IsMetered(charmURL string) (bool, error) {
    59  	args := params.CharmInfo{CharmURL: charmURL}
    60  	metered := &params.IsMeteredResult{}
    61  	if err := c.facade.FacadeCall("IsMetered", args, metered); err != nil {
    62  		return false, err
    63  	}
    64  	return metered.Metered, nil
    65  }