diff --git a/cmd/event-received/handlers.go b/cmd/event-received/handlers.go index 18fa8c3c80..87e67876f8 100644 --- a/cmd/event-received/handlers.go +++ b/cmd/event-received/handlers.go @@ -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") diff --git a/cmd/event-received/handlers_test.go b/cmd/event-received/handlers_test.go index 3d575eb1f2..53264d7636 100644 --- a/cmd/event-received/handlers_test.go +++ b/cmd/event-received/handlers_test.go @@ -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"}}}, }}, } @@ -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"}}}, }}, } @@ -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{}}}, }}, } @@ -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"}}}, }}, } @@ -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"}}}, }}, } @@ -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"}}}, }}, } diff --git a/cmd/event-received/lpastore_event_handler.go b/cmd/event-received/lpastore_event_handler.go index d9ee0a9d48..3b2eb5b1db 100644 --- a/cmd/event-received/lpastore_event_handler.go +++ b/cmd/event-received/lpastore_event_handler.go @@ -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()) @@ -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) diff --git a/cmd/event-received/lpastore_event_handler_test.go b/cmd/event-received/lpastore_event_handler_test.go index c562fa2c56..f415323f95 100644 --- a/cmd/event-received/lpastore_event_handler_test.go +++ b/cmd/event-received/lpastore_event_handler_test.go @@ -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"}`), } @@ -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"}`), } @@ -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"}`), } @@ -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"}`), } diff --git a/cmd/event-received/main.go b/cmd/event-received/main.go index 4e0f5ea9bc..9d0f641ab6 100644 --- a/cmd/event-received/main.go +++ b/cmd/event-received/main.go @@ -5,6 +5,7 @@ import ( "cmp" "context" "encoding/json" + "errors" "fmt" "log" "log/slog" @@ -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 { @@ -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 { @@ -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) @@ -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 } diff --git a/cmd/event-received/main_test.go b/cmd/event-received/main_test.go index 9334155549..d87e59ce6c 100644 --- a/cmd/event-received/main_test.go +++ b/cmd/event-received/main_test.go @@ -3,11 +3,7 @@ package main import ( "context" "errors" - "testing" "time" - - "github.com/aws/aws-lambda-go/events" - "github.com/stretchr/testify/assert" ) var ( @@ -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()) -} diff --git a/cmd/event-received/makeregister_event_handler.go b/cmd/event-received/makeregister_event_handler.go index 23f8781967..1d7c0326ab 100644 --- a/cmd/event-received/makeregister_event_handler.go +++ b/cmd/event-received/makeregister_event_handler.go @@ -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() @@ -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) diff --git a/cmd/event-received/makeregister_event_handler_test.go b/cmd/event-received/makeregister_event_handler_test.go index 061c6e0a9b..965f820445 100644 --- a/cmd/event-received/makeregister_event_handler_test.go +++ b/cmd/event-received/makeregister_event_handler_test.go @@ -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"}}`, @@ -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"}}`, @@ -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"}}`, @@ -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"}}`), } @@ -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"}}`), } @@ -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"}}`), } @@ -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"}}`), } diff --git a/cmd/event-received/sirius_event_handler.go b/cmd/event-received/sirius_event_handler.go index 45b332121f..cba648616f 100644 --- a/cmd/event-received/sirius_event_handler.go +++ b/cmd/event-received/sirius_event_handler.go @@ -19,7 +19,7 @@ import ( type siriusEventHandler struct{} -func (h *siriusEventHandler) Handle(ctx context.Context, factory factory, cloudWatchEvent events.CloudWatchEvent) error { +func (h *siriusEventHandler) Handle(ctx context.Context, factory factory, cloudWatchEvent *events.CloudWatchEvent) error { switch cloudWatchEvent.DetailType { case "evidence-received": return handleEvidenceReceived(ctx, factory.DynamoClient(), cloudWatchEvent) @@ -74,7 +74,7 @@ func (h *siriusEventHandler) Handle(ctx context.Context, factory factory, cloudW } } -func handleEvidenceReceived(ctx context.Context, client dynamodbClient, event events.CloudWatchEvent) error { +func handleEvidenceReceived(ctx context.Context, client dynamodbClient, event *events.CloudWatchEvent) error { var v uidEvent if err := json.Unmarshal(event.Detail, &v); err != nil { return fmt.Errorf("failed to unmarshal detail: %w", err) @@ -99,7 +99,7 @@ func handleEvidenceReceived(ctx context.Context, client dynamodbClient, event ev func handleFeeApproved( ctx context.Context, client dynamodbClient, - e events.CloudWatchEvent, + e *events.CloudWatchEvent, shareCodeSender ShareCodeSender, lpaStoreClient LpaStoreClient, eventClient EventClient, @@ -149,7 +149,7 @@ func handleFeeApproved( return nil } -func handleFurtherInfoRequested(ctx context.Context, client dynamodbClient, event events.CloudWatchEvent, now func() time.Time) error { +func handleFurtherInfoRequested(ctx context.Context, client dynamodbClient, event *events.CloudWatchEvent, now func() time.Time) error { var v uidEvent if err := json.Unmarshal(event.Detail, &v); err != nil { return fmt.Errorf("failed to unmarshal detail: %w", err) @@ -173,7 +173,7 @@ func handleFurtherInfoRequested(ctx context.Context, client dynamodbClient, even return nil } -func handleFeeDenied(ctx context.Context, client dynamodbClient, event events.CloudWatchEvent, now func() time.Time) error { +func handleFeeDenied(ctx context.Context, client dynamodbClient, event *events.CloudWatchEvent, now func() time.Time) error { var v uidEvent if err := json.Unmarshal(event.Detail, &v); err != nil { return fmt.Errorf("failed to unmarshal detail: %w", err) @@ -198,7 +198,7 @@ func handleFeeDenied(ctx context.Context, client dynamodbClient, event events.Cl return nil } -func handleDonorSubmissionCompleted(ctx context.Context, client dynamodbClient, event events.CloudWatchEvent, shareCodeSender ShareCodeSender, appData appcontext.Data, lpaStoreClient LpaStoreClient, uuidString func() string, now func() time.Time) error { +func handleDonorSubmissionCompleted(ctx context.Context, client dynamodbClient, event *events.CloudWatchEvent, shareCodeSender ShareCodeSender, appData appcontext.Data, lpaStoreClient LpaStoreClient, uuidString func() string, now func() time.Time) error { var v uidEvent if err := json.Unmarshal(event.Detail, &v); err != nil { return fmt.Errorf("failed to unmarshal detail: %w", err) @@ -246,7 +246,7 @@ func handleDonorSubmissionCompleted(ctx context.Context, client dynamodbClient, return nil } -func handleCertificateProviderSubmissionCompleted(ctx context.Context, event events.CloudWatchEvent, factory factory) error { +func handleCertificateProviderSubmissionCompleted(ctx context.Context, event *events.CloudWatchEvent, factory factory) error { var v uidEvent if err := json.Unmarshal(event.Detail, &v); err != nil { return fmt.Errorf("failed to unmarshal detail: %w", err) diff --git a/cmd/event-received/sirius_event_handler_test.go b/cmd/event-received/sirius_event_handler_test.go index 756f45d5fa..7e59cd3d08 100644 --- a/cmd/event-received/sirius_event_handler_test.go +++ b/cmd/event-received/sirius_event_handler_test.go @@ -25,12 +25,12 @@ import ( func TestSiriusEventHandlerHandleUnknownEvent(t *testing.T) { handler := &siriusEventHandler{} - 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 sirius event"), err) } func TestHandleEvidenceReceived(t *testing.T) { - event := events.CloudWatchEvent{ + event := &events.CloudWatchEvent{ DetailType: "evidence-received", Detail: json.RawMessage(`{"uid":"M-1111-2222-3333"}`), } @@ -62,7 +62,7 @@ func TestHandleEvidenceReceived(t *testing.T) { } func TestHandleEvidenceReceivedWhenClientGetError(t *testing.T) { - event := events.CloudWatchEvent{ + event := &events.CloudWatchEvent{ DetailType: "evidence-required", Detail: json.RawMessage(`{"uid":"M-1111-2222-3333"}`), } @@ -77,7 +77,7 @@ func TestHandleEvidenceReceivedWhenClientGetError(t *testing.T) { } func TestHandleEvidenceReceivedWhenLpaMissingPK(t *testing.T) { - event := events.CloudWatchEvent{ + event := &events.CloudWatchEvent{ DetailType: "evidence-required", Detail: json.RawMessage(`{"uid":"M-1111-2222-3333"}`), } @@ -96,7 +96,7 @@ func TestHandleEvidenceReceivedWhenLpaMissingPK(t *testing.T) { } func TestHandleEvidenceReceivedWhenClientPutError(t *testing.T) { - event := events.CloudWatchEvent{ + event := &events.CloudWatchEvent{ DetailType: "evidence-required", Detail: json.RawMessage(`{"uid":"M-1111-2222-3333"}`), } @@ -121,7 +121,7 @@ func TestHandleEvidenceReceivedWhenClientPutError(t *testing.T) { } func TestHandleFeeApproved(t *testing.T) { - e := events.CloudWatchEvent{ + e := &events.CloudWatchEvent{ DetailType: "reduced-fee-approved", Detail: json.RawMessage(`{"uid":"M-1111-2222-3333"}`), } @@ -207,7 +207,7 @@ func TestHandleFeeApproved(t *testing.T) { } func TestHandleFeeApprovedWhenNotPaid(t *testing.T) { - event := events.CloudWatchEvent{ + event := &events.CloudWatchEvent{ DetailType: "reduced-fee-approved", Detail: json.RawMessage(`{"uid":"M-1111-2222-3333"}`), } @@ -276,7 +276,7 @@ func TestHandleFeeApprovedWhenNotPaid(t *testing.T) { } func TestHandleFeeApprovedWhenNotSigned(t *testing.T) { - event := events.CloudWatchEvent{ + event := &events.CloudWatchEvent{ DetailType: "reduced-fee-approved", Detail: json.RawMessage(`{"uid":"M-1111-2222-3333"}`), } @@ -325,7 +325,7 @@ func TestHandleFeeApprovedWhenAlreadyPaidOrApproved(t *testing.T) { for _, taskState := range testcases { t.Run(taskState.String(), func(t *testing.T) { - event := events.CloudWatchEvent{ + event := &events.CloudWatchEvent{ DetailType: "reduced-fee-approved", Detail: json.RawMessage(`{"uid":"M-1111-2222-3333"}`), } @@ -358,7 +358,7 @@ func TestHandleFeeApprovedWhenAlreadyPaidOrApproved(t *testing.T) { } func TestHandleFeeApprovedWhenDynamoClientPutError(t *testing.T) { - event := events.CloudWatchEvent{ + event := &events.CloudWatchEvent{ DetailType: "reduced-fee-approved", Detail: json.RawMessage(`{"uid":"M-1111-2222-3333"}`), } @@ -387,7 +387,7 @@ func TestHandleFeeApprovedWhenDynamoClientPutError(t *testing.T) { } func TestHandleFeeApprovedWhenShareCodeSenderError(t *testing.T) { - event := events.CloudWatchEvent{ + event := &events.CloudWatchEvent{ DetailType: "reduced-fee-approved", Detail: json.RawMessage(`{"uid":"M-1111-2222-3333"}`), } @@ -433,7 +433,7 @@ func TestHandleFeeApprovedWhenShareCodeSenderError(t *testing.T) { } func TestHandleFeeApprovedWhenEventClientError(t *testing.T) { - event := events.CloudWatchEvent{ + event := &events.CloudWatchEvent{ DetailType: "reduced-fee-approved", Detail: json.RawMessage(`{"uid":"M-1111-2222-3333"}`), } @@ -474,7 +474,7 @@ func TestHandleFeeApprovedWhenEventClientError(t *testing.T) { } func TestHandleFeeApprovedWhenLpaStoreError(t *testing.T) { - event := events.CloudWatchEvent{ + event := &events.CloudWatchEvent{ DetailType: "reduced-fee-approved", Detail: json.RawMessage(`{"uid":"M-1111-2222-3333"}`), } @@ -510,7 +510,7 @@ func TestHandleFeeApprovedWhenLpaStoreError(t *testing.T) { } func TestHandleFurtherInfoRequested(t *testing.T) { - event := events.CloudWatchEvent{ + event := &events.CloudWatchEvent{ DetailType: "further-info-requested", Detail: json.RawMessage(`{"uid":"M-1111-2222-3333"}`), } @@ -552,7 +552,7 @@ func TestHandleFurtherInfoRequested(t *testing.T) { } func TestHandleFurtherInfoRequestedWhenPaymentTaskIsAlreadyMoreEvidenceRequired(t *testing.T) { - event := events.CloudWatchEvent{ + event := &events.CloudWatchEvent{ DetailType: "further-info-requested", Detail: json.RawMessage(`{"uid":"M-1111-2222-3333"}`), } @@ -584,7 +584,7 @@ func TestHandleFurtherInfoRequestedWhenPaymentTaskIsAlreadyMoreEvidenceRequired( } func TestHandleFurtherInfoRequestedWhenPutError(t *testing.T) { - event := events.CloudWatchEvent{ + event := &events.CloudWatchEvent{ DetailType: "further-info-requested", Detail: json.RawMessage(`{"uid":"M-1111-2222-3333"}`), } @@ -616,7 +616,7 @@ func TestHandleFurtherInfoRequestedWhenPutError(t *testing.T) { } func TestHandleFeeDenied(t *testing.T) { - event := events.CloudWatchEvent{ + event := &events.CloudWatchEvent{ DetailType: "reduced-fee-declined", Detail: json.RawMessage(`{"uid":"M-1111-2222-3333"}`), } @@ -658,7 +658,7 @@ func TestHandleFeeDenied(t *testing.T) { } func TestHandleFeeDeniedWhenTaskAlreadyDenied(t *testing.T) { - event := events.CloudWatchEvent{ + event := &events.CloudWatchEvent{ DetailType: "reduced-fee-declined", Detail: json.RawMessage(`{"uid":"M-1111-2222-3333"}`), } @@ -684,7 +684,7 @@ func TestHandleFeeDeniedWhenTaskAlreadyDenied(t *testing.T) { } func TestHandleFeeDeniedWhenPutError(t *testing.T) { - event := events.CloudWatchEvent{ + event := &events.CloudWatchEvent{ DetailType: "reduced-fee-declined", Detail: json.RawMessage(`{"uid":"M-1111-2222-3333"}`), } @@ -712,7 +712,7 @@ func TestHandleFeeDeniedWhenPutError(t *testing.T) { assert.Equal(t, fmt.Errorf("failed to update LPA task status: %w", expectedError), err) } -var donorSubmissionCompletedEvent = events.CloudWatchEvent{ +var donorSubmissionCompletedEvent = &events.CloudWatchEvent{ DetailType: "donor-submission-completed", Detail: json.RawMessage(`{"uid":"M-1111-2222-3333"}`), } @@ -911,7 +911,7 @@ func TestHandleDonorSubmissionCompletedWhenShareCodeSenderError(t *testing.T) { assert.Equal(t, fmt.Errorf("failed to send share code to certificate provider: %w", expectedError), err) } -var certificateProviderSubmissionCompletedEvent = events.CloudWatchEvent{ +var certificateProviderSubmissionCompletedEvent = &events.CloudWatchEvent{ DetailType: "certificate-provider-submission-completed", Detail: json.RawMessage(`{"uid":"M-1111-2222-3333"}`), } diff --git a/terraform/environment/region/modules/event_received/main.tf b/terraform/environment/region/modules/event_received/main.tf index 2ec52daaae..3a713215f2 100644 --- a/terraform/environment/region/modules/event_received/main.tf +++ b/terraform/environment/region/modules/event_received/main.tf @@ -32,6 +32,36 @@ module "event_received" { } } +resource "aws_sqs_queue" "receive_events_queue" { + name = "${data.aws_default_tags.current.tags.environment-name}-receive-events-queue" + + redrive_policy = jsonencode({ + deadLetterTargetArn = aws_sqs_queue.receive_events_deadletter.arn + maxReceiveCount = 3 + }) +} + +resource "aws_sqs_queue" "receive_events_deadletter" { + name = "${data.aws_default_tags.current.tags.environment-name}-receive-events-deadletter" + # need to figure out best way to handle the retention policy here +} + +resource "aws_sqs_queue_redrive_allow_policy" "receive_events_redrive_allow_policy" { + queue_url = aws_sqs_queue.receive_events_deadletter.id + + redrive_allow_policy = jsonencode({ + redrivePermission = "byQueue", + sourceQueueArns = [aws_sqs_queue.receive_events_queue.arn] + }) +} + +resource "aws_lambda_event_source_mapping" "reveive_events_mapping" { + event_source_arn = aws_sqs_queue.receive_events_queue.arn + enabled = true + function_name = event_received.lambda.arn + batch_size = 1 +} + data "aws_iam_policy_document" "api_access_policy" { statement { sid = "allowApiAccess" @@ -66,7 +96,7 @@ resource "aws_cloudwatch_event_target" "receive_events_sirius" { target_id = "${data.aws_default_tags.current.tags.environment-name}-receive-events-sirius" event_bus_name = var.event_bus_name rule = aws_cloudwatch_event_rule.receive_events_sirius.name - arn = module.event_received.lambda.arn + arn = aws_sqs_queue.receive_events_queue.arn provider = aws.region dead_letter_config { arn = var.event_bus_dead_letter_queue.arn @@ -89,7 +119,7 @@ resource "aws_cloudwatch_event_target" "receive_events_lpa_store" { target_id = "${data.aws_default_tags.current.tags.environment-name}-receive-events-lpa-store" event_bus_name = var.event_bus_name rule = aws_cloudwatch_event_rule.receive_events_lpa_store.name - arn = module.event_received.lambda.arn + arn = aws_sqs_queue.receive_events_queue.arn dead_letter_config { arn = var.event_bus_dead_letter_queue.arn } @@ -112,7 +142,7 @@ resource "aws_cloudwatch_event_target" "receive_events_mlpa" { target_id = "${data.aws_default_tags.current.tags.environment-name}-receive-events-mlpa" event_bus_name = var.event_bus_name rule = aws_cloudwatch_event_rule.receive_events_mlpa.name - arn = module.event_received.lambda.arn + arn = aws_sqs_queue.receive_events_queue.arn dead_letter_config { arn = var.event_bus_dead_letter_queue.arn }