database/api: add layer deletion support

This commit is contained in:
Quentin Machu 2016-01-20 13:57:51 -05:00 committed by Jimmy Zelinskie
parent 248fc7df72
commit 726bd3c0c6
5 changed files with 36 additions and 9 deletions

View File

@ -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 // DELETELayers deletes the specified layer and any child layers that are
// dependent on the specified layer. // dependent on the specified layer.
func DELETELayers(w http.ResponseWriter, r *http.Request, p httprouter.Params, e *Env) { 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) httputils.WriteHTTPError(w, 0, err)
return return
} }
@ -93,7 +93,7 @@ func GETLayers(w http.ResponseWriter, r *http.Request, p httprouter.Params, e *E
_, withFeatures := r.URL.Query()["withFeatures"] _, withFeatures := r.URL.Query()["withFeatures"]
_, withVulnerabilities := r.URL.Query()["withVulnerabilities"] _, 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 { if err != nil {
httputils.WriteHTTPError(w, 0, err) httputils.WriteHTTPError(w, 0, err)
return return

View File

@ -69,8 +69,8 @@ func NewRouterV1(to time.Duration, env *Env) *httprouter.Router {
// Layers // Layers
router.POST("/layers", wrap(POSTLayers, env)) router.POST("/layers", wrap(POSTLayers, env))
router.DELETE("/layers/:id", wrap(DELETELayers, env)) router.DELETE("/layers/:name", wrap(DELETELayers, env))
router.GET("/layers/:id", wrap(GETLayers, env)) router.GET("/layers/:name", wrap(GETLayers, env))
// Vulnerabilities // Vulnerabilities
// router.POST("/vulnerabilities", wrap(logic.POSTVulnerabilities)) // router.POST("/vulnerabilities", wrap(logic.POSTVulnerabilities))

View File

@ -350,6 +350,19 @@ func createNV(features []database.FeatureVersion) (map[string]*database.FeatureV
} }
func (pgSQL *pgSQL) DeleteLayer(name string) error { 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 return nil
} }

View File

@ -19,6 +19,7 @@ import (
"testing" "testing"
"github.com/coreos/clair/database" "github.com/coreos/clair/database"
cerrors "github.com/coreos/clair/utils/errors"
"github.com/coreos/clair/utils/types" "github.com/coreos/clair/utils/types"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -101,7 +102,7 @@ func TestFindLayer(t *testing.T) {
} }
func TestInsertLayer(t *testing.T) { func TestInsertLayer(t *testing.T) {
datastore, err := OpenForTest("InsertLayer", true) datastore, err := OpenForTest("InsertLayer", false)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
@ -118,7 +119,20 @@ func TestInsertLayer(t *testing.T) {
// TODO(Quentin-M) // TODO(Quentin-M)
// Delete layer. // 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) { func testInsertLayerInvalid(t *testing.T, datastore database.Datastore) {
@ -135,8 +149,6 @@ func testInsertLayerInvalid(t *testing.T, datastore database.Datastore) {
} }
func testInsertLayerTree(t *testing.T, datastore database.Datastore) { func testInsertLayerTree(t *testing.T, datastore database.Datastore) {
fmt.Println("- testInsertLayerTree")
f1 := database.FeatureVersion{ f1 := database.FeatureVersion{
Feature: database.Feature{ Feature: database.Feature{
Namespace: database.Namespace{Name: "TestInsertLayerNamespace2"}, Namespace: database.Namespace{Name: "TestInsertLayerNamespace2"},

View File

@ -127,6 +127,8 @@ func init() {
FROM FeatureVersion fv FROM FeatureVersion fv
WHERE fv.id = ANY($3::integer[])` WHERE fv.id = ANY($3::integer[])`
queries["r_layer"] = `DELETE FROM Layer WHERE name = $1`
// lock.go // lock.go
queries["i_lock"] = `INSERT INTO Lock(name, owner, until) VALUES($1, $2, $3)` queries["i_lock"] = `INSERT INTO Lock(name, owner, until) VALUES($1, $2, $3)`