Skip to content
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

fix: Useful error message when "Properties" key is missing in embedded connectors #2966

Merged
merged 8 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions samtranslator/translator/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,25 +367,28 @@ def _get_generated_connector(
connector = copy.deepcopy(connector_dict)
connector["Type"] = SamConnector.resource_type

# No need to raise an error for this instance as the error will be caught by the parser
if "Properties" in connector_dict and isinstance(connector_dict["Properties"], dict):
properties = connector["Properties"]
properties["Source"] = {"Id": source_logical_id}
if "SourceReference" in properties:
sam_expect(
properties.get("SourceReference"),
connector_logical_id,
f"{connector_logical_id}.Properties.SourceReference",
).to_be_a_map()

# can't allow user to override the Id using SourceReference
if "Id" in properties["SourceReference"]:
raise InvalidResourceException(
connector_logical_id, "'Id' shouldn't be defined in 'SourceReference'."
)

properties["Source"].update(properties["SourceReference"])
del properties["SourceReference"]
sam_expect(
connector_dict.get("Properties"),
connector_logical_id,
f"{connector_logical_id}.Properties",
is_resource_attribute=True,
).to_be_a_map()

properties = connector["Properties"]
properties["Source"] = {"Id": source_logical_id}
if "SourceReference" in properties:
sam_expect(
properties.get("SourceReference"),
connector_logical_id,
f"{connector_logical_id}.Properties.SourceReference",
).to_be_a_map()

# can't allow user to override the Id using SourceReference
if "Id" in properties["SourceReference"]:
raise InvalidResourceException(connector_logical_id, "'Id' shouldn't be defined in 'SourceReference'.")

properties["Source"].update(properties["SourceReference"])
del properties["SourceReference"]

return SamConnector.from_dict(connector_logical_id, connector)

Expand Down
5 changes: 5 additions & 0 deletions tests/translator/input/error_embedded_connectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ Resources:
MyFunction:
Type: AWS::Serverless::Function
Connectors:
NoProperties:
Destination:
Id: MyQueue
Permissions:
- Write
NonDictProperties:
Properties: foo
EmptyProperties:
Expand Down
2 changes: 1 addition & 1 deletion tests/translator/output/error_embedded_connectors.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 22. Transforming resource with id [DuplicateTest] attempts to create a new resource with id [DuplicateTestDuplicateId] and type \"AWS::Serverless::Connector\". A resource with that id already exists within this template. Please use a different id for that resource. Resource with id [EventsRuleMissingLambdaFunctionArn] is invalid. Unable to get Lambda function ARN from 'Destination' resource. Resource with id [EventsRuleMissingSnsTopicArn] is invalid. Destination.Arn is missing. Resource with id [EventsRuleMissingSqsQueueUrl] is invalid. Unable to create connector from AWS::Events::Rule to AWS::SQS:Queue; it's not supported or the template is invalid. Resource with id [MyFunctionBothIdAndOtherProps] is invalid. Must provide 'Id' (with optional 'Qualifier') or a supported combination of other properties. Resource with id [MyFunctionEmptyDestination] is invalid. Missing required property 'Destination'. Resource with id [MyFunctionEmptyPermissions] is invalid. Missing required property 'Permissions'. Resource with id [MyFunctionEmptyPermissionsList] is invalid. 'Permissions' cannot be empty; valid values are: Read, Write. Resource with id [MyFunctionEmptyProperties] is invalid. Properties of a resource must be an object. Resource with id [MyFunctionMissingRoleMissingRole] is invalid. Unable to get IAM role name from 'Source' resource. Resource with id [MyFunctionNoDestination] is invalid. Missing required property 'Destination'. Resource with id [MyFunctionNoDictConnectorNonDictConnector] is invalid. MyFunctionNoDictConnector.MyFunctionNoDictConnectorNonDictConnector should be a map. Resource with id [MyFunctionNoIdMissingType] is invalid. 'Type' is missing or not a string. Resource with id [MyFunctionNoPermissions] is invalid. Missing required property 'Permissions'. Resource with id [MyFunctionNoStrId] is invalid. 'Id' is missing or not a string. Resource with id [MyFunctionNonDictProperties] is invalid. Properties of a resource must be an object. Resource with id [MyFunctionNonExistentId] is invalid. Unable to find resource with logical ID 'ThisDoesntExist'. Resource with id [MyFunctionTestSourceReferenceId] is invalid. 'Id' shouldn't be defined in 'SourceReference'. Resource with id [MyFunctionUnsupportedAccessCategory] is invalid. Unsupported 'Permissions' provided for connector from AWS::Lambda::Function to AWS::SQS::Queue; valid values are: Read, Write. Resource with id [MyQueueMissingRoleDestination] is invalid. Unable to get IAM role name from 'Destination' resource. Resource with id [MyQueueUnsupportedAccessCategoryCombination] is invalid. Unsupported 'Permissions' provided for connector from AWS::SQS::Queue to AWS::Lambda::Function; valid combinations are: Read + Write. Resource with id [MyResourceWithoutTypeResourceWithoutType] is invalid. 'Type' is missing or not a string."
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 23. Transforming resource with id [DuplicateTest] attempts to create a new resource with id [DuplicateTestDuplicateId] and type \"AWS::Serverless::Connector\". A resource with that id already exists within this template. Please use a different id for that resource. Resource with id [EventsRuleMissingLambdaFunctionArn] is invalid. Unable to get Lambda function ARN from 'Destination' resource. Resource with id [EventsRuleMissingSnsTopicArn] is invalid. Destination.Arn is missing. Resource with id [EventsRuleMissingSqsQueueUrl] is invalid. Unable to create connector from AWS::Events::Rule to AWS::SQS:Queue; it's not supported or the template is invalid. Resource with id [MyFunctionBothIdAndOtherProps] is invalid. Must provide 'Id' (with optional 'Qualifier') or a supported combination of other properties. Resource with id [MyFunctionEmptyDestination] is invalid. Missing required property 'Destination'. Resource with id [MyFunctionEmptyPermissions] is invalid. Missing required property 'Permissions'. Resource with id [MyFunctionEmptyPermissionsList] is invalid. 'Permissions' cannot be empty; valid values are: Read, Write. Resource with id [MyFunctionEmptyProperties] is invalid. Attribute 'MyFunctionEmptyProperties.Properties' should be a map. Resource with id [MyFunctionMissingRoleMissingRole] is invalid. Unable to get IAM role name from 'Source' resource. Resource with id [MyFunctionNoDestination] is invalid. Missing required property 'Destination'. Resource with id [MyFunctionNoDictConnectorNonDictConnector] is invalid. MyFunctionNoDictConnector.MyFunctionNoDictConnectorNonDictConnector should be a map. Resource with id [MyFunctionNoIdMissingType] is invalid. 'Type' is missing or not a string. Resource with id [MyFunctionNoPermissions] is invalid. Missing required property 'Permissions'. Resource with id [MyFunctionNoProperties] is invalid. Attribute 'MyFunctionNoProperties.Properties' should be a map. Resource with id [MyFunctionNoStrId] is invalid. 'Id' is missing or not a string. Resource with id [MyFunctionNonDictProperties] is invalid. Attribute 'MyFunctionNonDictProperties.Properties' should be a map. Resource with id [MyFunctionNonExistentId] is invalid. Unable to find resource with logical ID 'ThisDoesntExist'. Resource with id [MyFunctionTestSourceReferenceId] is invalid. 'Id' shouldn't be defined in 'SourceReference'. Resource with id [MyFunctionUnsupportedAccessCategory] is invalid. Unsupported 'Permissions' provided for connector from AWS::Lambda::Function to AWS::SQS::Queue; valid values are: Read, Write. Resource with id [MyQueueMissingRoleDestination] is invalid. Unable to get IAM role name from 'Destination' resource. Resource with id [MyQueueUnsupportedAccessCategoryCombination] is invalid. Unsupported 'Permissions' provided for connector from AWS::SQS::Queue to AWS::Lambda::Function; valid combinations are: Read + Write. Resource with id [MyResourceWithoutTypeResourceWithoutType] is invalid. 'Type' is missing or not a string."
aaythapa marked this conversation as resolved.
Show resolved Hide resolved
}