Skip to content

Commit

Permalink
Merge dc736da into d82c2bb
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx authored Oct 22, 2024
2 parents d82c2bb + dc736da commit 1c7d780
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 107 deletions.
2 changes: 1 addition & 1 deletion cmd/event-received/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
)

func handleObjectTagsAdded(ctx context.Context, dynamodbClient dynamodbClient, event events.S3Event, s3Client s3Client, documentStore DocumentStore) error {
func handleObjectTagsAdded(ctx context.Context, dynamodbClient dynamodbClient, event *events.S3Event, s3Client s3Client, documentStore DocumentStore) error {
objectKey := event.Records[0].S3.Object.Key
if objectKey == "" {
return fmt.Errorf("object key missing")
Expand Down
12 changes: 6 additions & 6 deletions cmd/event-received/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestHandleObjectTagsAdded(t *testing.T) {
for scanResult, hasVirus := range testCases {
t.Run(scanResult, func(t *testing.T) {
event := Event{
S3Event: events.S3Event{Records: []events.S3EventRecord{
S3Event: &events.S3Event{Records: []events.S3EventRecord{
{S3: events.S3Entity{Object: events.S3Object{Key: "M-1111-2222-3333/evidence/a-uid"}}},
}},
}
Expand Down Expand Up @@ -67,7 +67,7 @@ func TestHandleObjectTagsAdded(t *testing.T) {

func TestHandleObjectTagsAddedWhenScannedTagMissing(t *testing.T) {
event := Event{
S3Event: events.S3Event{Records: []events.S3EventRecord{
S3Event: &events.S3Event{Records: []events.S3EventRecord{
{S3: events.S3Entity{Object: events.S3Object{Key: "M-1111-2222-3333/evidence/a-uid"}}},
}},
}
Expand All @@ -85,7 +85,7 @@ func TestHandleObjectTagsAddedWhenScannedTagMissing(t *testing.T) {

func TestHandleObjectTagsAddedWhenObjectKeyMissing(t *testing.T) {
event := Event{
S3Event: events.S3Event{Records: []events.S3EventRecord{
S3Event: &events.S3Event{Records: []events.S3EventRecord{
{S3: events.S3Entity{Object: events.S3Object{}}},
}},
}
Expand All @@ -96,7 +96,7 @@ func TestHandleObjectTagsAddedWhenObjectKeyMissing(t *testing.T) {

func TestHandleObjectTagsAddedWhenS3ClientGetObjectTagsError(t *testing.T) {
event := Event{
S3Event: events.S3Event{Records: []events.S3EventRecord{
S3Event: &events.S3Event{Records: []events.S3EventRecord{
{S3: events.S3Entity{Object: events.S3Object{Key: "M-1111-2222-3333/evidence/a-uid"}}},
}},
}
Expand All @@ -112,7 +112,7 @@ func TestHandleObjectTagsAddedWhenS3ClientGetObjectTagsError(t *testing.T) {

func TestHandleObjectTagsAddedWhenDynamoClientOneByUIDError(t *testing.T) {
event := Event{
S3Event: events.S3Event{Records: []events.S3EventRecord{
S3Event: &events.S3Event{Records: []events.S3EventRecord{
{S3: events.S3Entity{Object: events.S3Object{Key: "M-1111-2222-3333/evidence/a-uid"}}},
}},
}
Expand Down Expand Up @@ -146,7 +146,7 @@ func TestHandleObjectTagsAddedWhenDynamoClientOneByUIDError(t *testing.T) {

func TestHandleObjectTagsAddedWhenDocumentStoreUpdateScanResultsError(t *testing.T) {
event := Event{
S3Event: events.S3Event{Records: []events.S3EventRecord{
S3Event: &events.S3Event{Records: []events.S3EventRecord{
{S3: events.S3Entity{Object: events.S3Object{Key: "M-1111-2222-3333/evidence/a-uid"}}},
}},
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/event-received/lpastore_event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

type lpastoreEventHandler struct{}

func (h *lpastoreEventHandler) Handle(ctx context.Context, factory factory, cloudWatchEvent events.CloudWatchEvent) error {
func (h *lpastoreEventHandler) Handle(ctx context.Context, factory factory, cloudWatchEvent *events.CloudWatchEvent) error {
switch cloudWatchEvent.DetailType {
case "lpa-updated":
return handleLpaUpdated(ctx, factory.DynamoClient(), cloudWatchEvent, factory.Now())
Expand All @@ -26,7 +26,7 @@ type lpaUpdatedEvent struct {
ChangeType string `json:"changeType"`
}

func handleLpaUpdated(ctx context.Context, client dynamodbClient, event events.CloudWatchEvent, now func() time.Time) error {
func handleLpaUpdated(ctx context.Context, client dynamodbClient, event *events.CloudWatchEvent, now func() time.Time) error {
var v lpaUpdatedEvent
if err := json.Unmarshal(event.Detail, &v); err != nil {
return fmt.Errorf("failed to unmarshal detail: %w", err)
Expand Down
10 changes: 5 additions & 5 deletions cmd/event-received/lpastore_event_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (
func TestLpaStoreEventHandlerHandleUnknownEvent(t *testing.T) {
handler := &lpastoreEventHandler{}

err := handler.Handle(ctx, nil, events.CloudWatchEvent{DetailType: "some-event"})
err := handler.Handle(ctx, nil, &events.CloudWatchEvent{DetailType: "some-event"})
assert.Equal(t, fmt.Errorf("unknown lpastore event"), err)
}

func TestLpaStoreEventHandlerHandleLpaUpdated(t *testing.T) {
event := events.CloudWatchEvent{
event := &events.CloudWatchEvent{
DetailType: "lpa-updated",
Detail: json.RawMessage(`{"uid":"M-1111-2222-3333","changeType":"PERFECT"}`),
}
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestLpaStoreEventHandlerHandleLpaUpdated(t *testing.T) {
}

func TestLpaStoreEventHandlerHandleLpaUpdatedWhenChangeTypeNotPerfect(t *testing.T) {
event := events.CloudWatchEvent{
event := &events.CloudWatchEvent{
DetailType: "lpa-updated",
Detail: json.RawMessage(`{"uid":"M-1111-2222-3333","changeType":"WHAT"}`),
}
Expand All @@ -80,7 +80,7 @@ func TestLpaStoreEventHandlerHandleLpaUpdatedWhenChangeTypeNotPerfect(t *testing
}

func TestLpaStoreEventHandlerHandleLpaUpdatedWhenDynamoGetErrors(t *testing.T) {
event := events.CloudWatchEvent{
event := &events.CloudWatchEvent{
DetailType: "lpa-updated",
Detail: json.RawMessage(`{"uid":"M-1111-2222-3333","changeType":"PERFECT"}`),
}
Expand Down Expand Up @@ -109,7 +109,7 @@ func TestLpaStoreEventHandlerHandleLpaUpdatedWhenDynamoGetErrors(t *testing.T) {
}

func TestLpaStoreEventHandlerHandleLpaUpdatedWhenDynamoPutErrors(t *testing.T) {
event := events.CloudWatchEvent{
event := &events.CloudWatchEvent{
DetailType: "lpa-updated",
Detail: json.RawMessage(`{"uid":"M-1111-2222-3333","changeType":"PERFECT"}`),
}
Expand Down
106 changes: 68 additions & 38 deletions cmd/event-received/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"cmp"
"context"
"encoding/json"
"errors"
"fmt"
"log"
"log/slog"
Expand Down Expand Up @@ -42,7 +43,7 @@ type factory interface {
}

type Handler interface {
Handle(context.Context, factory, events.CloudWatchEvent) error
Handle(context.Context, factory, *events.CloudWatchEvent) error
}

type uidEvent struct {
Expand Down Expand Up @@ -73,12 +74,31 @@ type EventClient interface {
}

type Event struct {
events.S3Event
events.CloudWatchEvent
CloudWatchEvent *events.CloudWatchEvent
S3Event *events.S3Event
SQSEvent *events.SQSEvent
}

func (e Event) isS3Event() bool {
return len(e.Records) > 0
func (e *Event) UnmarshalJSON(data []byte) error {
var cloud events.CloudWatchEvent
if err := json.Unmarshal(data, &cloud); err == nil {
e.CloudWatchEvent = &cloud
return nil
}

var s3 events.S3Event
if err := json.Unmarshal(data, &s3); err == nil {
e.S3Event = &s3
return nil
}

var sqs events.SQSEvent
if err := json.Unmarshal(data, &sqs); err == nil {
e.SQSEvent = &sqs
return nil
}

return errors.New("unknown event type")
}

func handler(ctx context.Context, event Event) error {
Expand Down Expand Up @@ -117,7 +137,7 @@ func handler(ctx context.Context, event Event) error {
return fmt.Errorf("failed to create dynamodb client: %w", err)
}

if event.isS3Event() {
if event.S3Event != nil {
s3Client := s3.NewClient(cfg, evidenceBucketName)
documentStore := document.NewStore(dynamoClient, nil, nil)

Expand All @@ -128,44 +148,54 @@ func handler(ctx context.Context, event Event) error {
return nil
}

factory := &Factory{
logger: logger,
now: time.Now,
uuidString: random.UuidString,
cfg: cfg,
dynamoClient: dynamoClient,
appPublicURL: appPublicURL,
lpaStoreBaseURL: lpaStoreBaseURL,
lpaStoreSecretARN: lpaStoreSecretARN,
uidBaseURL: uidBaseURL,
notifyBaseURL: notifyBaseURL,
notifyIsProduction: notifyIsProduction,
eventBusName: eventBusName,
searchEndpoint: searchEndpoint,
searchIndexName: searchIndexName,
searchIndexingEnabled: searchIndexingEnabled,
}
if event.SQSEvent != nil {
for _, record := range event.SQSEvent.Records {
logger.InfoContext(ctx, "record", slog.Any("all", record))
}

var handler Handler
switch event.Source {
case "opg.poas.sirius":
handler = &siriusEventHandler{}
case "opg.poas.makeregister":
handler = &makeregisterEventHandler{}
case "opg.poas.lpastore":
handler = &lpastoreEventHandler{}
return nil
}

if handler == nil {
eJson, _ := json.Marshal(event)
return fmt.Errorf("unknown event received: %s", string(eJson))
}
if event.CloudWatchEvent != nil {
factory := &Factory{
logger: logger,
now: time.Now,
uuidString: random.UuidString,
cfg: cfg,
dynamoClient: dynamoClient,
appPublicURL: appPublicURL,
lpaStoreBaseURL: lpaStoreBaseURL,
lpaStoreSecretARN: lpaStoreSecretARN,
uidBaseURL: uidBaseURL,
notifyBaseURL: notifyBaseURL,
notifyIsProduction: notifyIsProduction,
eventBusName: eventBusName,
searchEndpoint: searchEndpoint,
searchIndexName: searchIndexName,
searchIndexingEnabled: searchIndexingEnabled,
}

if err := handler.Handle(ctx, factory, event.CloudWatchEvent); err != nil {
return fmt.Errorf("%s: %w", event.DetailType, err)
var handler Handler
switch event.CloudWatchEvent.Source {
case "opg.poas.sirius":
handler = &siriusEventHandler{}
case "opg.poas.makeregister":
handler = &makeregisterEventHandler{}
case "opg.poas.lpastore":
handler = &lpastoreEventHandler{}
}

if handler == nil {
eJson, _ := json.Marshal(event)
return fmt.Errorf("unknown event received: %s", string(eJson))
}

if err := handler.Handle(ctx, factory, event.CloudWatchEvent); err != nil {
return fmt.Errorf("%s: %w", event.CloudWatchEvent.DetailType, err)
}
log.Println("successfully handled ", event.CloudWatchEvent.DetailType)
}

log.Println("successfully handled ", event.DetailType)
return nil
}

Expand Down
14 changes: 0 additions & 14 deletions cmd/event-received/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ package main
import (
"context"
"errors"
"testing"
"time"

"github.com/aws/aws-lambda-go/events"
"github.com/stretchr/testify/assert"
)

var (
Expand All @@ -20,13 +16,3 @@ var (
testUuidString = "a-uuid"
testUuidStringFn = func() string { return testUuidString }
)

func TestIsS3Event(t *testing.T) {
s3Event := Event{S3Event: events.S3Event{Records: []events.S3EventRecord{{}, {}}}}

assert.True(t, s3Event.isS3Event())

s3Event.Records = []events.S3EventRecord{}

assert.False(t, s3Event.isS3Event())
}
4 changes: 2 additions & 2 deletions cmd/event-received/makeregister_event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

type makeregisterEventHandler struct{}

func (h *makeregisterEventHandler) Handle(ctx context.Context, factory factory, cloudWatchEvent events.CloudWatchEvent) error {
func (h *makeregisterEventHandler) Handle(ctx context.Context, factory factory, cloudWatchEvent *events.CloudWatchEvent) error {
switch cloudWatchEvent.DetailType {
case "uid-requested":
uidStore, err := factory.UidStore()
Expand All @@ -33,7 +33,7 @@ func (h *makeregisterEventHandler) Handle(ctx context.Context, factory factory,
}
}

func handleUidRequested(ctx context.Context, uidStore UidStore, uidClient UidClient, e events.CloudWatchEvent, dynamoClient dynamodbClient, eventClient EventClient) error {
func handleUidRequested(ctx context.Context, uidStore UidStore, uidClient UidClient, e *events.CloudWatchEvent, dynamoClient dynamodbClient, eventClient EventClient) error {
var v event.UidRequested
if err := json.Unmarshal(e.Detail, &v); err != nil {
return fmt.Errorf("failed to unmarshal detail: %w", err)
Expand Down
16 changes: 8 additions & 8 deletions cmd/event-received/makeregister_event_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import (
func TestMakeRegisterHandlerHandleUnknownEvent(t *testing.T) {
handler := &makeregisterEventHandler{}

err := handler.Handle(ctx, nil, events.CloudWatchEvent{DetailType: "some-event"})
err := handler.Handle(ctx, nil, &events.CloudWatchEvent{DetailType: "some-event"})
assert.Equal(t, fmt.Errorf("unknown makeregister event"), err)
}

func TestHandleUidRequestedDonor(t *testing.T) {
e := events.CloudWatchEvent{
e := &events.CloudWatchEvent{
DetailType: "uid-requested",
Detail: json.RawMessage(
`{"lpaID":"lpa-id","donorSessionID":"donor-session-id","organisationID":"","type":"personal-welfare","donor":{"name":"a donor","dob":"2000-01-02","postcode":"F1 1FF"}}`,
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestHandleUidRequestedDonor(t *testing.T) {
}

func TestHandleUidRequestedOrganisation(t *testing.T) {
e := events.CloudWatchEvent{
e := &events.CloudWatchEvent{
DetailType: "uid-requested",
Detail: json.RawMessage(
`{"lpaID":"lpa-id","donorSessionID":"","organisationID":"organisation-id","type":"personal-welfare","donor":{"name":"a donor","dob":"2000-01-02","postcode":"F1 1FF"}}`,
Expand Down Expand Up @@ -166,7 +166,7 @@ func TestHandleUidRequestedOrganisation(t *testing.T) {
}

func TestHandleUidRequestedWhenLpaUIDExists(t *testing.T) {
e := events.CloudWatchEvent{
e := &events.CloudWatchEvent{
DetailType: "uid-requested",
Detail: json.RawMessage(
`{"lpaID":"lpa-id","donorSessionID":"","organisationID":"organisation-id","type":"personal-welfare","donor":{"name":"a donor","dob":"2000-01-02","postcode":"F1 1FF"}}`,
Expand Down Expand Up @@ -197,7 +197,7 @@ func TestHandleUidRequestedWhenLpaUIDExists(t *testing.T) {
}

func TestHandleUidRequestedWhenDynamoClientError(t *testing.T) {
e := events.CloudWatchEvent{
e := &events.CloudWatchEvent{
DetailType: "uid-requested",
Detail: json.RawMessage(`{"lpaID":"an-id","donorSessionID":"donor-id","type":"personal-welfare","donor":{"name":"a donor","dob":"2000-01-02","postcode":"F1 1FF"}}`),
}
Expand All @@ -212,7 +212,7 @@ func TestHandleUidRequestedWhenDynamoClientError(t *testing.T) {
}

func TestHandleUidRequestedWhenUidClientErrors(t *testing.T) {
e := events.CloudWatchEvent{
e := &events.CloudWatchEvent{
DetailType: "uid-requested",
Detail: json.RawMessage(`{"lpaID":"an-id","donorSessionID":"donor-id","type":"personal-welfare","donor":{"name":"a donor","dob":"2000-01-02","postcode":"F1 1FF"}}`),
}
Expand All @@ -232,7 +232,7 @@ func TestHandleUidRequestedWhenUidClientErrors(t *testing.T) {
}

func TestHandleUidRequestedWhenUidStoreErrors(t *testing.T) {
e := events.CloudWatchEvent{
e := &events.CloudWatchEvent{
DetailType: "uid-requested",
Detail: json.RawMessage(`{"lpaID":"an-id","donorSessionID":"donor-id","type":"personal-welfare","donor":{"name":"a donor","dob":"2000-01-02","postcode":"F1 1FF"}}`),
}
Expand All @@ -257,7 +257,7 @@ func TestHandleUidRequestedWhenUidStoreErrors(t *testing.T) {
}

func TestHandleUidRequestedWhenEventClientErrors(t *testing.T) {
e := events.CloudWatchEvent{
e := &events.CloudWatchEvent{
DetailType: "uid-requested",
Detail: json.RawMessage(`{"lpaID":"an-id","donorSessionID":"donor-id","type":"personal-welfare","donor":{"name":"a donor","dob":"2000-01-02","postcode":"F1 1FF"}}`),
}
Expand Down
Loading

0 comments on commit 1c7d780

Please sign in to comment.