From 85f38cd73936d017d7eab0f06204b1bd18b54930 Mon Sep 17 00:00:00 2001 From: Sylvain Baubeau Date: Thu, 18 Dec 2014 11:40:09 +0100 Subject: [PATCH] Allow setting description on snapshots using API --- api/router.go | 2 +- api/snapshot.go | 31 +++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/api/router.go b/api/router.go index b84356f7..b891e7b5 100644 --- a/api/router.go +++ b/api/router.go @@ -48,7 +48,7 @@ func Router(c *ctx.AptlyContext) http.Handler { { root.GET("/snapshots", apiSnapshotsList) root.POST("/snapshots", apiSnapshotsCreateEmpty) - root.PUT("/snapshots/:name", apiSnapshotsRename) + root.PUT("/snapshots/:name", apiSnapshotsUpdate) root.GET("/snapshots/:name", apiSnapshotsShow) root.GET("/snapshots/:name/packages", apiSnapshotsSearchPackages) root.DELETE("/snapshots/:name", apiSnapshotsDrop) diff --git a/api/snapshot.go b/api/snapshot.go index 5d833585..e6e4174f 100644 --- a/api/snapshot.go +++ b/api/snapshot.go @@ -34,6 +34,7 @@ func apiSnapshotsCreateFromMirror(c *gin.Context) { var b struct { Name string `binding:"required"` + Description string } if !c.Bind(&b) { @@ -72,6 +73,10 @@ func apiSnapshotsCreateFromMirror(c *gin.Context) { return } + if b.Description != "" { + snapshot.Description = b.Description + } + err = snapshotCollection.Add(snapshot) if err != nil { c.Fail(500, err) @@ -90,19 +95,24 @@ func apiSnapshotsCreateEmpty(c *gin.Context) { var b struct { Name string `binding:"required"` + Description string } if !c.Bind(&b) { return } + if b.Description == "" { + b.Description = "Created as empty" + } + snapshotCollection := context.CollectionFactory().SnapshotCollection() snapshotCollection.Lock() defer snapshotCollection.Unlock() packageList := deb.NewPackageList() - snapshot = deb.NewSnapshotFromPackageList(b.Name, nil, packageList, "Created as empty") + snapshot = deb.NewSnapshotFromPackageList(b.Name, nil, packageList, b.Description) err = snapshotCollection.Add(snapshot) if err != nil { @@ -123,6 +133,7 @@ func apiSnapshotsCreateFromRepository(c *gin.Context) { var b struct { Name string `binding:"required"` + Description string } if !c.Bind(&b) { @@ -155,6 +166,10 @@ func apiSnapshotsCreateFromRepository(c *gin.Context) { return } + if b.Description != "" { + snapshot.Description = b.Description + } + err = snapshotCollection.Add(snapshot) if err != nil { c.Fail(500, err) @@ -165,14 +180,15 @@ func apiSnapshotsCreateFromRepository(c *gin.Context) { } // PUT /api/snapshots/:name -func apiSnapshotsRename(c *gin.Context) { +func apiSnapshotsUpdate(c *gin.Context) { var ( err error snapshot *deb.Snapshot ) var b struct { - Name string `binding:"required"` + Name string + Description string } if !c.Bind(&b) { @@ -193,7 +209,14 @@ func apiSnapshotsRename(c *gin.Context) { c.Fail(409, fmt.Errorf("unable to rename: snapshot %s already exists", b.Name)) } - snapshot.Name = b.Name + if b.Name != "" { + snapshot.Name = b.Name + } + + if b.Description != "" { + snapshot.Description = b.Description + } + err = context.CollectionFactory().SnapshotCollection().Update(snapshot) if err != nil { c.Fail(403, err)