1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
10
"github.com/juju/errors"
11
"gopkg.in/juju/names.v2"
12
"launchpad.net/gnuflag"
14
"github.com/juju/juju/api/metricsdebug"
15
"github.com/juju/juju/cmd/modelcmd"
18
const setMeterStatusDoc = `
19
Set meter status on the given application or unit. This command is used
20
to test the meter-status-changed hook for charms in development.
23
# Set Red meter status on all units of myapp
24
juju set-meter-status myapp RED
26
# Set AMBER meter status with "my message" as info on unit myapp/0
27
juju set-meter-status myapp/0 AMBER --info "my message"
30
// SetMeterStatusCommand sets the meter status on an application or unit. Useful for charm authors.
31
type SetMeterStatusCommand struct {
32
modelcmd.ModelCommandBase
38
// New creates a new SetMeterStatusCommand.
39
func New() cmd.Command {
40
return modelcmd.Wrap(&SetMeterStatusCommand{})
43
// Info implements Command.Info.
44
func (c *SetMeterStatusCommand) Info() *cmd.Info {
46
Name: "set-meter-status",
47
Args: "[application or unit] status",
48
Purpose: "Sets the meter status on an application or unit.",
49
Doc: setMeterStatusDoc,
53
// Init reads and verifies the cli arguments for the SetMeterStatusCommand
54
func (c *SetMeterStatusCommand) Init(args []string) error {
56
return errors.New("you need to specify an entity (application or unit) and a status")
58
if names.IsValidUnit(args[0]) {
59
c.Tag = names.NewUnitTag(args[0])
60
} else if names.IsValidApplication(args[0]) {
61
c.Tag = names.NewApplicationTag(args[0])
63
return errors.Errorf("%q is not a valid unit or application", args[0])
67
if err := cmd.CheckEmpty(args[2:]); err != nil {
68
return errors.Errorf("unknown command line arguments: " + strings.Join(args, ","))
73
// SetFlags implements Command.SetFlags.
74
func (c *SetMeterStatusCommand) SetFlags(f *gnuflag.FlagSet) {
75
c.ModelCommandBase.SetFlags(f)
76
f.StringVar(&c.StatusInfo, "info", "", "Set the meter status info to this string")
79
// SetMeterStatusClient defines the juju api required by the command.
80
type SetMeterStatusClient interface {
81
SetMeterStatus(tag, status, info string) error
85
var newClient = func(env modelcmd.ModelCommandBase) (SetMeterStatusClient, error) {
86
state, err := env.NewAPIRoot()
88
return nil, errors.Trace(err)
90
return metricsdebug.NewClient(state), nil
93
// Run implements Command.Run.
94
func (c *SetMeterStatusCommand) Run(ctx *cmd.Context) error {
95
client, err := newClient(c.ModelCommandBase)
97
return errors.Trace(err)
100
err = client.SetMeterStatus(c.Tag.String(), c.Status, c.StatusInfo)
102
return errors.Trace(err)