-
Notifications
You must be signed in to change notification settings - Fork 421
feat(parser): add support for SQS-wrapped S3 event notifications #2108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(parser): add support for SQS-wrapped S3 event notifications #2108
Conversation
Signed-off-by: Alan Ip <[email protected]>
Signed-off-by: Alan Ip <[email protected]>
Signed-off-by: Alan Ip <[email protected]>
Signed-off-by: Alan Ip <[email protected]>
Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need. |
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## develop #2108 +/- ##
========================================
Coverage 97.47% 97.47%
========================================
Files 148 149 +1
Lines 6882 6891 +9
Branches 505 505
========================================
+ Hits 6708 6717 +9
Misses 137 137
Partials 37 37
☔ View full report in Codecov by Sentry. |
The latter represents an event payload, i.e. a list of records.
Since the SqsS3EventNotificationModel is only responsible for chaining (rather than redefining) the existing SqsModel and S3Model models, let's re-use both the existing stubs and their corresponding assertions.
@leandrodamascena Thanks for your guidance. Do these latest changes look roughly sane to you? 🙂 I've cheekily borrowed the |
Signed-off-by: Alan Ip <[email protected]>
Hi @theipster! Surely these things make sense. I didn't get a chance to see why the CI is crashing, it looks like I will look into this today and we can work together to complete it and release in the next Powertools version. So far, very good job 👏! |
Hi @theipster! I'm trying to send commits to this PullRequest and I can't because it looks like you didn't allow the maintainers to do that. There is an option in this pull request that must be checked to allow this. Can you enable it, please? |
@theipster hi!! When you have time, could you pls check my last comment? Here's the patch I'm looking to apply to this PR to test things out: https://gist.github.com/leandrodamascena/69cad79e01fcff4697c3672f5db14f67 |
@leandrodamascena Apologies for the late reply. I've now enabled this setting; thanks for your patch. Is there any reason why mypy requires |
Hi @theipster no worries! Thanks for enabling this. The problem is not in the The problem we're encountering is due to the I changed the field type to I don't know if I made myself very clear about this, but I leave some references here: https://mypy.readthedocs.io/en/stable/common_issues.html#variance |
def test_handle_sqs_json_body_containing_s3_notifications(): | ||
sqs_event_dict = load_event("sqsEvent.json") | ||
s3_event_notification_dict = load_event("s3Event.json") | ||
for record in sqs_event_dict["Records"]: | ||
record["body"] = json_serialize(s3_event_notification_dict) | ||
|
||
parsed_event: SqsS3EventNotificationModel = handle_sqs_json_body_containing_s3_notifications( | ||
sqs_event_dict, LambdaContext() | ||
) | ||
|
||
assert len(parsed_event.Records) == 2 | ||
for parsed_sqs_record in parsed_event.Records: | ||
assert_s3(parsed_sqs_record.body) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like an opinion here @rubenfonseca @heitorlessa. I think this test is ok, do you see another way to optimize this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks ok to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good opportunity to move to the new unit tests since all we should care here is whether the new Model can be instantiated correctly (along with its ramifications, JSON deserialization etc.).
Following this, all you'd need to do here is:
- Create a new S3->SQS JSON event file under
tests/events
- Create a new test at
tests/unit/parser/test_s3.py
- Test whether the model can be initialized without any problem (we already test both SQS and S3 Models)
- Test whether an invalid JSON in the S3 data will lead to a model validation failure
Previously, we were shoving everything under tests/functional
where we have a lot of boilerplate like @event_parser
and these functions, when in fact we're merely testing the Model since we already have separate tests for the @event_parser
... which in this case is a proper functional
test.
Hope that makes sense ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes perfect sense @heitorlessa! I changed the files as per this feedback
| Model name | Description | | ||
| --------------------------------------- | ------------------------------------------------------------------------------------- | | ||
| **AlbModel** | Lambda Event Source payload for Amazon Application Load Balancer | | ||
| **APIGatewayProxyEventModel** | Lambda Event Source payload for Amazon API Gateway | | ||
| **APIGatewayProxyEventV2Model** | Lambda Event Source payload for Amazon API Gateway v2 payload | | ||
| **CloudwatchLogsModel** | Lambda Event Source payload for Amazon CloudWatch Logs | | ||
| **DynamoDBStreamModel** | Lambda Event Source payload for Amazon DynamoDB Streams | | ||
| **EventBridgeModel** | Lambda Event Source payload for Amazon EventBridge | | ||
| **KafkaMskEventModel** | Lambda Event Source payload for AWS MSK payload | | ||
| **KafkaSelfManagedEventModel** | Lambda Event Source payload for self managed Kafka payload | | ||
| **KinesisDataStreamModel** | Lambda Event Source payload for Amazon Kinesis Data Streams | | ||
| **KinesisFirehoseModel** | Lambda Event Source payload for Amazon Kinesis Firehose | | ||
| **LambdaFunctionUrlModel** | Lambda Event Source payload for Lambda Function URL payload | | ||
| **S3EventNotificationEventBridgeModel** | Lambda Event Source payload for Amazon S3 Event Notification to EventBridge. | | ||
| **S3Model** | Lambda Event Source payload for Amazon S3 | | ||
| **S3ObjectLambdaEvent** | Lambda Event Source payload for Amazon S3 Object Lambda | | ||
| **S3SqsEventNotificationModel** | Lambda Event Source payload for S3 event notifications wrapped in SQS event (S3->SQS) | | ||
| **SesModel** | Lambda Event Source payload for Amazon Simple Email Service | | ||
| **SnsModel** | Lambda Event Source payload for Amazon Simple Notification Service | | ||
| **SqsModel** | Lambda Event Source payload for Amazon SQS | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took the opportunity to organize the names of the models in alphabetical order.
Hi @theipster, congrats on this amazing job, you've done most of the work. Thank you very much for your time dedicated to this PR. Do you have any considerations before we make the last review and merge this PR? |
@leandrodamascena Thank you for your support in making this go much more smoothly! Nothing to add from my side, LGTM ✅ |
tests/events/sqsS3Event.json
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we rename this file to s3SqsEvent.json
for consistency?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
tests/unit/parser/test_s3.py
Outdated
|
||
|
||
def test_s3_sqs_event_notification(): | ||
raw_event = load_event("sqsS3Event.json") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change this line too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
tests/unit/parser/test_s3.py
Outdated
|
||
|
||
def test_s3_sqs_event_notification_body_invalid_json(): | ||
raw_event = load_event("s3Event.json") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change this line too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test was wrong. I was using a S3 JSON file to simulate an invalid body, but it is supposed to use an invalid json body (like a string).
Fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you compare against my original code in commit 251d8ba, I used sqsEvent.json
, which is another way to solve this problem.
This is also why I originally named the classes SqsS3...
(because the SQS payload literally wraps the S3 payload) rather than S3Sqs...
(the direction of traffic). They're simply different ways of conceptualising the same scenario, neither is particularly right or wrong I guess!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right @theipster, I used the wrong words to explain why I changed the test. It's not a question of being right or wrong, it's just different ways of solving the same question.
Thank you very much for this feedback, it will help me to avoid mistakes like this in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience! |
Issue number: #2078
Summary
Changes
Exposes a new data model for SQS-wrapped S3 event notifications.
User experience
Please see #2078 description.
Checklist
If your change doesn't seem to apply, please leave them unchecked.
Is this a breaking change?
RFC issue number:
Checklist:
Acknowledgment
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.