-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parser): add support to VpcLatticeModel (#2584)
Co-authored-by: Ruben Fonseca <rubefons@amazon.com>
- Loading branch information
1 parent
fa7cc51
commit 1931584
Showing
7 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
aws_lambda_powertools/utilities/parser/envelopes/vpc_lattice.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import logging | ||
from typing import Any, Dict, Optional, Type, Union | ||
|
||
from ..models import VpcLatticeModel | ||
from ..types import Model | ||
from .base import BaseEnvelope | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class VpcLatticeEnvelope(BaseEnvelope): | ||
"""Amazon VPC Lattice envelope to extract data within body key""" | ||
|
||
def parse(self, data: Optional[Union[Dict[str, Any], Any]], model: Type[Model]) -> Optional[Model]: | ||
"""Parses data found with model provided | ||
Parameters | ||
---------- | ||
data : Dict | ||
Lambda event to be parsed | ||
model : Type[Model] | ||
Data model provided to parse after extracting data using envelope | ||
Returns | ||
------- | ||
Optional[Model] | ||
Parsed detail payload with model provided | ||
""" | ||
logger.debug(f"Parsing incoming data with VPC Lattice model {VpcLatticeModel}") | ||
parsed_envelope: VpcLatticeModel = VpcLatticeModel.parse_obj(data) | ||
logger.debug(f"Parsing event payload in `detail` with {model}") | ||
return self._parse(data=parsed_envelope.body, model=model) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
aws_lambda_powertools/utilities/parser/models/vpc_lattice.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from typing import Dict, Type, Union | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class VpcLatticeModel(BaseModel): | ||
method: str | ||
raw_path: str | ||
body: Union[str, Type[BaseModel]] | ||
is_base64_encoded: bool | ||
headers: Dict[str, str] | ||
query_string_parameters: Dict[str, str] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import pytest | ||
|
||
from aws_lambda_powertools.utilities.parser import ( | ||
ValidationError, | ||
envelopes, | ||
event_parser, | ||
) | ||
from aws_lambda_powertools.utilities.parser.models import VpcLatticeModel | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
from tests.functional.parser.schemas import MyVpcLatticeBusiness | ||
from tests.functional.utils import load_event | ||
|
||
|
||
@event_parser(model=MyVpcLatticeBusiness, envelope=envelopes.VpcLatticeEnvelope) | ||
def handle_lambda_vpclattice_with_envelope(event: MyVpcLatticeBusiness, context: LambdaContext): | ||
assert event.username == "Leandro" | ||
assert event.name == "Damascena" | ||
|
||
|
||
def test_vpc_lattice_event_with_envelope(): | ||
event = load_event("vpcLatticeEvent.json") | ||
event["body"] = '{"username": "Leandro", "name": "Damascena"}' | ||
handle_lambda_vpclattice_with_envelope(event, LambdaContext()) | ||
|
||
|
||
def test_vpc_lattice_event(): | ||
raw_event = load_event("vpcLatticeEvent.json") | ||
model = VpcLatticeModel(**raw_event) | ||
|
||
assert model.body == raw_event["body"] | ||
assert model.method == raw_event["method"] | ||
assert model.raw_path == raw_event["raw_path"] | ||
assert model.is_base64_encoded == raw_event["is_base64_encoded"] | ||
assert model.headers == raw_event["headers"] | ||
assert model.query_string_parameters == raw_event["query_string_parameters"] | ||
|
||
|
||
def test_vpc_lattice_event_custom_model(): | ||
class MyCustomResource(VpcLatticeModel): | ||
body: str | ||
|
||
raw_event = load_event("vpcLatticeEvent.json") | ||
model = MyCustomResource(**raw_event) | ||
|
||
assert model.body == raw_event["body"] | ||
|
||
|
||
def test_vpc_lattice_event_invalid(): | ||
raw_event = load_event("vpcLatticeEvent.json") | ||
raw_event["body"] = ["some_data"] | ||
|
||
with pytest.raises(ValidationError): | ||
VpcLatticeModel(**raw_event) |