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

Feature: SNS Event FilterPolicyScope attribute #2988

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
1 change: 1 addition & 0 deletions integration/combination/test_function_with_sns.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ def test_function_with_sns_intrinsics(self):
subscription_arn = subscription["SubscriptionArn"]
subscription_attributes = sns_client.get_subscription_attributes(SubscriptionArn=subscription_arn)
self.assertEqual(subscription_attributes["Attributes"]["FilterPolicy"], '{"price_usd":[{"numeric":["<",100]}]}')
self.assertEqual(subscription_attributes["Attributes"]["FilterPolicyScope"], "MessageAttributes")
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Resources:
- numeric:
- <
- 100
FilterPolicyScope: MessageAttributes
Region:
Ref: AWS::Region
SqsSubscription: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class SqsSubscription(BaseModel):

class SNSEventProperties(BaseModel):
FilterPolicy: Optional[PassThroughProp] = snseventproperties("FilterPolicy")
FilterPolicyScope: Optional[PassThroughProp] # TODO: add documentation
Region: Optional[PassThroughProp] = snseventproperties("Region")
SqsSubscription: Optional[Union[bool, SqsSubscription]] = snseventproperties("SqsSubscription")
Topic: PassThroughProp = snseventproperties("Topic")
Expand Down
27 changes: 24 additions & 3 deletions samtranslator/model/eventsources/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from samtranslator.intrinsics.resolver import IntrinsicsResolver
from samtranslator.metrics.method_decorator import cw_timer
from samtranslator.model import PropertyType, ResourceMacro
from samtranslator.model import PassThroughProperty, PropertyType, ResourceMacro
from samtranslator.model.cognito import CognitoUserPool
from samtranslator.model.eventbridge_utils import EventBridgeRuleUtils
from samtranslator.model.events import EventsRule, generate_valid_target_id
Expand Down Expand Up @@ -486,13 +486,15 @@ class SNS(PushEventSource):
"Topic": PropertyType(True, IS_STR),
"Region": PropertyType(False, IS_STR),
"FilterPolicy": PropertyType(False, dict_of(IS_STR, list_of(one_of(IS_STR, IS_DICT)))),
"FilterPolicyScope": PassThroughProperty(False),
"SqsSubscription": PropertyType(False, one_of(IS_BOOL, IS_DICT)),
"RedrivePolicy": PropertyType(False, IS_DICT),
}

Topic: str
Region: Optional[str]
FilterPolicy: Optional[Dict[str, Any]]
FilterPolicyScope: Optional[str]
SqsSubscription: Optional[Any]
RedrivePolicy: Optional[Dict[str, Any]]

Expand All @@ -518,6 +520,7 @@ def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def]
self.Topic,
self.Region,
self.FilterPolicy,
self.FilterPolicyScope,
self.RedrivePolicy,
function,
)
Expand All @@ -532,7 +535,14 @@ def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def]

queue_policy = self._inject_sqs_queue_policy(self.Topic, queue_arn, queue_url, function) # type: ignore[no-untyped-call]
subscription = self._inject_subscription(
"sqs", queue_arn, self.Topic, self.Region, self.FilterPolicy, self.RedrivePolicy, function
"sqs",
queue_arn,
self.Topic,
self.Region,
self.FilterPolicy,
self.FilterPolicyScope,
self.RedrivePolicy,
function,
)
event_source = self._inject_sqs_event_source_mapping(function, role, queue_arn) # type: ignore[no-untyped-call]

Expand Down Expand Up @@ -560,7 +570,14 @@ def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def]
self.Topic, queue_arn, queue_url, function, queue_policy_logical_id
)
subscription = self._inject_subscription(
"sqs", queue_arn, self.Topic, self.Region, self.FilterPolicy, self.RedrivePolicy, function
"sqs",
queue_arn,
self.Topic,
self.Region,
self.FilterPolicy,
self.FilterPolicyScope,
self.RedrivePolicy,
function,
)
event_source = self._inject_sqs_event_source_mapping(function, role, queue_arn, batch_size, enabled) # type: ignore[no-untyped-call]

Expand All @@ -576,6 +593,7 @@ def _inject_subscription( # noqa: too-many-arguments
topic: str,
region: Optional[str],
filterPolicy: Optional[Dict[str, Any]],
filterPolicyScope: Optional[str],
redrivePolicy: Optional[Dict[str, Any]],
function: Any,
) -> SNSSubscription:
Expand All @@ -590,6 +608,9 @@ def _inject_subscription( # noqa: too-many-arguments
if filterPolicy is not None:
subscription.FilterPolicy = filterPolicy

if filterPolicyScope is not None:
subscription.FilterPolicyScope = filterPolicyScope

if redrivePolicy is not None:
subscription.RedrivePolicy = redrivePolicy

Expand Down
1 change: 1 addition & 0 deletions samtranslator/model/sns.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class SNSSubscription(Resource):
"TopicArn": GeneratedProperty(),
"Region": GeneratedProperty(),
"FilterPolicy": GeneratedProperty(),
"FilterPolicyScope": GeneratedProperty(),
"RedrivePolicy": GeneratedProperty(),
}

Expand Down
3 changes: 3 additions & 0 deletions samtranslator/schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -196572,6 +196572,9 @@
"markdownDescription": "The filter policy JSON assigned to the subscription\\. For more information, see [GetSubscriptionAttributes](https://docs.aws.amazon.com/sns/latest/api/API_GetSubscriptionAttributes.html) in the Amazon Simple Notification Service API Reference\\. \n*Type*: [SnsFilterPolicy](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-sns-subscription.html#cfn-sns-subscription-filterpolicy) \n*Required*: No \n*AWS CloudFormation compatibility*: This property is passed directly to the [`FilterPolicy`](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-sns-subscription.html#cfn-sns-subscription-filterpolicy) property of an `AWS::SNS::Subscription` resource\\.",
"title": "FilterPolicy"
},
"FilterPolicyScope": {
"$ref": "#/definitions/PassThroughProp"
},
"Region": {
"allOf": [
{
Expand Down
3 changes: 3 additions & 0 deletions samtranslator/validator/sam_schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,9 @@
},
"FilterPolicy": {
"type": "object"
},
"FilterPolicyScope": {
"type": "string"
}
},
"required": [
Expand Down
3 changes: 3 additions & 0 deletions schema_source/sam.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2470,6 +2470,9 @@
"markdownDescription": "The filter policy JSON assigned to the subscription\\. For more information, see [GetSubscriptionAttributes](https://docs.aws.amazon.com/sns/latest/api/API_GetSubscriptionAttributes.html) in the Amazon Simple Notification Service API Reference\\. \n*Type*: [SnsFilterPolicy](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-sns-subscription.html#cfn-sns-subscription-filterpolicy) \n*Required*: No \n*AWS CloudFormation compatibility*: This property is passed directly to the [`FilterPolicy`](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-sns-subscription.html#cfn-sns-subscription-filterpolicy) property of an `AWS::SNS::Subscription` resource\\.",
"title": "FilterPolicy"
},
"FilterPolicyScope": {
"$ref": "#/definitions/PassThroughProp"
},
"Region": {
"allOf": [
{
Expand Down
12 changes: 12 additions & 0 deletions tests/model/eventsources/test_sns_event_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def test_to_cloudformation_returns_permission_and_subscription_resources(self):
self.assertEqual(subscription.Endpoint, "arn:aws:lambda:mock")
self.assertIsNone(subscription.Region)
self.assertIsNone(subscription.FilterPolicy)
self.assertIsNone(subscription.FilterPolicyScope)
self.assertIsNone(subscription.RedrivePolicy)

def test_to_cloudformation_passes_the_region(self):
Expand All @@ -56,6 +57,16 @@ def test_to_cloudformation_passes_the_filter_policy(self):
subscription = resources[1]
self.assertEqual(subscription.FilterPolicy, filterPolicy)

def test_to_cloudformation_passes_the_filter_policy_scope(self):
filterPolicyScope = "MessageAttributes"
self.sns_event_source.FilterPolicyScope = filterPolicyScope

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.FilterPolicyScope, filterPolicyScope)

def test_to_cloudformation_passes_the_redrive_policy(self):
redrive_policy = {"deadLetterTargetArn": "arn:aws:sqs:us-east-2:123456789012:MyDeadLetterQueue"}
self.sns_event_source.RedrivePolicy = redrive_policy
Expand Down Expand Up @@ -89,4 +100,5 @@ def test_to_cloudformation_when_sqs_subscription_disable(self):
self.assertEqual(subscription.Endpoint, "arn:aws:lambda:mock")
self.assertIsNone(subscription.Region)
self.assertIsNone(subscription.FilterPolicy)
self.assertIsNone(subscription.FilterPolicyScope)
self.assertIsNone(subscription.RedrivePolicy)
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ Resources:
- numeric:
- '>='
- 100
FilterPolicyScope: MessageAttributes
1 change: 1 addition & 0 deletions tests/translator/input/sns_existing_sqs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Resources:
- numeric:
- '>='
- 100
FilterPolicyScope: MessageAttributes

Notifications:
Type: AWS::SNS::Topic
Expand Down
1 change: 1 addition & 0 deletions tests/translator/input/sns_intrinsics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Resources:
- numeric:
- <
- 100
FilterPolicyScope: MessageAttributes
Region:
Ref: SnsRegion
SqsSubscription: true
Expand Down
1 change: 1 addition & 0 deletions tests/translator/input/sns_outside_sqs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Resources:
- numeric:
- '>='
- 100
FilterPolicyScope: MessageAttributes

Notifications:
Type: AWS::SNS::Topic
1 change: 1 addition & 0 deletions tests/translator/input/sns_sqs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Resources:
- numeric:
- '>='
- 100
FilterPolicyScope: MessageAttributes

Notifications:
Type: AWS::SNS::Topic
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"example_corp"
]
},
"FilterPolicyScope": "MessageAttributes",
"Protocol": "lambda",
"Region": "region",
"TopicArn": "topicArn"
Expand Down
1 change: 1 addition & 0 deletions tests/translator/output/aws-cn/sns_existing_sqs.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"example_corp"
]
},
"FilterPolicyScope": "MessageAttributes",
"Protocol": "sqs",
"TopicArn": {
"Ref": "Notifications"
Expand Down
1 change: 1 addition & 0 deletions tests/translator/output/aws-cn/sns_intrinsics.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
}
]
},
"FilterPolicyScope": "MessageAttributes",
"Protocol": "sqs",
"Region": {
"Ref": "SnsRegion"
Expand Down
1 change: 1 addition & 0 deletions tests/translator/output/aws-cn/sns_outside_sqs.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"example_corp"
]
},
"FilterPolicyScope": "MessageAttributes",
"Protocol": "sqs",
"TopicArn": {
"Ref": "Notifications"
Expand Down
1 change: 1 addition & 0 deletions tests/translator/output/aws-cn/sns_sqs.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"example_corp"
]
},
"FilterPolicyScope": "MessageAttributes",
"Protocol": "sqs",
"TopicArn": {
"Ref": "Notifications"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"example_corp"
]
},
"FilterPolicyScope": "MessageAttributes",
"Protocol": "lambda",
"Region": "region",
"TopicArn": "topicArn"
Expand Down
1 change: 1 addition & 0 deletions tests/translator/output/aws-us-gov/sns_existing_sqs.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"example_corp"
]
},
"FilterPolicyScope": "MessageAttributes",
"Protocol": "sqs",
"TopicArn": {
"Ref": "Notifications"
Expand Down
1 change: 1 addition & 0 deletions tests/translator/output/aws-us-gov/sns_intrinsics.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
}
]
},
"FilterPolicyScope": "MessageAttributes",
"Protocol": "sqs",
"Region": {
"Ref": "SnsRegion"
Expand Down
1 change: 1 addition & 0 deletions tests/translator/output/aws-us-gov/sns_outside_sqs.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"example_corp"
]
},
"FilterPolicyScope": "MessageAttributes",
"Protocol": "sqs",
"TopicArn": {
"Ref": "Notifications"
Expand Down
1 change: 1 addition & 0 deletions tests/translator/output/aws-us-gov/sns_sqs.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"example_corp"
]
},
"FilterPolicyScope": "MessageAttributes",
"Protocol": "sqs",
"TopicArn": {
"Ref": "Notifications"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"example_corp"
]
},
"FilterPolicyScope": "MessageAttributes",
"Protocol": "lambda",
"Region": "region",
"TopicArn": "topicArn"
Expand Down
1 change: 1 addition & 0 deletions tests/translator/output/sns_existing_sqs.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"example_corp"
]
},
"FilterPolicyScope": "MessageAttributes",
"Protocol": "sqs",
"TopicArn": {
"Ref": "Notifications"
Expand Down
1 change: 1 addition & 0 deletions tests/translator/output/sns_intrinsics.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
}
]
},
"FilterPolicyScope": "MessageAttributes",
"Protocol": "sqs",
"Region": {
"Ref": "SnsRegion"
Expand Down
1 change: 1 addition & 0 deletions tests/translator/output/sns_outside_sqs.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"example_corp"
]
},
"FilterPolicyScope": "MessageAttributes",
"Protocol": "sqs",
"TopicArn": {
"Ref": "Notifications"
Expand Down
1 change: 1 addition & 0 deletions tests/translator/output/sns_sqs.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"example_corp"
]
},
"FilterPolicyScope": "MessageAttributes",
"Protocol": "sqs",
"TopicArn": {
"Ref": "Notifications"
Expand Down