Skip to content

Commit

Permalink
add validation for missing Queue or Stream
Browse files Browse the repository at this point in the history
  • Loading branch information
brettstack committed Jun 1, 2018
1 parent c100723 commit a26e50c
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 0 deletions.
5 changes: 5 additions & 0 deletions samtranslator/model/eventsources/pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from samtranslator.model.lambda_ import LambdaEventSourceMapping
from samtranslator.translator.arn_generator import ArnGenerator
from samtranslator.model.exceptions import InvalidEventException


class PullEventSource(ResourceMacro):
Expand Down Expand Up @@ -48,6 +49,10 @@ def to_cloudformation(self, **kwargs):
except NotImplementedError:
function_name_or_arn = function.get_runtime_attr("arn")

if not self.Stream and not self.Queue:
raise InvalidEventException(
self.relative_id, "No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided.")

lambda_eventsourcemapping.FunctionName = function_name_or_arn
lambda_eventsourcemapping.EventSourceArn = self.Stream or self.Queue
lambda_eventsourcemapping.StartingPosition = self.StartingPosition
Expand Down
17 changes: 17 additions & 0 deletions tests/translator/input/error_missing_queue.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# File: sam.yml
# Version: 0.9

AWSTemplateFormatVersion: '2010-09-09'
Parameters: {}
Resources:
SQSFunction:
Type: 'AWS::Serverless::Function'
Properties:
CodeUri: s3://sam-demo-bucket/queues.zip
Handler: queue.sqs_handler
Runtime: python2.7
Events:
MySqsQueue:
Type: SQS
Properties:
BatchSize: 10
15 changes: 15 additions & 0 deletions tests/translator/input/error_missing_stream.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
AWSTemplateFormatVersion: '2010-09-09'
Parameters: {}
Resources:
DynamoDBFunction:
Type: 'AWS::Serverless::Function'
Properties:
CodeUri: s3://sam-demo-bucket/streams.zip
Handler: stream.ddb_handler
Runtime: python2.7
Events:
MyDDBStream:
Type: DynamoDB
Properties:
BatchSize: 200
StartingPosition: LATEST
6 changes: 6 additions & 0 deletions tests/translator/output/aws-cn/error_missing_queue.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"errors": [{
"errorMessage": "Resource with id [SQSFunction] is invalid. Event with id [MySqsQueue] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}],
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [SQSFunction] is invalid. Event with id [MySqsQueue] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}
6 changes: 6 additions & 0 deletions tests/translator/output/aws-cn/error_missing_stream.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"errors": [{
"errorMessage": "Resource with id [DynamoDBFunction] is invalid. Event with id [MyDDBStream] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}],
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [DynamoDBFunction] is invalid. Event with id [MyDDBStream] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}
6 changes: 6 additions & 0 deletions tests/translator/output/aws-us-gov/error_missing_queue.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"errors": [{
"errorMessage": "Resource with id [SQSFunction] is invalid. Event with id [MySqsQueue] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}],
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [SQSFunction] is invalid. Event with id [MySqsQueue] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}
6 changes: 6 additions & 0 deletions tests/translator/output/aws-us-gov/error_missing_stream.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"errors": [{
"errorMessage": "Resource with id [DynamoDBFunction] is invalid. Event with id [MyDDBStream] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}],
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [DynamoDBFunction] is invalid. Event with id [MyDDBStream] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}
6 changes: 6 additions & 0 deletions tests/translator/output/error_missing_queue.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"errors": [{
"errorMessage": "Resource with id [SQSFunction] is invalid. Event with id [MySqsQueue] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}],
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [SQSFunction] is invalid. Event with id [MySqsQueue] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}
6 changes: 6 additions & 0 deletions tests/translator/output/error_missing_stream.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"errors": [{
"errorMessage": "Resource with id [DynamoDBFunction] is invalid. Event with id [MyDDBStream] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}],
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [DynamoDBFunction] is invalid. Event with id [MyDDBStream] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}
2 changes: 2 additions & 0 deletions tests/translator/test_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ def test_transform_success(self, testcase, partition_with_region):
'error_function_with_deployment_preference_missing_alias',
'error_function_with_invalid_deployment_preference_hook_property',
'error_invalid_logical_id',
'error_missing_queue',
'error_missing_stream',
'error_multiple_resource_errors',
'error_s3_not_in_template',
'error_table_invalid_attributetype',
Expand Down

0 comments on commit a26e50c

Please sign in to comment.