Skip to content
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
22 changes: 21 additions & 1 deletion localstack/services/sns/sns_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ def do_unsubscribe(subscription_arn):
def _get_tags(topic_arn):
if topic_arn not in SNS_TAGS:
SNS_TAGS[topic_arn] = []

return SNS_TAGS[topic_arn]


Expand All @@ -295,7 +296,26 @@ def do_list_tags_for_resource(topic_arn):


def do_tag_resource(topic_arn, tags):
_get_tags(topic_arn).extend(tags)
existing_tags = SNS_TAGS.get(topic_arn, [])
tags = [
tag for idx, tag in enumerate(tags)
if tag not in tags[:idx]
]

def existing_tag_index(item):
for idx, tag in enumerate(existing_tags):
if item['Key'] == tag['Key']:
return idx
return None

for item in tags:
existing_index = existing_tag_index(item)
if existing_index is None:
existing_tags.append(item)
else:
existing_tags[existing_index] = item

SNS_TAGS[topic_arn] = existing_tags


def do_untag_resource(topic_arn, tag_keys):
Expand Down
22 changes: 22 additions & 0 deletions tests/integration/test_sns.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,17 @@ def test_tags(self):
'Key': '456',
'Value': 'def'
},
{
'Key': '456',
'Value': 'def'
}
]
)

tags = self.sns_client.list_tags_for_resource(ResourceArn=self.topic_arn)
distinct_tags = [tag for idx, tag in enumerate(tags['Tags']) if tag not in tags['Tags'][:idx]]
# test for duplicate tags
self.assertEqual(len(tags['Tags']), len(distinct_tags))
self.assertEqual(len(tags['Tags']), 2)
self.assertEqual(tags['Tags'][0]['Key'], '123')
self.assertEqual(tags['Tags'][0]['Value'], 'abc')
Expand All @@ -182,6 +189,21 @@ def test_tags(self):
self.assertEqual(tags['Tags'][0]['Key'], '456')
self.assertEqual(tags['Tags'][0]['Value'], 'def')

self.sns_client.tag_resource(
ResourceArn=self.topic_arn,
Tags=[
{
'Key': '456',
'Value': 'pqr'
}
]
)

tags = self.sns_client.list_tags_for_resource(ResourceArn=self.topic_arn)
self.assertEqual(len(tags['Tags']), 1)
self.assertEqual(tags['Tags'][0]['Key'], '456')
self.assertEqual(tags['Tags'][0]['Value'], 'pqr')

def test_dead_letter_queue(self):
lambda_name = 'test-%s' % short_uid()
lambda_arn = aws_stack.lambda_function_arn(lambda_name)
Expand Down