diff --git a/api/handlers.go b/api/handlers.go index bbba8bc8..b8848852 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -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 } @@ -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 diff --git a/api/router.go b/api/router.go index 41afae74..61f70d1f 100644 --- a/api/router.go +++ b/api/router.go @@ -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)) diff --git a/database/pgsql/layer.go b/database/pgsql/layer.go index 62b5968c..c98e657e 100644 --- a/database/pgsql/layer.go +++ b/database/pgsql/layer.go @@ -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 } diff --git a/database/pgsql/layer_test.go b/database/pgsql/layer_test.go index 816386e5..530c061f 100644 --- a/database/pgsql/layer_test.go +++ b/database/pgsql/layer_test.go @@ -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" ) @@ -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 @@ -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) { @@ -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"}, diff --git a/database/pgsql/queries.go b/database/pgsql/queries.go index 0da1e32c..ac727c99 100644 --- a/database/pgsql/queries.go +++ b/database/pgsql/queries.go @@ -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)`