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