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

Add Tags, Tracing, KmsKeyArn, DLQ to serverless(SAM) #853

Merged
merged 3 commits into from
Oct 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
48 changes: 47 additions & 1 deletion tests/test_serverless.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,55 @@
import unittest
from troposphere import Template
from troposphere.serverless import Function, Api, SimpleTable
from troposphere.serverless import (
Api, DeadLetterQueue, Function, S3Location, SimpleTable
)


class TestServerless(unittest.TestCase):
def test_s3_location(self):
serverless_func = Function(
"SomeHandler",
Handler="index.handler",
Runtime="nodejs",
CodeUri=S3Location(
Bucket="mybucket",
Key="mykey",
)
)
t = Template()
t.add_resource(serverless_func)
t.to_json()

def test_tags(self):
serverless_func = Function(
"SomeHandler",
Handler="index.handler",
Runtime="nodejs",
CodeUri="s3://bucket/handler.zip",
Tags={
'Tag1': 'TagValue1',
'Tag2': 'TagValue2'
}
)
t = Template()
t.add_resource(serverless_func)
t.to_json()

def test_DLQ(self):
serverless_func = Function(
"SomeHandler",
Handler="index.handler",
Runtime="nodejs",
CodeUri="s3://bucket/handler.zip",
DeadLetterQueue=DeadLetterQueue(
Type='SNS',
TargetArn='arn:aws:sns:us-east-1:000000000000:SampleTopic'
)
)
t = Template()
t.add_resource(serverless_func)
t.to_json()

def test_required_function(self):
serverless_func = Function(
"SomeHandler",
Expand Down
29 changes: 27 additions & 2 deletions troposphere/serverless.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,46 @@ def policy_validator(x):
+ " policy documents")


class DeadLetterQueue(AWSProperty):
props = {
'Type': (basestring, False),
'TargetArn': (basestring, False)
}

def validate(self):
valid_types = ['SQS', 'SNS']
if ('Type' in self.properties and
self.properties['Type'] not in valid_types):
raise ValueError('Type must be either SQS or SNS')


class S3Location(AWSProperty):
props = {
"Bucket": (basestring, True),
"Key": (basestring, True),
"Version": (basestring, False)
}


class Function(AWSObject):
resource_type = "AWS::Serverless::Function"

props = {
'Handler': (basestring, True),
'Runtime': (basestring, True),
'CodeUri': (basestring, True),
'CodeUri': ((S3Location, basestring), True),
'Description': (basestring, False),
'MemorySize': (validate_memory_size, False),
'Timeout': (positive_integer, False),
'Role': (basestring, False),
'Policies': (policy_validator, False),
'Environment': (Environment, False),
'VpcConfig': (VPCConfig, False),
'Events': (dict, False)
'Events': (dict, False),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The properties should be alphabetized (again, looks like they weren't to begin with).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in the same order as defined here. I do prefer this order though, this way you could follow SAM's example more easily. (So you don't have to rearrange them in your mind to get something to work)

'Tags': (dict, False),
'Tracing': (basestring, False),
'KmsKeyArn': (basestring, False),
'DeadLetterQueue': (DeadLetterQueue, False)
}


Expand Down