From 73610020489e819bf9253883b2a72baf4581814a Mon Sep 17 00:00:00 2001 From: gomi_ningen Date: Wed, 3 Jul 2019 02:50:26 +0900 Subject: [PATCH] feat: Add support to specify a SNS topic that belongs to a different region (#989) --- samtranslator/model/eventsources/push.py | 7 +++++-- samtranslator/model/sns.py | 1 + samtranslator/validator/sam_schema/schema.json | 3 +++ tests/model/eventsources/test_sns_event_source.py | 13 +++++++++++++ ...nction_with_sns_event_source_all_parameters.yaml | 1 + ...nction_with_sns_event_source_all_parameters.json | 3 ++- ...nction_with_sns_event_source_all_parameters.json | 3 ++- ...nction_with_sns_event_source_all_parameters.json | 3 ++- versions/2016-10-31.md | 1 + 9 files changed, 30 insertions(+), 5 deletions(-) diff --git a/samtranslator/model/eventsources/push.py b/samtranslator/model/eventsources/push.py index 14217806f..6b759ab5f 100644 --- a/samtranslator/model/eventsources/push.py +++ b/samtranslator/model/eventsources/push.py @@ -344,6 +344,7 @@ class SNS(PushEventSource): principal = 'sns.amazonaws.com' property_types = { 'Topic': PropertyType(True, is_str()), + 'Region': PropertyType(False, is_str()), 'FilterPolicy': PropertyType(False, dict_of(is_str(), list_of(one_of(is_str(), is_type(dict))))) } @@ -360,13 +361,15 @@ def to_cloudformation(self, **kwargs): raise TypeError("Missing required keyword argument: function") return [self._construct_permission(function, source_arn=self.Topic), - self._inject_subscription(function, self.Topic, self.FilterPolicy)] + self._inject_subscription(function, self.Topic, self.Region, self.FilterPolicy)] - def _inject_subscription(self, function, topic, filterPolicy): + def _inject_subscription(self, function, topic, region, filterPolicy): subscription = SNSSubscription(self.logical_id) subscription.Protocol = 'lambda' subscription.Endpoint = function.get_runtime_attr("arn") subscription.TopicArn = topic + if region is not None: + subscription.Region = region if CONDITION in function.resource_attributes: subscription.set_resource_attribute(CONDITION, function.resource_attributes[CONDITION]) diff --git a/samtranslator/model/sns.py b/samtranslator/model/sns.py index 7bfb1b94a..4b4290b94 100644 --- a/samtranslator/model/sns.py +++ b/samtranslator/model/sns.py @@ -8,5 +8,6 @@ class SNSSubscription(Resource): 'Endpoint': PropertyType(True, is_str()), 'Protocol': PropertyType(True, is_str()), 'TopicArn': PropertyType(True, is_str()), + 'Region': PropertyType(False, is_str()), 'FilterPolicy': PropertyType(False, is_type(dict)) } diff --git a/samtranslator/validator/sam_schema/schema.json b/samtranslator/validator/sam_schema/schema.json index 8fa499b9e..98046249f 100644 --- a/samtranslator/validator/sam_schema/schema.json +++ b/samtranslator/validator/sam_schema/schema.json @@ -632,6 +632,9 @@ "Topic": { "type": "string" }, + "Region": { + "type": "string" + }, "FilterPolicy": { "type": "object" } diff --git a/tests/model/eventsources/test_sns_event_source.py b/tests/model/eventsources/test_sns_event_source.py index efedec6ca..434aca948 100644 --- a/tests/model/eventsources/test_sns_event_source.py +++ b/tests/model/eventsources/test_sns_event_source.py @@ -31,8 +31,21 @@ def test_to_cloudformation_returns_permission_and_subscription_resources(self): self.assertEqual(subscription.TopicArn, 'arn:aws:sns:MyTopic') self.assertEqual(subscription.Protocol, 'lambda') self.assertEqual(subscription.Endpoint, 'arn:aws:lambda:mock') + self.assertIsNone(subscription.Region) self.assertIsNone(subscription.FilterPolicy) + def test_to_cloudformation_passes_the_region(self): + region = 'us-west-2' + self.sns_event_source.Region = region + + resources = self.sns_event_source.to_cloudformation( + function=self.function) + self.assertEqual(len(resources), 2) + self.assertEqual(resources[1].resource_type, + 'AWS::SNS::Subscription') + subscription = resources[1] + self.assertEqual(subscription.Region, region) + def test_to_cloudformation_passes_the_filter_policy(self): filterPolicy = { 'attribute1': ['value1'], diff --git a/tests/translator/input/function_with_sns_event_source_all_parameters.yaml b/tests/translator/input/function_with_sns_event_source_all_parameters.yaml index a745b2f9c..65711e733 100644 --- a/tests/translator/input/function_with_sns_event_source_all_parameters.yaml +++ b/tests/translator/input/function_with_sns_event_source_all_parameters.yaml @@ -11,6 +11,7 @@ Resources: Type: SNS Properties: Topic: topicArn + Region: region FilterPolicy: store: - example_corp diff --git a/tests/translator/output/aws-cn/function_with_sns_event_source_all_parameters.json b/tests/translator/output/aws-cn/function_with_sns_event_source_all_parameters.json index 2a2072b9b..cac15164c 100644 --- a/tests/translator/output/aws-cn/function_with_sns_event_source_all_parameters.json +++ b/tests/translator/output/aws-cn/function_with_sns_event_source_all_parameters.json @@ -33,7 +33,8 @@ ] }, "Protocol": "lambda", - "TopicArn": "topicArn" + "TopicArn": "topicArn", + "Region": "region" } }, "MyAwesomeFunctionNotificationTopicPermission": { diff --git a/tests/translator/output/aws-us-gov/function_with_sns_event_source_all_parameters.json b/tests/translator/output/aws-us-gov/function_with_sns_event_source_all_parameters.json index 4e682d5be..5ebf5d48e 100644 --- a/tests/translator/output/aws-us-gov/function_with_sns_event_source_all_parameters.json +++ b/tests/translator/output/aws-us-gov/function_with_sns_event_source_all_parameters.json @@ -33,7 +33,8 @@ ] }, "Protocol": "lambda", - "TopicArn": "topicArn" + "TopicArn": "topicArn", + "Region": "region" } }, "MyAwesomeFunctionNotificationTopicPermission": { diff --git a/tests/translator/output/function_with_sns_event_source_all_parameters.json b/tests/translator/output/function_with_sns_event_source_all_parameters.json index fb7465588..698eb0055 100644 --- a/tests/translator/output/function_with_sns_event_source_all_parameters.json +++ b/tests/translator/output/function_with_sns_event_source_all_parameters.json @@ -33,7 +33,8 @@ ] }, "Protocol": "lambda", - "TopicArn": "topicArn" + "TopicArn": "topicArn", + "Region": "region" } }, "MyAwesomeFunctionNotificationTopicPermission": { diff --git a/versions/2016-10-31.md b/versions/2016-10-31.md index 9493abade..bab076985 100644 --- a/versions/2016-10-31.md +++ b/versions/2016-10-31.md @@ -426,6 +426,7 @@ The object describing an event source with type `SNS`. Property Name | Type | Description ---|:---:|--- Topic | `string` | **Required.** Topic ARN. +Region | `string` | Region. FilterPolicy | [Amazon SNS filter policy](https://docs.aws.amazon.com/sns/latest/dg/message-filtering.html) | Policy assigned to the topic subscription in order to receive only a subset of the messages. ##### Example: SNS event source object