github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/romulus/updateallocation/updateallocation.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  // Package updateallocation defines the command used to update allocations.
     5  package updateallocation
     6  
     7  import (
     8  	"fmt"
     9  	"strconv"
    10  
    11  	"github.com/juju/cmd"
    12  	"github.com/juju/errors"
    13  	"github.com/juju/juju/cmd/modelcmd"
    14  	"gopkg.in/macaroon-bakery.v1/httpbakery"
    15  
    16  	api "github.com/juju/romulus/api/budget"
    17  )
    18  
    19  type updateAllocationCommand struct {
    20  	modelcmd.ModelCommandBase
    21  	api   apiClient
    22  	Name  string
    23  	Value string
    24  }
    25  
    26  // NewUpdateAllocationCommand returns a new updateAllocationCommand.
    27  func NewUpdateAllocationCommand() modelcmd.ModelCommand {
    28  	return &updateAllocationCommand{}
    29  }
    30  
    31  func (c *updateAllocationCommand) newAPIClient(bakery *httpbakery.Client) (apiClient, error) {
    32  	if c.api != nil {
    33  		return c.api, nil
    34  	}
    35  	c.api = api.NewClient(bakery)
    36  	return c.api, nil
    37  }
    38  
    39  type apiClient interface {
    40  	UpdateAllocation(string, string, string) (string, error)
    41  }
    42  
    43  const doc = `
    44  Updates an existing allocation on an application.
    45  
    46  Examples:
    47      # Sets the allocation for the wordpress application to 10.
    48      juju update-allocation wordpress 10
    49  `
    50  
    51  // Info implements cmd.Command.Info.
    52  func (c *updateAllocationCommand) Info() *cmd.Info {
    53  	return &cmd.Info{
    54  		Name:    "update-allocation",
    55  		Args:    "<application> <value>",
    56  		Purpose: "Update an allocation.",
    57  		Doc:     doc,
    58  	}
    59  }
    60  
    61  // Init implements cmd.Command.Init.
    62  func (c *updateAllocationCommand) Init(args []string) error {
    63  	if len(args) < 2 {
    64  		return errors.New("application and value required")
    65  	}
    66  	c.Name, c.Value = args[0], args[1]
    67  	if _, err := strconv.ParseInt(c.Value, 10, 32); err != nil {
    68  		return errors.New("value needs to be a whole number")
    69  	}
    70  	return cmd.CheckEmpty(args[2:])
    71  }
    72  
    73  func (c *updateAllocationCommand) modelUUID() (string, error) {
    74  	model, err := c.ClientStore().ModelByName(c.ControllerName(), c.ModelName())
    75  	if err != nil {
    76  		return "", errors.Trace(err)
    77  	}
    78  	return model.ModelUUID, nil
    79  }
    80  
    81  // Run implements cmd.Command.Run and contains most of the setbudget logic.
    82  func (c *updateAllocationCommand) Run(ctx *cmd.Context) error {
    83  	modelUUID, err := c.modelUUID()
    84  	if err != nil {
    85  		return errors.Annotate(err, "failed to get model uuid")
    86  	}
    87  	client, err := c.BakeryClient()
    88  	if err != nil {
    89  		return errors.Annotate(err, "failed to create an http client")
    90  	}
    91  	api, err := c.newAPIClient(client)
    92  	if err != nil {
    93  		return errors.Annotate(err, "failed to create an api client")
    94  	}
    95  	resp, err := api.UpdateAllocation(modelUUID, c.Name, c.Value)
    96  	if err != nil {
    97  		return errors.Annotate(err, "failed to update the allocation")
    98  	}
    99  	fmt.Fprintln(ctx.Stdout, resp)
   100  	return nil
   101  }