-
Notifications
You must be signed in to change notification settings - Fork 429
feat(idempotency): support methods with the same name (ABCs) by including fully qualified name in v2 #1535
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
Merged
rubenfonseca
merged 10 commits into
aws-powertools:v2
from
leandrodamascena:feat/idempotency-qual-name
Sep 27, 2022
Merged
feat(idempotency): support methods with the same name (ABCs) by including fully qualified name in v2 #1535
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c0f1e97
feat(v2/idempotency): Changing hash key computation
leandrodamascena 735d345
Merge branch 'v2' into feat/idempotency-qual-name
leandrodamascena 4bbae72
feat(v2/idempotency): adding end2end tests
leandrodamascena 6932775
feat(v2/idempotency): documentation
leandrodamascena a3ddac7
feat(v2/idempotency): addressing feedbacks
leandrodamascena e6fe673
feat(v2/idempotency): new e2e tests
leandrodamascena f062799
feat(v2/idempotency): adding tests in parallel
leandrodamascena b93e471
feat(v2/idempotency): adding tests in parallel
leandrodamascena 4198401
feat(v2/idempotency): refactoring parallel calls to use map instead s…
leandrodamascena 07d76c8
chore(docs): added more details to upgrade guide
rubenfonseca File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Empty file.
This file contains hidden or 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,19 @@ | ||
import pytest | ||
|
||
from tests.e2e.idempotency.infrastructure import IdempotencyDynamoDBStack | ||
|
||
|
||
@pytest.fixture(autouse=True, scope="module") | ||
def infrastructure(tmp_path_factory, worker_id): | ||
"""Setup and teardown logic for E2E test infrastructure | ||
|
||
Yields | ||
------ | ||
Dict[str, str] | ||
CloudFormation Outputs from deployed infrastructure | ||
""" | ||
stack = IdempotencyDynamoDBStack() | ||
try: | ||
yield stack.deploy() | ||
finally: | ||
stack.delete() |
This file contains hidden or 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,11 @@ | ||
from aws_lambda_powertools.utilities.idempotency import DynamoDBPersistenceLayer, idempotent | ||
|
||
persistence_layer = DynamoDBPersistenceLayer(table_name="IdempotencyTable") | ||
|
||
|
||
@idempotent(persistence_store=persistence_layer) | ||
def lambda_handler(event, context): | ||
return { | ||
"message": "success", | ||
"statusCode": 200, | ||
} |
This file contains hidden or 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,23 @@ | ||
from aws_cdk import CfnOutput, RemovalPolicy | ||
from aws_cdk import aws_dynamodb as dynamodb | ||
|
||
from tests.e2e.utils.infrastructure import BaseInfrastructure | ||
|
||
|
||
class IdempotencyDynamoDBStack(BaseInfrastructure): | ||
def create_resources(self): | ||
self.create_lambda_functions() | ||
self._create_dynamodb_table() | ||
|
||
def _create_dynamodb_table(self): | ||
table = dynamodb.Table( | ||
self.stack, | ||
"Idempotency", | ||
table_name="IdempotencyTable", | ||
removal_policy=RemovalPolicy.DESTROY, | ||
partition_key=dynamodb.Attribute(name="id", type=dynamodb.AttributeType.STRING), | ||
time_to_live_attribute="expiration", | ||
billing_mode=dynamodb.BillingMode.PAY_PER_REQUEST, | ||
) | ||
|
||
CfnOutput(self.stack, "DynamoDBTable", value=table.table_name) |
This file contains hidden or 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,30 @@ | ||
import json | ||
|
||
import pytest | ||
|
||
from tests.e2e.utils import data_fetcher | ||
|
||
|
||
@pytest.fixture | ||
def basic_handler_fn(infrastructure: dict) -> str: | ||
return infrastructure.get("BasicHandler", "") | ||
|
||
|
||
@pytest.fixture | ||
def basic_handler_fn_arn(infrastructure: dict) -> str: | ||
return infrastructure.get("BasicHandlerArn", "") | ||
|
||
|
||
def test_basic_idempotency_record(basic_handler_fn_arn: str, basic_handler_fn: str): | ||
# GIVEN | ||
function_name = "basic_handler.lambda_handler" | ||
table_name = "IdempotencyTable" | ||
payload = json.dumps({"message": "Lambda Powertools"}) | ||
|
||
# WHEN | ||
data_fetcher.get_lambda_response(lambda_arn=basic_handler_fn_arn, payload=payload) | ||
|
||
# THEN | ||
ddb_records = data_fetcher.get_ddb_idempotency_record(function_name=function_name, table_name=table_name) | ||
|
||
assert (ddb_records.get_records()) == 1 |
This file contains hidden or 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from tests.e2e.utils.data_fetcher.common import get_http_response, get_lambda_response | ||
from tests.e2e.utils.data_fetcher.idempotency import get_ddb_idempotency_record | ||
from tests.e2e.utils.data_fetcher.logs import get_logs | ||
from tests.e2e.utils.data_fetcher.metrics import get_metrics | ||
from tests.e2e.utils.data_fetcher.traces import get_traces |
This file contains hidden or 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,57 @@ | ||
import boto3 | ||
from retry import retry | ||
|
||
|
||
class DynamoDB: | ||
leandrodamascena marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def __init__( | ||
self, | ||
function_name: str, | ||
table_name: str, | ||
): | ||
"""Fetch and expose Powertools Idempotency key from DynamoDB | ||
|
||
Parameters | ||
---------- | ||
function_name : str | ||
Name of Lambda function to fetch dynamodb record | ||
table_name : str | ||
Name of DynamoDB table | ||
""" | ||
self.function_name = function_name | ||
self.table_name = table_name | ||
self.ddb_client = boto3.resource("dynamodb") | ||
|
||
def get_records(self) -> int: | ||
|
||
table = self.ddb_client.Table(self.table_name) | ||
ret = table.scan( | ||
FilterExpression="contains (id, :functionName)", | ||
ExpressionAttributeValues={":functionName": f"{self.function_name}#"}, | ||
) | ||
|
||
if not ret["Items"]: | ||
raise ValueError("Empty response from DynamoDB Repeating...") | ||
|
||
return ret["Count"] | ||
|
||
|
||
@retry(ValueError, delay=2, jitter=1.5, tries=10) | ||
def get_ddb_idempotency_record( | ||
function_name: str, | ||
table_name: str, | ||
) -> DynamoDB: | ||
"""_summary_ | ||
|
||
Parameters | ||
---------- | ||
function_name : str | ||
Name of Lambda function to fetch dynamodb record | ||
table_name : str | ||
Name of DynamoDB table | ||
|
||
Returns | ||
------- | ||
DynamoDB | ||
DynamoDB instance with dynamodb record | ||
""" | ||
return DynamoDB(function_name=function_name, table_name=table_name) | ||
leandrodamascena marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.