From c3fd172c27289ce14c350bb21f9499f58c939866 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Roth?= Date: Thu, 3 Oct 2024 14:33:52 +0200 Subject: [PATCH] fix REST api --- api/snapshot.go | 13 +++++++------ system/t12_api/snapshots.py | 27 +++++++++------------------ 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/api/snapshot.go b/api/snapshot.go index 35a417681..898f01a24 100644 --- a/api/snapshot.go +++ b/api/snapshot.go @@ -486,8 +486,6 @@ func apiSnapshotsMerge(c *gin.Context) { type snapshotsPullBody struct { // Source name where packages and dependencies will be searched Source string `binding:"required" json:"Source" example:"source-snapshot"` - // Snapshot where packages and dependencies will be pulled to - To string `binding:"required" json:"To" example:"to-snapshot"` // Name of the snapshot that will be created Destination string `binding:"required" json:"Destination" example:"idestination-snapshot"` // List of package queries, in the simplest form, name of package to be pulled from @@ -504,13 +502,14 @@ type snapshotsPullBody struct { // @Param no-deps query int false "no-deps: 1 to enable" // @Param no-remove query int false "no-remove: 1 to enable" // @Accept json +// @Param name path string true "Snapshot where packages and dependencies will be pulled to" // @Param request body snapshotsPullBody true "See api.snapshotsPullBody" // @Produce json // @Success 200 // @Failure 400 {object} Error "Bad Request" // @Failure 404 {object} Error "Not Found" // @Failure 500 {object} Error "Internal Error" -// @Router /api/snapshots/pull [post] +// @Router /api/snapshots/{name}/pull [post] func apiSnapshotsPull(c *gin.Context) { var ( err error @@ -518,6 +517,8 @@ func apiSnapshotsPull(c *gin.Context) { body snapshotsPullBody ) + name := c.Params.ByName("name") + if err = c.BindJSON(&body); err != nil { AbortWithJSONError(c, http.StatusBadRequest, err) return @@ -530,8 +531,8 @@ func apiSnapshotsPull(c *gin.Context) { collectionFactory := context.NewCollectionFactory() - // Load snapshot - toSnapshot, err := collectionFactory.SnapshotCollection().ByName(body.To) + // Load snapshot + toSnapshot, err := collectionFactory.SnapshotCollection().ByName(name) if err != nil { AbortWithJSONError(c, http.StatusNotFound, err) return @@ -555,7 +556,7 @@ func apiSnapshotsPull(c *gin.Context) { } resources := []string{string(sourceSnapshot.ResourceKey()), string(toSnapshot.ResourceKey())} - taskName := fmt.Sprintf("Pull snapshot %s into %s and save as %s", body.Source, body.To, body.Destination) + taskName := fmt.Sprintf("Pull snapshot %s into %s and save as %s", body.Source, name, body.Destination) maybeRunTaskInBackground(c, taskName, resources, func(_ aptly.Progress, _ *task.Detail) (*task.ProcessReturnValue, error) { // convert snapshots to package list toPackageList, err := deb.NewPackageListFromRefList(toSnapshot.RefList(), collectionFactory.PackageCollection(), context.Progress()) diff --git a/system/t12_api/snapshots.py b/system/t12_api/snapshots.py index 78179536b..51915dd1b 100644 --- a/system/t12_api/snapshots.py +++ b/system/t12_api/snapshots.py @@ -434,9 +434,8 @@ def check(self): snapshot_pull_libboost = self.random_name() # dry run first - resp = self.post("/api/snapshots/pull?dry-run=1", json={ + resp = self.post(f"/api/snapshots/{snapshot_empty_repo}/pull?dry-run=1", json={ 'Source': snapshot_repo_with_libboost, - 'To': snapshot_empty_repo, 'Destination': snapshot_pull_libboost, 'Queries': [ 'libboost-program-options-dev' @@ -449,9 +448,8 @@ def check(self): self.check_equal(resp.status_code, 200) # dry run, all-matches - resp = self.post("/api/snapshots/pull?dry-run=1&all-matches=1", json={ + resp = self.post("/api/snapshots/{snapshot_empty_repo}/pull?dry-run=1&all-matches=1", json={ 'Source': snapshot_repo_with_libboost, - 'To': snapshot_empty_repo, 'Destination': snapshot_pull_libboost, 'Queries': [ 'libboost-program-options-dev' @@ -464,17 +462,15 @@ def check(self): self.check_equal(resp.status_code, 200) # missing argument - resp = self.post("/api/snapshots/pull", json={ + resp = self.post("/api/snapshots/{snapshot_empty_repo}/pull", json={ 'Source': snapshot_repo_with_libboost, - 'To': snapshot_empty_repo, 'Destination': snapshot_pull_libboost, }) self.check_equal(resp.status_code, 400) # dry run, emtpy architectures - resp = self.post("/api/snapshots/pull?dry-run=1", json={ + resp = self.post("/api/snapshots/{snapshot_empty_repo}/pull?dry-run=1", json={ 'Source': snapshot_repo_with_libboost, - 'To': snapshot_empty_repo, 'Destination': snapshot_pull_libboost, 'Queries': [ 'libboost-program-options-dev' @@ -484,9 +480,8 @@ def check(self): self.check_equal(resp.status_code, 500) # dry run, non-existing To - resp = self.post("/api/snapshots/pull", json={ + resp = self.post("/api/snapshots/asd123/pull", json={ 'Source': snapshot_repo_with_libboost, - 'To': "asd123", 'Destination': snapshot_pull_libboost, 'Queries': [ 'libboost-program-options-dev' @@ -495,9 +490,8 @@ def check(self): self.check_equal(resp.status_code, 404) # dry run, non-existing source - resp = self.post("/api/snapshots/pull?dry-run=1", json={ + resp = self.post("/api/snapshots/{snapshot_empty_repo}/pull?dry-run=1", json={ 'Source': "asd123", - 'To': snapshot_empty_repo, 'Destination': snapshot_pull_libboost, 'Queries': [ 'libboost-program-options-dev' @@ -506,9 +500,8 @@ def check(self): self.check_equal(resp.status_code, 404) # snapshot pull - resp = self.post("/api/snapshots/pull", json={ + resp = self.post("/api/snapshots/{snapshot_empty_repo}/pull", json={ 'Source': snapshot_repo_with_libboost, - 'To': snapshot_empty_repo, 'Destination': snapshot_pull_libboost, 'Queries': [ 'libboost-program-options-dev' @@ -532,9 +525,8 @@ def check(self): # pull from non-existing source non_existing_source = self.random_name() destination = self.random_name() - resp = self.post("/api/snapshots/pull", json={ + resp = self.post("/api/snapshots/{snapshot_empty_repo}/pull", json={ 'Source': non_existing_source, - 'To': snapshot_empty_repo, 'Destination': destination, 'Queries': [ 'Name (~ *)' @@ -549,9 +541,8 @@ def check(self): # pull to non-existing snapshot non_existing_snapshot = self.random_name() destination = self.random_name() - resp = self.post("/api/snapshots/pull", json={ + resp = self.post("/api/snapshots/{snapshot_empty_repo}/pull", json={ 'Source': non_existing_snapshot, - 'To': snapshot_empty_repo, 'Destination': destination, 'Queries': [ 'Name (~ *)'