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

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  // Package meterstatus contains an implementation of the api facade to
     5  // watch the meter status of a unit for changes and return the current meter status.
     6  package meterstatus
     7  
     8  import (
     9  	"github.com/juju/errors"
    10  	"github.com/juju/names"
    11  
    12  	"github.com/juju/juju/api/base"
    13  	"github.com/juju/juju/api/watcher"
    14  	"github.com/juju/juju/apiserver/params"
    15  )
    16  
    17  // MeterStatusClient defines the methods on the MeterStatus API end point.
    18  type MeterStatusClient interface {
    19  	// MeterStatus returns the meter status and additional information for the
    20  	// API client.
    21  	MeterStatus() (string, string, error)
    22  	// WatchMeterStatus returns a watcher for observing changes to the unit's meter
    23  	// status.
    24  	WatchMeterStatus() (watcher.NotifyWatcher, error)
    25  }
    26  
    27  // NewClient creates a new client for accessing the MeterStatus API.
    28  func NewClient(caller base.APICaller, tag names.UnitTag) *Client {
    29  	return &Client{
    30  		facade: base.NewFacadeCaller(caller, "MeterStatus"),
    31  		tag:    tag,
    32  	}
    33  }
    34  
    35  var _ MeterStatusClient = (*Client)(nil)
    36  
    37  // Client provides access to the meter status API.
    38  type Client struct {
    39  	facade base.FacadeCaller
    40  	tag    names.UnitTag
    41  }
    42  
    43  // MeterStatus is part of the MeterStatusClient interface.
    44  func (c *Client) MeterStatus() (statusCode, statusInfo string, rErr error) {
    45  	var results params.MeterStatusResults
    46  	args := params.Entities{
    47  		Entities: []params.Entity{{Tag: c.tag.String()}},
    48  	}
    49  	err := c.facade.FacadeCall("GetMeterStatus", args, &results)
    50  	if err != nil {
    51  		return "", "", errors.Trace(err)
    52  	}
    53  	if len(results.Results) != 1 {
    54  		return "", "", errors.Errorf("expected 1 result, got %d", len(results.Results))
    55  	}
    56  	result := results.Results[0]
    57  	if result.Error != nil {
    58  		return "", "", errors.Trace(result.Error)
    59  	}
    60  	return result.Code, result.Info, nil
    61  }
    62  
    63  // WatchMeterStatus is part of the MeterStatusClient interface.
    64  func (c *Client) WatchMeterStatus() (watcher.NotifyWatcher, error) {
    65  	var results params.NotifyWatchResults
    66  	args := params.Entities{
    67  		Entities: []params.Entity{{Tag: c.tag.String()}},
    68  	}
    69  	err := c.facade.FacadeCall("WatchMeterStatus", args, &results)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	if len(results.Results) != 1 {
    74  		return nil, errors.Errorf("expected 1 result, got %d", len(results.Results))
    75  	}
    76  	result := results.Results[0]
    77  	if result.Error != nil {
    78  		return nil, result.Error
    79  	}
    80  	w := watcher.NewNotifyWatcher(c.facade.RawAPICaller(), result)
    81  	return w, nil
    82  }