Skip to content

Commit

Permalink
daemon: fix /v2/snapshots import handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mvo5 committed Oct 1, 2020
1 parent 8299169 commit 07e063a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 18 deletions.
1 change: 0 additions & 1 deletion daemon/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ var api = []*Command{
debugCmd,
snapshotCmd,
snapshotExportCmd,
snapshotImportCmd,
connectionsCmd,
modelCmd,
cohortsCmd,
Expand Down
16 changes: 6 additions & 10 deletions daemon/api_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ func (action snapshotAction) String() string {
}

func changeSnapshots(c *Command, r *http.Request, user *auth.UserState) Response {
contentType := r.Header.Get("Content-Type")
if contentType == "application/x.snapd.snapshot-v1" {
return doSnapshotImport(c, r, user)
}

var action snapshotAction
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&action); err != nil {
Expand Down Expand Up @@ -174,16 +179,7 @@ func getSnapshotExport(c *Command, r *http.Request, user *auth.UserState) Respon
return &snapshotExportResponse{SnapshotExport: export}
}

var snapshotImportCmd = &Command{
Path: "/v2/snapshot",
POST: postSnapshots,
}

func postSnapshots(c *Command, r *http.Request, user *auth.UserState) Response {
contentType := r.Header.Get("Content-Type")
if contentType != "application/x.snapd.snapshot-v1" {
return BadRequest("cannot use content type %v", contentType)
}
func doSnapshotImport(c *Command, r *http.Request, user *auth.UserState) Response {

// XXX: check that we have enough space to import the compressed snapshots

Expand Down
4 changes: 2 additions & 2 deletions daemon/api_snapshots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func (s *snapshotSuite) TestImportSnapshot(c *check.C) {
c.Assert(err, check.IsNil)
req.Header.Set("Content-Type", "application/x.snapd.snapshot-v1")

rsp := daemon.PostSnapshots(daemon.SnapshotImportCmd, req, nil)
rsp := daemon.ChangeSnapshots(daemon.SnapshotCmd, req, nil)
c.Check(rsp.Type, check.Equals, daemon.ResponseTypeSync)
c.Check(rsp.Status, check.Equals, 200)
c.Check(rsp.Result, check.DeepEquals, map[string]interface{}{"set-id": setID, "snaps": snapNames})
Expand All @@ -416,7 +416,7 @@ func (s *snapshotSuite) TestImportSnapshotError(c *check.C) {
c.Assert(err, check.IsNil)
req.Header.Set("Content-Type", "application/x.snapd.snapshot-v1")

rsp := daemon.PostSnapshots(daemon.SnapshotImportCmd, req, nil)
rsp := daemon.ChangeSnapshots(daemon.SnapshotCmd, req, nil)
c.Assert(rsp.Type, check.Equals, daemon.ResponseTypeError)
c.Check(rsp.Status, check.Equals, 400)
c.Check(rsp.ErrorResult().Message, check.Equals, "no")
Expand Down
5 changes: 0 additions & 5 deletions daemon/export_api_snapshots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,10 @@ func ExportSnapshot(c *Command, r *http.Request, user *auth.UserState) interface
return getSnapshotExport(c, r, user)
}

func PostSnapshots(c *Command, r *http.Request, user *auth.UserState) *resp {
return postSnapshots(c, r, user).(*resp)
}

var (
SnapshotMany = snapshotMany
SnapshotCmd = snapshotCmd
SnapshotExportCmd = snapshotExportCmd
SnapshotImportCmd = snapshotImportCmd
)

type SnapshotExportResponse = snapshotExportResponse

0 comments on commit 07e063a

Please sign in to comment.