Skip to content

Commit b778609

Browse files
Adding parser
1 parent dfb4618 commit b778609

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

aws_lambda_powertools/utilities/parser/models/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
S3Model,
6969
S3RecordModel,
7070
)
71+
from .s3_batch_operation import S3BatchOperationJobModel, S3BatchOperationModel, S3BatchOperationTaskModel
7172
from .s3_event_notification import (
7273
S3SqsEventNotificationModel,
7374
S3SqsEventNotificationRecordModel,
@@ -177,4 +178,7 @@
177178
"BedrockAgentEventModel",
178179
"BedrockAgentRequestBodyModel",
179180
"BedrockAgentRequestMediaModel",
181+
"S3BatchOperationJobModel",
182+
"S3BatchOperationModel",
183+
"S3BatchOperationTaskModel",
180184
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from typing import Any, Dict, List, Optional
2+
3+
from pydantic import BaseModel, validator
4+
5+
from aws_lambda_powertools.utilities.parser.types import Literal
6+
7+
8+
class S3BatchOperationTaskModel(BaseModel):
9+
taskId: str
10+
s3Key: str
11+
s3VersionId: Optional[str] = None
12+
s3BucketArn: Optional[str] = None
13+
s3Bucket: Optional[str] = None
14+
15+
@validator("s3Bucket", pre=True, always=True)
16+
def validate_bucket(cls, current_value, values):
17+
# Get the s3 bucket, either from 's3Bucket' property (invocationSchemaVersion '2.0')
18+
# or from 's3BucketArn' (invocationSchemaVersion '1.0')
19+
if values.get("s3BucketArn") and not current_value:
20+
# Replace s3Bucket value with the value from s3BucketArn
21+
return values["s3BucketArn"].split(":::")[-1]
22+
return current_value
23+
24+
25+
class S3BatchOperationJobModel(BaseModel):
26+
id: str
27+
userArguments: Optional[Dict[str, Any]] = None
28+
29+
30+
class S3BatchOperationModel(BaseModel):
31+
invocationId: str
32+
invocationSchemaVersion: Literal["1.0", "2.0"]
33+
job: S3BatchOperationJobModel
34+
tasks: List[S3BatchOperationTaskModel]

docs/utilities/parser.md

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ Parser comes with the following built-in models:
191191
| **KinesisFirehoseModel** | Lambda Event Source payload for Amazon Kinesis Firehose |
192192
| **KinesisFirehoseSqsModel** | Lambda Event Source payload for SQS messages wrapped in Kinesis Firehose records |
193193
| **LambdaFunctionUrlModel** | Lambda Event Source payload for Lambda Function URL payload |
194+
| **S3BatchOperationModel** | Lambda Event Source payload for Amazon S3 Batch Operation |
194195
| **S3EventNotificationEventBridgeModel** | Lambda Event Source payload for Amazon S3 Event Notification to EventBridge. |
195196
| **S3Model** | Lambda Event Source payload for Amazon S3 |
196197
| **S3ObjectLambdaEvent** | Lambda Event Source payload for Amazon S3 Object Lambda |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from aws_lambda_powertools.utilities.parser.models import S3BatchOperationModel
2+
from tests.functional.utils import load_event
3+
4+
5+
def test_s3_batch_operation_v1_trigger_event():
6+
raw_event = load_event("s3BatchOperationEventSchemaV1.json")
7+
parsed_event: S3BatchOperationModel = S3BatchOperationModel(**raw_event)
8+
9+
tasks = list(parsed_event.tasks)
10+
assert len(tasks) == 1
11+
12+
assert parsed_event.invocationId == raw_event["invocationId"]
13+
assert parsed_event.invocationSchemaVersion == raw_event["invocationSchemaVersion"]
14+
assert parsed_event.job.id == raw_event["job"]["id"]
15+
16+
assert tasks[0].taskId == raw_event["tasks"][0]["taskId"]
17+
assert tasks[0].s3Key == raw_event["tasks"][0]["s3Key"]
18+
assert tasks[0].s3VersionId == raw_event["tasks"][0]["s3VersionId"]
19+
assert tasks[0].s3BucketArn == raw_event["tasks"][0]["s3BucketArn"]
20+
assert tasks[0].s3Bucket == "powertools-dataset"
21+
22+
23+
def test_s3_batch_operation_v2_trigger_event():
24+
raw_event = load_event("s3BatchOperationEventSchemaV2.json")
25+
parsed_event: S3BatchOperationModel = S3BatchOperationModel(**raw_event)
26+
27+
tasks = list(parsed_event.tasks)
28+
assert len(tasks) == 1
29+
30+
assert parsed_event.invocationId == raw_event["invocationId"]
31+
assert parsed_event.invocationSchemaVersion == raw_event["invocationSchemaVersion"]
32+
assert parsed_event.job.id == raw_event["job"]["id"]
33+
assert parsed_event.job.userArguments == raw_event["job"]["userArguments"]
34+
35+
assert tasks[0].taskId == raw_event["tasks"][0]["taskId"]
36+
assert tasks[0].s3Key == raw_event["tasks"][0]["s3Key"]
37+
assert tasks[0].s3VersionId == raw_event["tasks"][0]["s3VersionId"]
38+
assert tasks[0].s3Bucket == raw_event["tasks"][0]["s3Bucket"]

0 commit comments

Comments
 (0)