Skip to content

Commit

Permalink
Add client side rollback bits
Browse files Browse the repository at this point in the history
  • Loading branch information
mvo5 committed May 20, 2016
1 parent 12ed549 commit 43c6493
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
5 changes: 5 additions & 0 deletions client/snap_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func (client *Client) Refresh(name string, options *SnapOptions) (changeID strin
return client.doSnapAction("refresh", name, options)
}

// Rollback rolls the snap back to the previous on-disk version
func (client *Client) Rollback(name string, options *SnapOptions) (changeID string, err error) {
return client.doSnapAction("rollback", name, options)
}

func (client *Client) doSnapAction(actionName string, snapName string, options *SnapOptions) (changeID string, err error) {
action := actionData{
Action: actionName,
Expand Down
1 change: 1 addition & 0 deletions client/snap_op_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var ops = []struct {
{(*client.Client).Install, "install"},
{(*client.Client).Refresh, "refresh"},
{(*client.Client).Remove, "remove"},
{(*client.Client).Rollback, "rollback"},
}

func (cs *clientSuite) TestClientOpSnapServerError(c *check.C) {
Expand Down
27 changes: 27 additions & 0 deletions cmd/snap/cmd_snap_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,35 @@ func (x *cmdRefresh) Execute([]string) error {
return listSnaps([]string{name})
}

type cmdRollback struct {
Positional struct {
Snap string `positional-arg-name:"<snap>"`
} `positional-args:"yes"`
}

var shortRollbackHelp = i18n.G("Rollback the given snap to the previous version")
var longRollbackHelp = i18n.G(`
The rollback command will rollback the given snap to the previous version.
`)

func (x *cmdRollback) Execute(args []string) error {
cli := Client()
name := x.Positional.Snap
changeID, err := cli.Rollback(name, nil)
if err != nil {
return err
}

if _, err := wait(cli, changeID); err != nil {
return err
}
return listSnaps([]string{name})
}

func init() {
addCommand("remove", shortRemoveHelp, longRemoveHelp, func() flags.Commander { return &cmdRemove{} })
addCommand("install", shortInstallHelp, longInstallHelp, func() flags.Commander { return &cmdInstall{} })
addCommand("refresh", shortRefreshHelp, longRefreshHelp, func() flags.Commander { return &cmdRefresh{} })
addCommand("rollback", shortRollbackHelp, longRollbackHelp, func() flags.Commander { return &cmdRollback{} })

}
20 changes: 20 additions & 0 deletions cmd/snap/cmd_snap_op_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,23 @@ func (s *SnapOpSuite) TestInstallPathDevMode(c *check.C) {
// ensure that the fake server api was actually hit
c.Check(s.srv.n, check.Equals, s.srv.total)
}

func (s *SnapOpSuite) TestRollback(c *check.C) {
s.srv.checker = func(r *http.Request) {
c.Check(r.URL.Path, check.Equals, "/v2/snaps/foo.bar")
c.Check(DecodedRequestBody(c, r), check.DeepEquals, map[string]interface{}{
"action": "rollback",
"name": "foo.bar",
"channel": "chan",
})
}

s.RedirectClientToTestServer(s.srv.handle)
rest, err := snap.Parser().ParseArgs([]string{"rollback", "foo.bar"})
c.Assert(err, check.IsNil)
c.Assert(rest, check.DeepEquals, []string{})
c.Check(s.Stdout(), check.Matches, `(?sm).*foo\s+1.0\s+42\s+bar.*`)
c.Check(s.Stderr(), check.Equals, "")
// ensure that the fake server api was actually hit
c.Check(s.srv.n, check.Equals, s.srv.total)
}
2 changes: 1 addition & 1 deletion docs/rest.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ named "snap".

### POST

* Description: Install, refresh, or remove
* Description: Install, refresh, rollback or remove
* Access: trusted
* Operation: async
* Return: background operation or standard error
Expand Down

0 comments on commit 43c6493

Please sign in to comment.