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

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package setwallet
     5  
     6  import (
     7  	"fmt"
     8  	"strconv"
     9  
    10  	"github.com/juju/cmd"
    11  	"github.com/juju/errors"
    12  	api "github.com/juju/romulus/api/budget"
    13  	"gopkg.in/macaroon-bakery.v2-unstable/httpbakery"
    14  
    15  	jujucmd "github.com/juju/juju/cmd"
    16  	rcmd "github.com/juju/juju/cmd/juju/romulus"
    17  	"github.com/juju/juju/cmd/modelcmd"
    18  )
    19  
    20  type setWalletCommand struct {
    21  	modelcmd.ControllerCommandBase
    22  	Name  string
    23  	Value string
    24  }
    25  
    26  // NewSetWalletCommand returns a new setWalletCommand.
    27  func NewSetWalletCommand() modelcmd.ControllerCommand {
    28  	return modelcmd.WrapController(&setWalletCommand{})
    29  }
    30  
    31  const doc = `
    32  Set the monthly wallet limit.
    33  
    34  Examples:
    35      # Sets the monthly limit for wallet named 'personal' to 96.
    36      juju set-wallet personal 96
    37  `
    38  
    39  // Info implements cmd.Command.Info.
    40  func (c *setWalletCommand) Info() *cmd.Info {
    41  	return jujucmd.Info(&cmd.Info{
    42  		Name:    "set-wallet",
    43  		Args:    "<wallet name> <value>",
    44  		Purpose: "Set the wallet limit.",
    45  		Doc:     doc,
    46  	})
    47  }
    48  
    49  // Init implements cmd.Command.Init.
    50  func (c *setWalletCommand) Init(args []string) error {
    51  	if len(args) < 2 {
    52  		return errors.New("name and value required")
    53  	}
    54  	c.Name, c.Value = args[0], args[1]
    55  	if _, err := strconv.ParseInt(c.Value, 10, 32); err != nil {
    56  		return errors.New("wallet value needs to be a whole number")
    57  	}
    58  	return c.CommandBase.Init(args[2:])
    59  }
    60  
    61  // Run implements cmd.Command.Run and contains most of the setwallet logic.
    62  func (c *setWalletCommand) Run(ctx *cmd.Context) error {
    63  	client, err := c.BakeryClient()
    64  	if err != nil {
    65  		return errors.Annotate(err, "failed to create an http client")
    66  	}
    67  	apiRoot, err := rcmd.GetMeteringURLForControllerCmd(&c.ControllerCommandBase)
    68  	if err != nil {
    69  		return errors.Trace(err)
    70  	}
    71  	api, err := newAPIClient(apiRoot, client)
    72  	if err != nil {
    73  		return errors.Annotate(err, "failed to create an api client")
    74  	}
    75  	resp, err := api.SetWallet(c.Name, c.Value)
    76  	if err != nil {
    77  		return errors.Annotate(err, "failed to set the wallet")
    78  	}
    79  	fmt.Fprintln(ctx.Stdout, resp)
    80  	return nil
    81  }
    82  
    83  var newAPIClient = newAPIClientImpl
    84  
    85  func newAPIClientImpl(apiRoot string, c *httpbakery.Client) (apiClient, error) {
    86  	return api.NewClient(api.APIRoot(apiRoot), api.HTTPClient(c))
    87  }
    88  
    89  type apiClient interface {
    90  	SetWallet(string, string) (string, error)
    91  }