Permalink
Browse files

use class/id for untrack

  • Loading branch information...
1 parent d6993b6 commit 06ef4664272c5110fe1198ac778a28fba7e73ee4 @wwitzel3 committed Oct 15, 2015
Showing with 45 additions and 20 deletions.
  1. +40 −16 workload/context/unregister.go
  2. +5 −4 workload/context/unregister_test.go
@@ -10,40 +10,64 @@ import (
const UntrackCmdName = "payload-unregister" const UntrackCmdName = "payload-unregister"
-// NewUntrackCmd returns an UntrackCmd that uses the given hook context. +// NewUntrackCmd returns a new UntrackCmd that wraps the given context.
func NewUntrackCmd(ctx HookContext) (*UntrackCmd, error) { func NewUntrackCmd(ctx HookContext) (*UntrackCmd, error) {
- base, err := newCommand(ctx) + compCtx, err := ContextComponent(ctx)
if err != nil { if err != nil {
+ // The component wasn't tracked properly.
return nil, errors.Trace(err) return nil, errors.Trace(err)
} }
- c := &UntrackCmd{ + return &UntrackCmd{api: compCtx}, nil
- baseCommand: base, +}
- } +
- c.cmdInfo = cmdInfo{ +// Info implements cmd.Command.
+func (c UntrackCmd) Info() *cmd.Info {
+ return &cmd.Info{
Name: "payload-unregister", Name: "payload-unregister",
- Summary: "stop tracking a payload", + Args: "<class> <id>",
+ Purpose: "stop tracking a payload",
Doc: ` Doc: `
"payload-unregister" is used while a hook is running to let Juju know "payload-unregister" is used while a hook is running to let Juju know
-that a payload has been manually stopped. The id +that a payload has been manually stopped. The <class> and <id> provided
-used to start tracking the payload must be provided. +must match a payload that has been previously registered with juju using
+payload-register.
`, `,
} }
- return c, nil
} }
-var _ cmd.Command = (*UntrackCmd)(nil)
-
// UntrackCmd implements the untrack command. // UntrackCmd implements the untrack command.
type UntrackCmd struct { type UntrackCmd struct {
- *baseCommand + *cmd.CommandBase
+
+ api Component
+ class string
+ id string
+}
+
+// Init implements cmd.Command.
+func (c *UntrackCmd) Init(args []string) error {
+ if len(args) < 2 {
+ return errors.Errorf("missing required arguments")
+ }
+ c.class = args[0]
+ c.id = args[1]
+ return nil
} }
// Run runs the untrack command. // Run runs the untrack command.
func (c *UntrackCmd) Run(ctx *cmd.Context) error { func (c *UntrackCmd) Run(ctx *cmd.Context) error {
- logger.Tracef("Running untrack command with id %q", c.ID) + ID := c.class + "/" + c.id
- if err := c.baseCommand.Run(ctx); err != nil { + logger.Tracef("Running untrack command for %q", ID)
+
+ if err := c.api.Untrack(ID); err != nil {
+ return errors.Trace(err)
+ }
+
+ // We flush to state immedeiately so that status reflects the
+ // workload correctly.
+ if err := c.api.Flush(); err != nil {
return errors.Trace(err) return errors.Trace(err)
} }
- return c.compCtx.Untrack(c.ID) + return nil
} }
@@ -33,19 +33,20 @@ func (s *untrackSuite) TestCommandRegistered(c *gc.C) {
func (s *untrackSuite) TestHelp(c *gc.C) { func (s *untrackSuite) TestHelp(c *gc.C) {
s.checkHelp(c, ` s.checkHelp(c, `
-usage: payload-unregister <name-or-id> +usage: payload-unregister <class> <id>
purpose: stop tracking a payload purpose: stop tracking a payload
"payload-unregister" is used while a hook is running to let Juju know "payload-unregister" is used while a hook is running to let Juju know
-that a payload has been manually stopped. The id +that a payload has been manually stopped. The <class> and <id> provided
-used to start tracking the payload must be provided. +must match a payload that has been previously registered with juju using
+payload-register.
`[1:]) `[1:])
} }
func (s *untrackSuite) TestRunOkay(c *gc.C) { func (s *untrackSuite) TestRunOkay(c *gc.C) {
s.setMetadata(s.workload) s.setMetadata(s.workload)
s.compCtx.workloads[s.workload.ID()] = s.workload s.compCtx.workloads[s.workload.ID()] = s.workload
- err := s.cmd.Init([]string{s.workload.Name}) + err := s.cmd.Init([]string{s.workload.Name, s.workload.Details.ID})
c.Assert(err, jc.ErrorIsNil) c.Assert(err, jc.ErrorIsNil)
c.Logf("%#v", s.cmd) c.Logf("%#v", s.cmd)

0 comments on commit 06ef466

Please sign in to comment.