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