Add sns.tag_resource

This commit is contained in:
gruebel 2019-10-12 20:36:15 +02:00
commit 726775678c
5 changed files with 183 additions and 11 deletions

View file

@ -10,6 +10,14 @@ class SNSNotFoundError(RESTError):
"NotFound", message)
class ResourceNotFoundError(RESTError):
code = 404
def __init__(self):
super(ResourceNotFoundError, self).__init__(
'ResourceNotFound', 'Resource does not exist')
class DuplicateSnsEndpointError(RESTError):
code = 400
@ -42,6 +50,14 @@ class InvalidParameterValue(RESTError):
"InvalidParameterValue", message)
class TagLimitExceededError(RESTError):
code = 400
def __init__(self):
super(TagLimitExceededError, self).__init__(
'TagLimitExceeded', 'Could not complete request: tag quota of per resource exceeded')
class InternalError(RESTError):
code = 500

View file

@ -18,7 +18,7 @@ from moto.awslambda import lambda_backends
from .exceptions import (
SNSNotFoundError, DuplicateSnsEndpointError, SnsEndpointDisabled, SNSInvalidParameter,
InvalidParameterValue, InternalError
InvalidParameterValue, InternalError, ResourceNotFoundError, TagLimitExceededError
)
from .utils import make_arn_for_topic, make_arn_for_subscription
@ -504,8 +504,23 @@ class SNSBackend(BaseBackend):
raise SNSInvalidParameter("Invalid parameter: FilterPolicy: Match value must be String, number, true, false, or null")
def list_tags_for_resource(self, resource_arn):
if resource_arn not in self.topics:
raise ResourceNotFoundError
return self.topics[resource_arn]._tags
def tag_resource(self, resource_arn, tags):
if resource_arn not in self.topics:
raise ResourceNotFoundError
updated_tags = self.topics[resource_arn]._tags.copy()
updated_tags.update(tags)
if len(updated_tags) > 50:
raise TagLimitExceededError
self.topics[resource_arn]._tags = updated_tags
sns_backends = {}
for region in Session().get_available_regions('sns'):

View file

@ -699,15 +699,19 @@ class SNSResponse(BaseResponse):
def list_tags_for_resource(self):
arn = self._get_param('ResourceArn')
if arn not in self.backend.topics:
error_response = self._error('ResourceNotFound', 'Resource does not exist')
return error_response, dict(status=404)
result = self.backend.list_tags_for_resource(arn)
template = self.response_template(LIST_TAGS_FOR_RESOURCE_TEMPLATE)
return template.render(tags=result)
def tag_resource(self):
arn = self._get_param('ResourceArn')
tags = self._get_tags()
self.backend.tag_resource(arn, tags)
return self.response_template(TAG_RESOURCE_TEMPLATE).render()
CREATE_TOPIC_TEMPLATE = """<CreateTopicResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/">
<CreateTopicResult>
@ -1105,3 +1109,10 @@ LIST_TAGS_FOR_RESOURCE_TEMPLATE = """<ListTagsForResourceResponse xmlns="http://
<RequestId>97fa763f-861b-5223-a946-20251f2a42e2</RequestId>
</ResponseMetadata>
</ListTagsForResourceResponse>"""
TAG_RESOURCE_TEMPLATE = """<TagResourceResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/">
<TagResourceResult/>
<ResponseMetadata>
<RequestId>fd4ab1da-692f-50a7-95ad-e7c665877d98</RequestId>
</ResponseMetadata>
</TagResourceResponse>"""