github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/romulus/createwallet/createwallet.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package createwallet 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 createWalletCommand struct { 21 modelcmd.ControllerCommandBase 22 Name string 23 Value string 24 } 25 26 // NewCreateWalletCommand returns a new createWalletCommand 27 func NewCreateWalletCommand() modelcmd.ControllerCommand { 28 return modelcmd.WrapController(&createWalletCommand{}) 29 } 30 31 const doc = ` 32 Create a new wallet with monthly limit. 33 34 Examples: 35 # Creates a wallet named 'qa' with a limit of 42. 36 juju create-wallet qa 42 37 ` 38 39 // Info implements cmd.Command.Info. 40 func (c *createWalletCommand) Info() *cmd.Info { 41 return jujucmd.Info(&cmd.Info{ 42 Name: "create-wallet", 43 Purpose: "Create a new wallet.", 44 Doc: doc, 45 }) 46 } 47 48 // Init implements cmd.Command.Init. 49 func (c *createWalletCommand) Init(args []string) error { 50 if len(args) < 2 { 51 return errors.New("name and value required") 52 } 53 c.Name, c.Value = args[0], args[1] 54 if _, err := strconv.ParseInt(c.Value, 10, 32); err != nil { 55 return errors.New("wallet value needs to be a whole number") 56 } 57 return c.CommandBase.Init(args[2:]) 58 } 59 60 // Run implements cmd.Command.Run and has most of the logic for the run command. 61 func (c *createWalletCommand) Run(ctx *cmd.Context) error { 62 client, err := c.BakeryClient() 63 if err != nil { 64 return errors.Annotate(err, "failed to create an http client") 65 } 66 apiRoot, err := rcmd.GetMeteringURLForControllerCmd(&c.ControllerCommandBase) 67 if err != nil { 68 return errors.Trace(err) 69 } 70 api, err := newAPIClient(apiRoot, client) 71 if err != nil { 72 return errors.Annotate(err, "failed to create an api client") 73 } 74 resp, err := api.CreateWallet(c.Name, c.Value) 75 if err != nil { 76 return errors.Annotate(err, "failed to create the wallet") 77 } 78 fmt.Fprintln(ctx.Stdout, resp) 79 return nil 80 } 81 82 var newAPIClient = newAPIClientImpl 83 84 func newAPIClientImpl(apiRoot string, c *httpbakery.Client) (apiClient, error) { 85 return api.NewClient(api.APIRoot(apiRoot), api.HTTPClient(c)) 86 } 87 88 type apiClient interface { 89 CreateWallet(name string, limit string) (string, error) 90 }