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