github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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  	"github.com/juju/juju/cmd/modelcmd"
    16  )
    17  
    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.
    21  
    22  Examples:
    23      # Set Red meter status on all units of myapp
    24      juju set-meter-status myapp RED
    25  
    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"
    28  `
    29  
    30  // SetMeterStatusCommand sets the meter status on an application or unit. Useful for charm authors.
    31  type SetMeterStatusCommand struct {
    32  	modelcmd.ModelCommandBase
    33  	Tag        names.Tag
    34  	Status     string
    35  	StatusInfo string
    36  }
    37  
    38  // New creates a new SetMeterStatusCommand.
    39  func New() cmd.Command {
    40  	return modelcmd.Wrap(&SetMeterStatusCommand{})
    41  }
    42  
    43  // Info implements Command.Info.
    44  func (c *SetMeterStatusCommand) Info() *cmd.Info {
    45  	return &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,
    50  	}
    51  }
    52  
    53  // Init reads and verifies the cli arguments for the SetMeterStatusCommand
    54  func (c *SetMeterStatusCommand) Init(args []string) error {
    55  	if len(args) != 2 {
    56  		return errors.New("you need to specify an entity (application or unit) and a status")
    57  	}
    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])
    62  	} else {
    63  		return errors.Errorf("%q is not a valid unit or application", args[0])
    64  	}
    65  	c.Status = args[1]
    66  
    67  	if err := cmd.CheckEmpty(args[2:]); err != nil {
    68  		return errors.Errorf("unknown command line arguments: " + strings.Join(args, ","))
    69  	}
    70  	return nil
    71  }
    72  
    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")
    77  }
    78  
    79  // SetMeterStatusClient defines the juju api required by the command.
    80  type SetMeterStatusClient interface {
    81  	SetMeterStatus(tag, status, info string) error
    82  	Close() error
    83  }
    84  
    85  var newClient = func(env modelcmd.ModelCommandBase) (SetMeterStatusClient, error) {
    86  	state, err := env.NewAPIRoot()
    87  	if err != nil {
    88  		return nil, errors.Trace(err)
    89  	}
    90  	return metricsdebug.NewClient(state), nil
    91  }
    92  
    93  // Run implements Command.Run.
    94  func (c *SetMeterStatusCommand) Run(ctx *cmd.Context) error {
    95  	client, err := newClient(c.ModelCommandBase)
    96  	if err != nil {
    97  		return errors.Trace(err)
    98  	}
    99  	defer client.Close()
   100  	err = client.SetMeterStatus(c.Tag.String(), c.Status, c.StatusInfo)
   101  	if err != nil {
   102  		return errors.Trace(err)
   103  	}
   104  	return nil
   105  }