Merge lp:~dave-cheney/juju-core/go-cmd-juju-add-unit into lp:~juju/juju-core/trunk

Proposed by Dave Cheney
Status: Merged
Approved by: Gustavo Niemeyer
Approved revision: no longer in the source branch.
Merged at revision: 374
Proposed branch: lp:~dave-cheney/juju-core/go-cmd-juju-add-unit
Merge into: lp:~juju/juju-core/trunk
Diff against target: 166 lines (+127/-0)
5 files modified
cmd/juju/addunit.go (+62/-0)
cmd/juju/addunit_test.go (+46/-0)
cmd/juju/cmd_test.go (+17/-0)
cmd/juju/main.go (+1/-0)
cmd/juju/main_test.go (+1/-0)
To merge this branch: bzr merge lp:~dave-cheney/juju-core/go-cmd-juju-add-unit
Reviewer Review Type Date Requested Status
The Go Language Gophers Pending
Review via email: mp+118280@code.launchpad.net

Description of the change

cmd/juju: add add-unit command

add-unit adds units.

https://codereview.appspot.com/6449093/

To post a comment you must log in.
Revision history for this message
William Reade (fwereade) wrote :

Code looks fine, but:

https://codereview.appspot.com/6449093/diff/4001/cmd/juju/addunit.go
File cmd/juju/addunit.go (right):

https://codereview.appspot.com/6449093/diff/4001/cmd/juju/addunit.go#newcode1
cmd/juju/addunit.go:1: package main
Can we call this add-unit please? Non-matching names just bug me :).

https://codereview.appspot.com/6449093/diff/4001/cmd/juju/addunit.go#newcode51
cmd/juju/addunit.go:51: st, err := conn.State()
I *think* that this is Doing It Wrong, and that Conn clients are not
meant to interact with state directly, but I may have the wrong end of
the stick.

https://codereview.appspot.com/6449093/

Revision history for this message
Roger Peppe (rogpeppe) wrote :

On 2012/08/06 09:16:36, dfc wrote:
> Thank you for your review.

> https://codereview.appspot.com/6449093/diff/4001/cmd/juju/addunit.go
> File cmd/juju/addunit.go (right):

https://codereview.appspot.com/6449093/diff/4001/cmd/juju/addunit.go#newcode51
> cmd/juju/addunit.go:51: st, err := conn.State()
> On 2012/08/06 06:46:37, fwereade wrote:
> > I *think* that this is Doing It Wrong, and that Conn clients are not
meant to
> > interact with state directly, but I may have the wrong end of the
stick.

> if conn.AddUnits took the service name as a string, we can avoid going
via the
> state. Should I raise a bug to address this ?

That could work. I'm still not sure where the correct line is between
juju.Conn and state.State. We're definitely not *hiding* the State - I'm
inclined to add operations to juju.Conn that make it trivial to
implement the juju command-line operations. AddUnits on the boundary
here, as it's trivial to implement on the State too.

https://codereview.appspot.com/6449093/

Revision history for this message
Roger Peppe (rogpeppe) wrote :
Revision history for this message
Gustavo Niemeyer (niemeyer) wrote :

LGTM with trivials:

https://codereview.appspot.com/6449093/diff/4001/cmd/juju/addunit.go
File cmd/juju/addunit.go (right):

https://codereview.appspot.com/6449093/diff/4001/cmd/juju/addunit.go#newcode1
cmd/juju/addunit.go:1: package main
On 2012/08/06 06:46:37, fwereade wrote:
> Can we call this add-unit please? Non-matching names just bug me :).

+1

https://codereview.appspot.com/6449093/diff/4001/cmd/juju/addunit.go#newcode24
cmd/juju/addunit.go:24: f.IntVar(&c.NumUnits, "num-units", 1, "Number of
service units to add.")
s/Number/number/
s/\.//

Also, I believe we have a -n shorthand for this.

https://codereview.appspot.com/6449093/diff/4001/cmd/juju/addunit.go#newcode51
cmd/juju/addunit.go:51: st, err := conn.State()
On 2012/08/06 09:16:38, dfc wrote:
> On 2012/08/06 06:46:37, fwereade wrote:
> > I *think* that this is Doing It Wrong, and that Conn clients are not
meant to
> > interact with state directly, but I may have the wrong end of the
stick.

> if conn.AddUnits took the service name as a string, we can avoid going
via the
> state. Should I raise a bug to address this ?

I think this is good as it is for now. Conn may be seen as a set of
high-level logic used by command lines, but IMO we don't need one-liner
wrappers there for functionality like the one seen here.

https://codereview.appspot.com/6449093/

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'cmd/juju/addunit.go'
--- cmd/juju/addunit.go 1970-01-01 00:00:00 +0000
+++ cmd/juju/addunit.go 2012-08-06 04:48:27 +0000
@@ -0,0 +1,62 @@
1package main
2
3import (
4 "errors"
5
6 "launchpad.net/gnuflag"
7 "launchpad.net/juju-core/cmd"
8 "launchpad.net/juju-core/juju"
9)
10
11// AddUnitCommand is responsible adding additional units to a service.
12type AddUnitCommand struct {
13 EnvName string
14 ServiceName string
15 NumUnits int
16}
17
18func (c *AddUnitCommand) Info() *cmd.Info {
19 return &cmd.Info{"add-unit", "", "add a service unit", ""}
20}
21
22func (c *AddUnitCommand) Init(f *gnuflag.FlagSet, args []string) error {
23 addEnvironFlags(&c.EnvName, f)
24 f.IntVar(&c.NumUnits, "num-units", 1, "Number of service units to add.")
25 if err := f.Parse(true, args); err != nil {
26 return err
27 }
28 args = f.Args()
29 switch len(args) {
30 case 1:
31 c.ServiceName = args[0]
32 case 0:
33 return errors.New("no service specified")
34 default:
35 return cmd.CheckEmpty(args[1:])
36 }
37 if c.NumUnits < 1 {
38 return errors.New("must add at least one unit")
39 }
40 return nil
41}
42
43// Run connects to the environment specified on the command line
44// and calls conn.AddUnits.
45func (c *AddUnitCommand) Run(_ *cmd.Context) error {
46 conn, err := juju.NewConn(c.EnvName)
47 if err != nil {
48 return err
49 }
50 defer conn.Close()
51 st, err := conn.State()
52 if err != nil {
53 return err
54 }
55 service, err := st.Service(c.ServiceName)
56 if err != nil {
57 return err
58 }
59 _, err = conn.AddUnits(service, c.NumUnits)
60 return err
61
62}
063
=== added file 'cmd/juju/addunit_test.go'
--- cmd/juju/addunit_test.go 1970-01-01 00:00:00 +0000
+++ cmd/juju/addunit_test.go 2012-08-06 04:48:27 +0000
@@ -0,0 +1,46 @@
1package main
2
3import (
4 "bytes"
5 . "launchpad.net/gocheck"
6 "launchpad.net/juju-core/charm"
7 "launchpad.net/juju-core/cmd"
8 "launchpad.net/juju-core/testing"
9)
10
11type AddUnitSuite struct {
12 DeploySuite
13}
14
15var _ = Suite(&AddUnitSuite{})
16
17func (s *AddUnitSuite) SetUpTest(c *C) {
18 s.DeploySuite.SetUpTest(c)
19}
20
21func (s *AddUnitSuite) TearDownTest(c *C) {
22 s.DeploySuite.TearDownTest(c)
23}
24
25func runAddUnit(c *C, args ...string) error {
26 com := &AddUnitCommand{}
27 err := com.Init(newFlagSet(), args)
28 c.Assert(err, IsNil)
29 return com.Run(&cmd.Context{c.MkDir(), &bytes.Buffer{}, &bytes.Buffer{}})
30}
31
32func (s *AddUnitSuite) TestAddUnit(c *C) {
33 testing.Charms.BundlePath(s.seriesPath, "dummy")
34 err := runDeploy(c, "local:dummy", "some-service-name")
35 c.Assert(err, IsNil)
36 curl := charm.MustParseURL("local:precise/dummy-1")
37 s.assertService(c, "some-service-name", curl, 1, 0)
38
39 err = runAddUnit(c, "some-service-name")
40 c.Assert(err, IsNil)
41 s.assertService(c, "some-service-name", curl, 2, 0)
42
43 err = runAddUnit(c, "--num-units", "2", "some-service-name")
44 c.Assert(err, IsNil)
45 s.assertService(c, "some-service-name", curl, 4, 0)
46}
047
=== modified file 'cmd/juju/cmd_test.go'
--- cmd/juju/cmd_test.go 2012-07-30 16:51:00 +0000
+++ cmd/juju/cmd_test.go 2012-08-06 04:48:27 +0000
@@ -264,3 +264,20 @@
264264
265 // environment tested elsewhere265 // environment tested elsewhere
266}266}
267
268func initAddUnitCommand(args ...string) (*AddUnitCommand, error) {
269 com := &AddUnitCommand{}
270 return com, com.Init(newFlagSet(), args)
271}
272
273func (*cmdSuite) TestAddUnitCommandInit(c *C) {
274 // missing args
275 _, err := initAddUnitCommand()
276 c.Assert(err, ErrorMatches, "no service specified")
277
278 // bad unit count
279 _, err = initAddUnitCommand("service-name", "--num-units", "0")
280 c.Assert(err, ErrorMatches, "must add at least one unit")
281
282 // environment tested elsewhere
283}
267284
=== modified file 'cmd/juju/main.go'
--- cmd/juju/main.go 2012-07-18 00:33:07 +0000
+++ cmd/juju/main.go 2012-08-06 04:48:27 +0000
@@ -25,6 +25,7 @@
25// provides an entry point for testing with arbitrary command line arguments.25// provides an entry point for testing with arbitrary command line arguments.
26func Main(args []string) {26func Main(args []string) {
27 juju := &cmd.SuperCommand{Name: "juju", Doc: jujuDoc, Log: &cmd.Log{}}27 juju := &cmd.SuperCommand{Name: "juju", Doc: jujuDoc, Log: &cmd.Log{}}
28 juju.Register(&AddUnitCommand{})
28 juju.Register(&BootstrapCommand{})29 juju.Register(&BootstrapCommand{})
29 juju.Register(&DeployCommand{})30 juju.Register(&DeployCommand{})
30 juju.Register(&DestroyCommand{})31 juju.Register(&DestroyCommand{})
3132
=== modified file 'cmd/juju/main_test.go'
--- cmd/juju/main_test.go 2012-07-25 15:46:01 +0000
+++ cmd/juju/main_test.go 2012-08-06 04:48:27 +0000
@@ -138,6 +138,7 @@
138}138}
139139
140var commandNames = []string{140var commandNames = []string{
141 "add-unit",
141 "bootstrap",142 "bootstrap",
142 "deploy",143 "deploy",
143 "destroy-environment",144 "destroy-environment",

Subscribers

People subscribed via source and target branches