github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/setmeterstatus/setmeterstatus.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package setmeterstatus
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    11  	"github.com/juju/gnuflag"
    12  	"gopkg.in/juju/names.v2"
    13  
    14  	"github.com/juju/juju/api/metricsdebug"
    15  	jujucmd "github.com/juju/juju/cmd"
    16  	"github.com/juju/juju/cmd/modelcmd"
    17  )
    18  
    19  const setMeterStatusDoc = `
    20  Set meter status on the given application or unit. This command is used
    21  to test the meter-status-changed hook for charms in development.
    22  
    23  Examples:
    24      # Set Red meter status on all units of myapp
    25      juju set-meter-status myapp RED
    26  
    27      # Set AMBER meter status with "my message" as info on unit myapp/0
    28      juju set-meter-status myapp/0 AMBER --info "my message"
    29  `
    30  
    31  // SetMeterStatusCommand sets the meter status on an application or unit. Useful for charm authors.
    32  type SetMeterStatusCommand struct {
    33  	modelcmd.ModelCommandBase
    34  	Tag        names.Tag
    35  	Status     string
    36  	StatusInfo string
    37  }
    38  
    39  // New creates a new SetMeterStatusCommand.
    40  func New() cmd.Command {
    41  	return modelcmd.Wrap(&SetMeterStatusCommand{})
    42  }
    43  
    44  // Info implements Command.Info.
    45  func (c *SetMeterStatusCommand) Info() *cmd.Info {
    46  	return jujucmd.Info(&cmd.Info{
    47  		Name:    "set-meter-status",
    48  		Args:    "[application or unit] status",
    49  		Purpose: "Sets the meter status on an application or unit.",
    50  		Doc:     setMeterStatusDoc,
    51  	})
    52  }
    53  
    54  // Init reads and verifies the cli arguments for the SetMeterStatusCommand
    55  func (c *SetMeterStatusCommand) Init(args []string) error {
    56  	if len(args) != 2 {
    57  		return errors.New("you need to specify an entity (application or unit) and a status")
    58  	}
    59  	if names.IsValidUnit(args[0]) {
    60  		c.Tag = names.NewUnitTag(args[0])
    61  	} else if names.IsValidApplication(args[0]) {
    62  		c.Tag = names.NewApplicationTag(args[0])
    63  	} else {
    64  		return errors.Errorf("%q is not a valid unit or application", args[0])
    65  	}
    66  	c.Status = args[1]
    67  
    68  	if err := cmd.CheckEmpty(args[2:]); err != nil {
    69  		return errors.Errorf("unknown command line arguments: " + strings.Join(args, ","))
    70  	}
    71  	return nil
    72  }
    73  
    74  // SetFlags implements Command.SetFlags.
    75  func (c *SetMeterStatusCommand) SetFlags(f *gnuflag.FlagSet) {
    76  	c.ModelCommandBase.SetFlags(f)
    77  	f.StringVar(&c.StatusInfo, "info", "", "Set the meter status info to this string")
    78  }
    79  
    80  // SetMeterStatusClient defines the juju api required by the command.
    81  type SetMeterStatusClient interface {
    82  	SetMeterStatus(tag, status, info string) error
    83  	Close() error
    84  }
    85  
    86  var newClient = func(env modelcmd.ModelCommandBase) (SetMeterStatusClient, error) {
    87  	state, err := env.NewAPIRoot()
    88  	if err != nil {
    89  		return nil, errors.Trace(err)
    90  	}
    91  	return metricsdebug.NewClient(state), nil
    92  }
    93  
    94  // Run implements Command.Run.
    95  func (c *SetMeterStatusCommand) Run(ctx *cmd.Context) error {
    96  	client, err := newClient(c.ModelCommandBase)
    97  	if err != nil {
    98  		return errors.Trace(err)
    99  	}
   100  	defer client.Close()
   101  	err = client.SetMeterStatus(c.Tag.String(), c.Status, c.StatusInfo)
   102  	if err != nil {
   103  		return errors.Trace(err)
   104  	}
   105  	return nil
   106  }