-
Notifications
You must be signed in to change notification settings - Fork 403
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parser): support for CloudFormation Custom Resources (#2335)
Co-authored-by: Leandro Damascena <leandro.damascena@gmail.com> Co-authored-by: Ran Isenberg <ran.isenberg@ranthebuilder.cloud> Co-authored-by: heitorlessa <lessa@amazon.co.uk>
- Loading branch information
1 parent
9a45a01
commit 29d6b94
Showing
7 changed files
with
195 additions
and
21 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
29 changes: 29 additions & 0 deletions
29
aws_lambda_powertools/utilities/parser/models/cloudformation_custom_resource.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,29 @@ | ||
from typing import Any, Dict, Union | ||
|
||
from pydantic import BaseModel, Field, HttpUrl | ||
|
||
from aws_lambda_powertools.utilities.parser.types import Literal | ||
|
||
|
||
class CloudFormationCustomResourceBaseModel(BaseModel): | ||
request_type: str = Field(..., alias="RequestType") | ||
service_token: str = Field(..., alias="ServiceToken") | ||
response_url: HttpUrl = Field(..., alias="ResponseURL") | ||
stack_id: str = Field(..., alias="StackId") | ||
request_id: str = Field(..., alias="RequestId") | ||
logical_resource_id: str = Field(..., alias="LogicalResourceId") | ||
resource_type: str = Field(..., alias="ResourceType") | ||
resource_properties: Union[Dict[str, Any], BaseModel, None] = Field(None, alias="ResourceProperties") | ||
|
||
|
||
class CloudFormationCustomResourceCreateModel(CloudFormationCustomResourceBaseModel): | ||
request_type: Literal["Create"] = Field(..., alias="RequestType") | ||
|
||
|
||
class CloudFormationCustomResourceDeleteModel(CloudFormationCustomResourceBaseModel): | ||
request_type: Literal["Delete"] = Field(..., alias="RequestType") | ||
|
||
|
||
class CloudFormationCustomResourceUpdateModel(CloudFormationCustomResourceBaseModel): | ||
request_type: Literal["Update"] = Field(..., alias="RequestType") | ||
old_resource_properties: Union[Dict[str, Any], BaseModel, None] = Field(None, alias="OldResourceProperties") |
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,13 @@ | ||
{ | ||
"RequestType": "Create", | ||
"ServiceToken": "arn:aws:lambda:us-east-1:xxx:function:xxxx-CrbuiltinfunctionidProvi-2vKAalSppmKe", | ||
"ResponseURL": "https://cloudformation-custom-resource-response-useast1.s3.amazonaws.com/7F%7Cb1f50fdfc25f3b", | ||
"StackId": "arn:aws:cloudformation:us-east-1:xxxx:stack/xxxx/271845b0-f2e8-11ed-90ac-0eeb25b8ae21", | ||
"RequestId": "xxxxx-d2a0-4dfb-ab1f-xxxxxx", | ||
"LogicalResourceId": "xxxxxxxxx", | ||
"ResourceType": "Custom::MyType", | ||
"ResourceProperties": { | ||
"ServiceToken": "arn:aws:lambda:us-east-1:xxxxx:function:xxxxx", | ||
"MyProps": "ss" | ||
} | ||
} |
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,13 @@ | ||
{ | ||
"RequestType": "Delete", | ||
"ServiceToken": "arn:aws:lambda:us-east-1:xxx:function:xxxx-CrbuiltinfunctionidProvi-2vKAalSppmKe", | ||
"ResponseURL": "https://cloudformation-custom-resource-response-useast1.s3.amazonaws.com/7F%7Cb1f50fdfc25f3b", | ||
"StackId": "arn:aws:cloudformation:us-east-1:xxxx:stack/xxxx/271845b0-f2e8-11ed-90ac-0eeb25b8ae21", | ||
"RequestId": "xxxxx-d2a0-4dfb-ab1f-xxxxxx", | ||
"LogicalResourceId": "xxxxxxxxx", | ||
"ResourceType": "Custom::MyType", | ||
"ResourceProperties": { | ||
"ServiceToken": "arn:aws:lambda:us-east-1:xxxxx:function:xxxxx", | ||
"MyProps": "ss" | ||
} | ||
} |
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,17 @@ | ||
{ | ||
"RequestType": "Update", | ||
"ServiceToken": "arn:aws:lambda:us-east-1:xxx:function:xxxx-CrbuiltinfunctionidProvi-2vKAalSppmKe", | ||
"ResponseURL": "https://cloudformation-custom-resource-response-useast1.s3.amazonaws.com/7F%7Cb1f50fdfc25f3b", | ||
"StackId": "arn:aws:cloudformation:us-east-1:xxxx:stack/xxxx/271845b0-f2e8-11ed-90ac-0eeb25b8ae21", | ||
"RequestId": "xxxxx-d2a0-4dfb-ab1f-xxxxxx", | ||
"LogicalResourceId": "xxxxxxxxx", | ||
"ResourceType": "Custom::MyType", | ||
"ResourceProperties": { | ||
"ServiceToken": "arn:aws:lambda:us-east-1:xxxxx:function:xxxxx", | ||
"MyProps": "new" | ||
}, | ||
"OldResourceProperties": { | ||
"ServiceToken": "arn:aws:lambda:us-east-1:xxxxx:function:xxxxx-xxxx-xxx", | ||
"MyProps": "old" | ||
} | ||
} |
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,89 @@ | ||
import pytest | ||
from pydantic import BaseModel, Field | ||
|
||
from aws_lambda_powertools.utilities.parser import ValidationError | ||
from aws_lambda_powertools.utilities.parser.models import ( | ||
CloudFormationCustomResourceCreateModel, | ||
CloudFormationCustomResourceDeleteModel, | ||
CloudFormationCustomResourceUpdateModel, | ||
) | ||
from tests.functional.utils import load_event | ||
|
||
|
||
def test_cloudformation_custom_resource_create_event(): | ||
raw_event = load_event("cloudformationCustomResourceCreate.json") | ||
model = CloudFormationCustomResourceCreateModel(**raw_event) | ||
|
||
assert model.request_type == raw_event["RequestType"] | ||
assert model.request_id == raw_event["RequestId"] | ||
assert model.service_token == raw_event["ServiceToken"] | ||
assert str(model.response_url) == raw_event["ResponseURL"] | ||
assert model.stack_id == raw_event["StackId"] | ||
assert model.logical_resource_id == raw_event["LogicalResourceId"] | ||
assert model.resource_type == raw_event["ResourceType"] | ||
assert model.resource_properties == raw_event["ResourceProperties"] | ||
|
||
|
||
def test_cloudformation_custom_resource_create_event_custom_model(): | ||
class MyModel(BaseModel): | ||
MyProps: str | ||
|
||
class MyCustomResource(CloudFormationCustomResourceCreateModel): | ||
resource_properties: MyModel = Field(..., alias="ResourceProperties") | ||
|
||
raw_event = load_event("cloudformationCustomResourceCreate.json") | ||
model = MyCustomResource(**raw_event) | ||
|
||
assert model.resource_properties.MyProps == raw_event["ResourceProperties"].get("MyProps") | ||
|
||
|
||
def test_cloudformation_custom_resource_create_event_invalid(): | ||
raw_event = load_event("cloudformationCustomResourceCreate.json") | ||
raw_event["ResourceProperties"] = ["some_data"] | ||
|
||
with pytest.raises(ValidationError): | ||
CloudFormationCustomResourceCreateModel(**raw_event) | ||
|
||
|
||
def test_cloudformation_custom_resource_update_event(): | ||
raw_event = load_event("cloudformationCustomResourceUpdate.json") | ||
model = CloudFormationCustomResourceUpdateModel(**raw_event) | ||
|
||
assert model.request_type == raw_event["RequestType"] | ||
assert model.request_id == raw_event["RequestId"] | ||
assert model.service_token == raw_event["ServiceToken"] | ||
assert str(model.response_url) == raw_event["ResponseURL"] | ||
assert model.stack_id == raw_event["StackId"] | ||
assert model.logical_resource_id == raw_event["LogicalResourceId"] | ||
assert model.resource_type == raw_event["ResourceType"] | ||
assert model.resource_properties == raw_event["ResourceProperties"] | ||
assert model.old_resource_properties == raw_event["OldResourceProperties"] | ||
|
||
|
||
def test_cloudformation_custom_resource_update_event_invalid(): | ||
raw_event = load_event("cloudformationCustomResourceUpdate.json") | ||
raw_event["OldResourceProperties"] = ["some_data"] | ||
|
||
with pytest.raises(ValidationError): | ||
CloudFormationCustomResourceUpdateModel(**raw_event) | ||
|
||
|
||
def test_cloudformation_custom_resource_delete_event(): | ||
raw_event = load_event("cloudformationCustomResourceDelete.json") | ||
model = CloudFormationCustomResourceDeleteModel(**raw_event) | ||
|
||
assert model.request_type == raw_event["RequestType"] | ||
assert model.request_id == raw_event["RequestId"] | ||
assert model.service_token == raw_event["ServiceToken"] | ||
assert str(model.response_url) == raw_event["ResponseURL"] | ||
assert model.stack_id == raw_event["StackId"] | ||
assert model.logical_resource_id == raw_event["LogicalResourceId"] | ||
assert model.resource_type == raw_event["ResourceType"] | ||
assert model.resource_properties == raw_event["ResourceProperties"] | ||
|
||
|
||
def test_cloudformation_custom_resource_delete_event_invalid(): | ||
raw_event = load_event("cloudformationCustomResourceDelete.json") | ||
raw_event["ResourceProperties"] = ["some_data"] | ||
with pytest.raises(ValidationError): | ||
CloudFormationCustomResourceDeleteModel(**raw_event) |