diff --git a/database/database.go b/database/database.go index 3f24591e..2001d1f0 100644 --- a/database/database.go +++ b/database/database.go @@ -37,6 +37,9 @@ var ( ) type Datastore interface { + // Namespace + ListNamespaces() ([]Namespace, error) + // Layer InsertLayer(Layer) error FindLayer(name string, withFeatures, withVulnerabilities bool) (Layer, error) diff --git a/database/pgsql/namespace.go b/database/pgsql/namespace.go index 48538fca..f755d3aa 100644 --- a/database/pgsql/namespace.go +++ b/database/pgsql/namespace.go @@ -49,3 +49,27 @@ func (pgSQL *pgSQL) insertNamespace(namespace database.Namespace) (int, error) { return id, nil } + +func (pgSQL *pgSQL) ListNamespaces() (namespaces []database.Namespace, err error) { + rows, err := pgSQL.Query(getQuery("l_namespace")) + if err != nil { + return namespaces, handleError("l_namespace", err) + } + defer rows.Close() + + for rows.Next() { + var namespace database.Namespace + + err = rows.Scan(&namespace.ID, &namespace.Name) + if err != nil { + return namespaces, handleError("l_namespace.Scan()", err) + } + + namespaces = append(namespaces, namespace) + } + if err = rows.Err(); err != nil { + return namespaces, handleError("l_namespace.Rows()", err) + } + + return namespaces, err +} diff --git a/database/pgsql/namespace_test.go b/database/pgsql/namespace_test.go index e689ca93..c5da400b 100644 --- a/database/pgsql/namespace_test.go +++ b/database/pgsql/namespace_test.go @@ -16,7 +16,8 @@ package pgsql import ( "testing" - + "fmt" + "github.com/coreos/clair/database" "github.com/stretchr/testify/assert" ) @@ -41,3 +42,25 @@ func TestInsertNamespace(t *testing.T) { assert.Nil(t, err) assert.Equal(t, id1, id2) } + +func TestListNamespace(t *testing.T) { + datastore, err := OpenForTest("ListNamespaces", true) + if err != nil { + t.Error(err) + return + } + defer datastore.Close() + + namespaces, err := datastore.ListNamespaces() + assert.Nil(t, err) + if assert.Len(t, namespaces, 2) { + for _, namespace := range namespaces { + switch namespace.Name { + case "debian:7", "debian:8": + continue + default: + assert.Error(t, fmt.Errorf("ListNamespaces should not have returned '%s'", namespace.Name)) + } + } + } +} diff --git a/database/pgsql/queries.go b/database/pgsql/queries.go index 00aab6dc..e255950d 100644 --- a/database/pgsql/queries.go +++ b/database/pgsql/queries.go @@ -43,6 +43,8 @@ func init() { UNION SELECT id FROM new_namespace` + queries["l_namespace"] = `SELECT id, name FROM Namespace` + // feature.go queries["soi_feature"] = ` WITH new_feature AS (