-
Notifications
You must be signed in to change notification settings - Fork 433
feat(event_source): add CloudFormationCustomResourceEvent data class. #4342
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
leandrodamascena
merged 6 commits into
aws-powertools:develop
from
phipag:feat/cloudformation-custom-resource
May 23, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d90f57b
feat: add CloudFormationCustomResourceEvent event source data class.
phipag ec6a6fa
Merge branch 'develop' into feat/cloudformation-custom-resource
leandrodamascena b6a8b7d
fix: remove CloudFormationRequestType type and other minor changes.
phipag da645ba
Merge branch 'develop' into feat/cloudformation-custom-resource
leandrodamascena 2f795a5
fix: fix copy-paste error when loading events in test_cloudformation_…
phipag 9fcbe05
Merge branch 'develop' into feat/cloudformation-custom-resource
phipag 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
45 changes: 45 additions & 0 deletions
45
aws_lambda_powertools/utilities/data_classes/cloudformation_custom_resource_event.py
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,45 @@ | ||
from typing import Any, Dict, Literal | ||
|
||
from aws_lambda_powertools.utilities.data_classes.common import DictWrapper | ||
|
||
|
||
class CloudFormationCustomResourceEvent(DictWrapper): | ||
@property | ||
def request_type(self) -> Literal["Create", "Update", "Delete"]: | ||
return self["RequestType"] | ||
|
||
@property | ||
def service_token(self) -> str: | ||
return self["ServiceToken"] | ||
|
||
@property | ||
def response_url(self) -> str: | ||
return self["ResponseURL"] | ||
|
||
@property | ||
def stack_id(self) -> str: | ||
return self["StackId"] | ||
|
||
@property | ||
def request_id(self) -> str: | ||
return self["RequestId"] | ||
|
||
@property | ||
def logical_resource_id(self) -> str: | ||
return self["LogicalResourceId"] | ||
|
||
@property | ||
def physical_resource_id(self) -> str: | ||
return self.get("PhysicalResourceId") or "" | ||
Check warning on line 33 in aws_lambda_powertools/utilities/data_classes/cloudformation_custom_resource_event.py
|
||
|
||
@property | ||
def resource_type(self) -> str: | ||
return self["ResourceType"] | ||
|
||
@property | ||
def resource_properties(self) -> Dict[str, Any]: | ||
return self.get("ResourceProperties") or {} | ||
|
||
@property | ||
def old_resource_properties(self) -> Dict[str, Any]: | ||
return self.get("OldResourceProperties") or {} |
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
43 changes: 43 additions & 0 deletions
43
examples/event_sources/src/cloudformation_custom_resource_handler.py
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,43 @@ | ||
from aws_lambda_powertools import Logger | ||
from aws_lambda_powertools.utilities.data_classes import ( | ||
CloudFormationCustomResourceEvent, | ||
event_source, | ||
) | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
logger = Logger() | ||
|
||
|
||
@event_source(data_class=CloudFormationCustomResourceEvent) | ||
def lambda_handler(event: CloudFormationCustomResourceEvent, context: LambdaContext): | ||
request_type = event.request_type | ||
|
||
if request_type == "Create": | ||
return on_create(event) | ||
if request_type == "Update": | ||
return on_update(event) | ||
if request_type == "Delete": | ||
return on_delete(event) | ||
|
||
|
||
def on_create(event: CloudFormationCustomResourceEvent): | ||
props = event.resource_properties | ||
logger.info(f"Create new resource with props {props}.") | ||
|
||
# Add your create code here ... | ||
physical_id = ... | ||
|
||
return {"PhysicalResourceId": physical_id} | ||
|
||
|
||
def on_update(event: CloudFormationCustomResourceEvent): | ||
physical_id = event.physical_resource_id | ||
props = event.resource_properties | ||
logger.info(f"Update resource {physical_id} with props {props}.") | ||
# ... | ||
|
||
|
||
def on_delete(event: CloudFormationCustomResourceEvent): | ||
physical_id = event.physical_resource_id | ||
logger.info(f"Delete resource {physical_id}.") | ||
# ... |
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
29 changes: 29 additions & 0 deletions
29
tests/unit/data_classes/test_cloudformation_custom_resource_event.py
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,29 @@ | ||
import pytest | ||
|
||
from aws_lambda_powertools.utilities.data_classes import ( | ||
CloudFormationCustomResourceEvent, | ||
) | ||
from tests.functional.utils import load_event | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"event_file", | ||
[ | ||
"cloudformationCustomResourceCreate.json", | ||
"cloudformationCustomResourceUpdate.json", | ||
"cloudformationCustomResourceDelete.json", | ||
], | ||
) | ||
def test_cloudformation_custom_resource_event(event_file): | ||
raw_event = load_event(event_file) | ||
parsed_event = CloudFormationCustomResourceEvent(raw_event) | ||
|
||
assert parsed_event.request_type == raw_event["RequestType"] | ||
assert parsed_event.service_token == raw_event["ServiceToken"] | ||
assert parsed_event.stack_id == raw_event["StackId"] | ||
assert parsed_event.request_id == raw_event["RequestId"] | ||
assert parsed_event.response_url == raw_event["ResponseURL"] | ||
assert parsed_event.logical_resource_id == raw_event["LogicalResourceId"] | ||
assert parsed_event.resource_type == raw_event["ResourceType"] | ||
assert parsed_event.resource_properties == raw_event.get("ResourceProperties", {}) | ||
assert parsed_event.old_resource_properties == raw_event.get("OldResourceProperties", {}) |
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.