feat(eda): name and delete capture sources; an armed binding blocks the delete
This commit is contained in:
22 files changed
+1103
-592
No files matched your search
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/eventtype"
|
||||
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
||||
"github.com/samcharles93/archie-core/internal/domain/source"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
@@ -148,4 +149,24 @@ func TestBindingLifecycle(t *testing.T) {
|
||||
if err := eda.PauseBinding(ctx, "missing"); !errors.Is(err, storecontract.ErrBindingNotFound) {
|
||||
t.Fatalf("pause of a missing binding = %v, want ErrBindingNotFound", err)
|
||||
}
|
||||
|
||||
// A source is not deleted while an armed binding fires on it.
|
||||
if err := eda.InsertSource(ctx, source.Source{Path: "forge", Signing: source.SigningSigned}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := eda.ApproveBinding(ctx, id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := eda.DeleteSource(ctx, "forge"); !errors.Is(err, storecontract.ErrSourceInUse) {
|
||||
t.Fatalf("delete of a source an armed binding fires on = %v, want ErrSourceInUse", err)
|
||||
}
|
||||
if err := eda.PauseBinding(ctx, id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := eda.DeleteSource(ctx, "forge"); err != nil {
|
||||
t.Fatalf("delete of a source with no armed binding = %v", err)
|
||||
}
|
||||
if err := eda.DeleteSource(ctx, "forge"); !errors.Is(err, storecontract.ErrSourceNotFound) {
|
||||
t.Fatalf("second delete = %v, want ErrSourceNotFound", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
-- sources.name is the operator's label for a source; its path stays the
|
||||
-- identity captures and bindings reference.
|
||||
|
||||
-- +goose Up
|
||||
|
||||
ALTER TABLE sources ADD COLUMN name text NOT NULL DEFAULT '';
|
||||
|
||||
-- +goose Down
|
||||
|
||||
ALTER TABLE sources DROP COLUMN name;
|
||||
@@ -189,6 +189,21 @@ func (q *Queries) DeletePlaybookDispatches(ctx context.Context, playbookID strin
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteUnboundSource = `-- name: DeleteUnboundSource :execrows
|
||||
DELETE FROM sources s WHERE s.path = $1
|
||||
AND NOT EXISTS (SELECT 1 FROM bindings b WHERE b.source = s.path AND b.status = 'armed')
|
||||
`
|
||||
|
||||
// A source an armed binding fires on is not deleted: the binding would
|
||||
// silently stop receiving events.
|
||||
func (q *Queries) DeleteUnboundSource(ctx context.Context, path string) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, deleteUnboundSource, path)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const deriveSources = `-- name: DeriveSources :exec
|
||||
SELECT derive_sources()
|
||||
`
|
||||
@@ -342,7 +357,7 @@ func (q *Queries) GetMapping(ctx context.Context, id string) (GetMappingRow, err
|
||||
}
|
||||
|
||||
const getSource = `-- name: GetSource :one
|
||||
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id FROM sources WHERE path = $1
|
||||
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name FROM sources WHERE path = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetSource(ctx context.Context, path string) (Source, error) {
|
||||
@@ -356,6 +371,7 @@ func (q *Queries) GetSource(ctx context.Context, path string) (Source, error) {
|
||||
&i.UpdatedAt,
|
||||
&i.OrgID,
|
||||
&i.WorkspaceID,
|
||||
&i.Name,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -837,7 +853,7 @@ func (q *Queries) ListMappings(ctx context.Context) ([]ListMappingsRow, error) {
|
||||
}
|
||||
|
||||
const listSources = `-- name: ListSources :many
|
||||
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id FROM sources ORDER BY created_at DESC, path
|
||||
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name FROM sources ORDER BY created_at DESC, path
|
||||
`
|
||||
|
||||
func (q *Queries) ListSources(ctx context.Context) ([]Source, error) {
|
||||
@@ -857,6 +873,7 @@ func (q *Queries) ListSources(ctx context.Context) ([]Source, error) {
|
||||
&i.UpdatedAt,
|
||||
&i.OrgID,
|
||||
&i.WorkspaceID,
|
||||
&i.Name,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -958,6 +975,23 @@ func (q *Queries) SetBindingDispatchTask(ctx context.Context, arg SetBindingDisp
|
||||
return err
|
||||
}
|
||||
|
||||
const setSourceName = `-- name: SetSourceName :execrows
|
||||
UPDATE sources SET name = $2, updated_at = now() WHERE path = $1
|
||||
`
|
||||
|
||||
type SetSourceNameParams struct {
|
||||
Path string
|
||||
Name string
|
||||
}
|
||||
|
||||
func (q *Queries) SetSourceName(ctx context.Context, arg SetSourceNameParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, setSourceName, arg.Path, arg.Name)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const setSourceSecret = `-- name: SetSourceSecret :execrows
|
||||
UPDATE sources SET secret = $2, updated_at = now() WHERE path = $1
|
||||
`
|
||||
|
||||
@@ -344,6 +344,7 @@ type Source struct {
|
||||
UpdatedAt time.Time
|
||||
OrgID string
|
||||
WorkspaceID string
|
||||
Name string
|
||||
}
|
||||
|
||||
type StepExecution struct {
|
||||
|
||||
@@ -152,10 +152,10 @@ SELECT pg_advisory_xact_lock(hashtext('event_types:' || sqlc.arg(source)::text))
|
||||
INSERT INTO sources (path, signing, secret) VALUES ($1, $2, $3);
|
||||
|
||||
-- name: GetSource :one
|
||||
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id FROM sources WHERE path = $1;
|
||||
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name FROM sources WHERE path = $1;
|
||||
|
||||
-- name: ListSources :many
|
||||
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id FROM sources ORDER BY created_at DESC, path;
|
||||
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name FROM sources ORDER BY created_at DESC, path;
|
||||
|
||||
-- name: SetSourceSigning :execrows
|
||||
UPDATE sources SET signing = sqlc.arg(to_signing), updated_at = now()
|
||||
@@ -164,6 +164,15 @@ WHERE path = sqlc.arg(path) AND signing = sqlc.arg(from_signing);
|
||||
-- name: SetSourceSecret :execrows
|
||||
UPDATE sources SET secret = $2, updated_at = now() WHERE path = $1;
|
||||
|
||||
-- name: SetSourceName :execrows
|
||||
UPDATE sources SET name = $2, updated_at = now() WHERE path = $1;
|
||||
|
||||
-- name: DeleteUnboundSource :execrows
|
||||
-- A source an armed binding fires on is not deleted: the binding would
|
||||
-- silently stop receiving events.
|
||||
DELETE FROM sources s WHERE s.path = $1
|
||||
AND NOT EXISTS (SELECT 1 FROM bindings b WHERE b.source = s.path AND b.status = 'armed');
|
||||
|
||||
-- name: SourceExists :one
|
||||
SELECT EXISTS (SELECT 1 FROM sources WHERE path = $1);
|
||||
|
||||
|
||||
@@ -102,6 +102,37 @@ func (s *EDA) SetSourceSecret(ctx context.Context, path, secret string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetSourceName relabels a source.
|
||||
func (s *EDA) SetSourceName(ctx context.Context, path, name string) error {
|
||||
n, err := s.q.SetSourceName(ctx, postgresdb.SetSourceNameParams{Path: path, Name: name})
|
||||
if err != nil {
|
||||
return fmt.Errorf("edastore: set source name: %w", err)
|
||||
}
|
||||
if n == 0 {
|
||||
return storecontract.ErrSourceNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteSource deletes a source no armed binding fires on.
|
||||
func (s *EDA) DeleteSource(ctx context.Context, path string) error {
|
||||
n, err := s.q.DeleteUnboundSource(ctx, path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("edastore: delete source: %w", err)
|
||||
}
|
||||
if n > 0 {
|
||||
return nil
|
||||
}
|
||||
exists, err := s.q.SourceExists(ctx, path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("edastore: delete source: %w", err)
|
||||
}
|
||||
if exists {
|
||||
return storecontract.ErrSourceInUse
|
||||
}
|
||||
return storecontract.ErrSourceNotFound
|
||||
}
|
||||
|
||||
// sealSecret encrypts a non-empty secret when a cipher is configured.
|
||||
func (s *EDA) sealSecret(secret string) (string, error) {
|
||||
if s.cipher == nil || secret == "" {
|
||||
@@ -124,7 +155,7 @@ func (s *EDA) sourceValue(r postgresdb.Source) (source.Source, error) {
|
||||
secret = plain
|
||||
}
|
||||
return source.Source{
|
||||
Path: r.Path, Signing: source.Signing(r.Signing), Secret: secret,
|
||||
Path: r.Path, Name: r.Name, Signing: source.Signing(r.Signing), Secret: secret,
|
||||
CreatedAt: r.CreatedAt, UpdatedAt: r.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
@@ -659,6 +659,16 @@ func (c *Client) SetSourceSigning(ctx context.Context, path string, from, to sou
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) SetSourceName(ctx context.Context, path, name string) error {
|
||||
_, err := c.client.SetSourceName(ctx, &pb.SetSourceNameRequest{Path: path, Name: name})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) DeleteSource(ctx context.Context, path string) error {
|
||||
_, err := c.client.DeleteSource(ctx, &pb.DeleteSourceRequest{Path: path})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) SetSourceSecret(ctx context.Context, path, secret string) error {
|
||||
_, err := c.client.SetSourceSecret(ctx, &pb.SetSourceSecretRequest{Path: path, Secret: secret})
|
||||
return unmapError(err)
|
||||
|
||||
@@ -954,6 +954,28 @@ func (s *server) SetSourceSecret(ctx context.Context, r *pb.SetSourceSecretReque
|
||||
return &pb.SetSourceSecretResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *server) SetSourceName(ctx context.Context, r *pb.SetSourceNameRequest) (*pb.SetSourceNameResponse, error) {
|
||||
ss, err := s.sources()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ss.SetSourceName(ctx, r.Path, r.Name); err != nil {
|
||||
return nil, s.logErr("SetSourceName", err)
|
||||
}
|
||||
return &pb.SetSourceNameResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *server) DeleteSource(ctx context.Context, r *pb.DeleteSourceRequest) (*pb.DeleteSourceResponse, error) {
|
||||
ss, err := s.sources()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ss.DeleteSource(ctx, r.Path); err != nil {
|
||||
return nil, s.logErr("DeleteSource", err)
|
||||
}
|
||||
return &pb.DeleteSourceResponse{}, nil
|
||||
}
|
||||
|
||||
// Dispatch
|
||||
|
||||
func (s *server) ArmedBindingsForSource(ctx context.Context, r *pb.ArmedBindingsForSourceRequest) (*pb.ArmedBindingsForSourceResponse, error) {
|
||||
|
||||
@@ -211,7 +211,7 @@ func capturedEventValue(c *pb.CapturedEvent) storecontract.CapturedEvent {
|
||||
|
||||
func sourceProto(s source.Source) *pb.Source {
|
||||
return &pb.Source{
|
||||
Path: s.Path, Signing: string(s.Signing), Secret: s.Secret,
|
||||
Path: s.Path, Name: s.Name, Signing: string(s.Signing), Secret: s.Secret,
|
||||
CreatedAt: timestamp(s.CreatedAt), UpdatedAt: timestamp(s.UpdatedAt),
|
||||
}
|
||||
}
|
||||
@@ -221,7 +221,7 @@ func sourceValue(s *pb.Source) source.Source {
|
||||
return source.Source{}
|
||||
}
|
||||
return source.Source{
|
||||
Path: s.Path, Signing: source.Signing(s.Signing), Secret: s.Secret,
|
||||
Path: s.Path, Name: s.Name, Signing: source.Signing(s.Signing), Secret: s.Secret,
|
||||
CreatedAt: timeValue(s.CreatedAt), UpdatedAt: timeValue(s.UpdatedAt),
|
||||
}
|
||||
}
|
||||
@@ -496,6 +496,7 @@ var wireErrors = []struct {
|
||||
{storecontract.ErrMappingNotFound, codes.NotFound, msgMappingNotFound},
|
||||
{storecontract.ErrEventTypeNotFound, codes.NotFound, msgEventTypeNotFound},
|
||||
{storecontract.ErrSourceNotFound, codes.NotFound, msgSourceNotFound},
|
||||
{storecontract.ErrSourceInUse, codes.FailedPrecondition, storecontract.ErrSourceInUse.Error()},
|
||||
{storecontract.ErrAlreadyDispatched, codes.AlreadyExists, msgAlreadyDispatched},
|
||||
{storecontract.ErrCallNotYours, codes.PermissionDenied, storecontract.ErrCallNotYours.Error()},
|
||||
{storecontract.ErrCallCallerNotRunning, codes.FailedPrecondition, storecontract.ErrCallCallerNotRunning.Error()},
|
||||
|
||||
Reference in new issue
Block a user