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