Skip to content

Commit

Permalink
database/api: add layer deletion support
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin-M authored and jzelinskie committed Feb 24, 2016
1 parent 248fc7d commit 726bd3c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
4 changes: 2 additions & 2 deletions api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func POSTLayers(w http.ResponseWriter, r *http.Request, _ httprouter.Params, e *
// DELETELayers deletes the specified layer and any child layers that are
// dependent on the specified layer.
func DELETELayers(w http.ResponseWriter, r *http.Request, p httprouter.Params, e *Env) {
if err := e.Datastore.DeleteLayer(p.ByName("id")); err != nil {
if err := e.Datastore.DeleteLayer(p.ByName("name")); err != nil {
httputils.WriteHTTPError(w, 0, err)
return
}
Expand All @@ -93,7 +93,7 @@ func GETLayers(w http.ResponseWriter, r *http.Request, p httprouter.Params, e *E
_, withFeatures := r.URL.Query()["withFeatures"]
_, withVulnerabilities := r.URL.Query()["withVulnerabilities"]

layer, err := e.Datastore.FindLayer(p.ByName("id"), withFeatures, withVulnerabilities)
layer, err := e.Datastore.FindLayer(p.ByName("name"), withFeatures, withVulnerabilities)
if err != nil {
httputils.WriteHTTPError(w, 0, err)
return
Expand Down
4 changes: 2 additions & 2 deletions api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func NewRouterV1(to time.Duration, env *Env) *httprouter.Router {

// Layers
router.POST("/layers", wrap(POSTLayers, env))
router.DELETE("/layers/:id", wrap(DELETELayers, env))
router.GET("/layers/:id", wrap(GETLayers, env))
router.DELETE("/layers/:name", wrap(DELETELayers, env))
router.GET("/layers/:name", wrap(GETLayers, env))

// Vulnerabilities
// router.POST("/vulnerabilities", wrap(logic.POSTVulnerabilities))
Expand Down
15 changes: 14 additions & 1 deletion database/pgsql/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,19 @@ func createNV(features []database.FeatureVersion) (map[string]*database.FeatureV
}

func (pgSQL *pgSQL) DeleteLayer(name string) error {
// TODO(Quentin-M): Implement and test me.
result, err := pgSQL.Exec(getQuery("r_layer"), name)
if err != nil {
return handleError("r_layer", err)
}

affected, err := result.RowsAffected()
if err != nil {
return handleError("r_layer.RowsAffected()", err)
}

if affected <= 0 {
return cerrors.ErrNotFound
}

return nil
}
20 changes: 16 additions & 4 deletions database/pgsql/layer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"

"github.com/coreos/clair/database"
cerrors "github.com/coreos/clair/utils/errors"
"github.com/coreos/clair/utils/types"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -101,7 +102,7 @@ func TestFindLayer(t *testing.T) {
}

func TestInsertLayer(t *testing.T) {
datastore, err := OpenForTest("InsertLayer", true)
datastore, err := OpenForTest("InsertLayer", false)
if err != nil {
t.Error(err)
return
Expand All @@ -118,7 +119,20 @@ func TestInsertLayer(t *testing.T) {
// TODO(Quentin-M)

// Delete layer.
// TODO(Quentin-M)
err = datastore.DeleteLayer("TestInsertLayerX")
assert.Equal(t, cerrors.ErrNotFound, err)

err = datastore.DeleteLayer("TestInsertLayer3")
assert.Nil(t, err)

_, err = datastore.FindLayer("TestInsertLayer3", false, false)
assert.Equal(t, cerrors.ErrNotFound, err)

_, err = datastore.FindLayer("TestInsertLayer4a", false, false)
assert.Equal(t, cerrors.ErrNotFound, err)

_, err = datastore.FindLayer("TestInsertLayer4b", true, false)
assert.Equal(t, cerrors.ErrNotFound, err)
}

func testInsertLayerInvalid(t *testing.T, datastore database.Datastore) {
Expand All @@ -135,8 +149,6 @@ func testInsertLayerInvalid(t *testing.T, datastore database.Datastore) {
}

func testInsertLayerTree(t *testing.T, datastore database.Datastore) {
fmt.Println("- testInsertLayerTree")

f1 := database.FeatureVersion{
Feature: database.Feature{
Namespace: database.Namespace{Name: "TestInsertLayerNamespace2"},
Expand Down
2 changes: 2 additions & 0 deletions database/pgsql/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ func init() {
FROM FeatureVersion fv
WHERE fv.id = ANY($3::integer[])`

queries["r_layer"] = `DELETE FROM Layer WHERE name = $1`

// lock.go
queries["i_lock"] = `INSERT INTO Lock(name, owner, until) VALUES($1, $2, $3)`

Expand Down

0 comments on commit 726bd3c

Please sign in to comment.