Include SNS message attributes with message body when delivering to SQS.

This commit is contained in:
wblack 2018-04-17 16:27:48 +00:00
commit f401c60825
4 changed files with 132 additions and 9 deletions

View file

@ -6,7 +6,7 @@ from collections import defaultdict
from moto.core.responses import BaseResponse
from moto.core.utils import camelcase_to_underscores
from .models import sns_backends
from .exceptions import SNSNotFoundError
from .exceptions import SNSNotFoundError, InvalidParameterValue
from .utils import is_e164
@ -30,6 +30,48 @@ class SNSResponse(BaseResponse):
in attributes
)
def _parse_message_attributes(self, prefix='', value_namespace='Value.'):
message_attributes = self._get_object_map(
'MessageAttributes.entry',
name='Name',
value='Value'
)
# SNS converts some key names before forwarding messages
# DataType -> Type, StringValue -> Value, BinaryValue -> Value
transformed_message_attributes = {}
for name, value in message_attributes.items():
# validation
data_type = value['DataType']
if not data_type:
raise InvalidParameterValue(
"The message attribute '{0}' must contain non-empty "
"message attribute value.".format(name))
data_type_parts = data_type.split('.')
if (len(data_type_parts) > 2 or
data_type_parts[0] not in ['String', 'Binary', 'Number']):
raise InvalidParameterValue(
"The message attribute '{0}' has an invalid message "
"attribute type, the set of supported type prefixes is "
"Binary, Number, and String.".format(name))
if 'StringValue' in value:
value = value['StringValue']
elif 'BinaryValue' in 'Value':
value = value['BinaryValue']
if not value:
raise InvalidParameterValue(
"The message attribute '{0}' must contain non-empty "
"message attribute value for message attribute "
"type '{1}'.".format(name, data_type[0]))
# transformation
transformed_message_attributes[name] = {
'Type': data_type, 'Value': value
}
return transformed_message_attributes
def create_topic(self):
name = self._get_param('Name')
topic = self.backend.create_topic(name)
@ -241,9 +283,10 @@ class SNSResponse(BaseResponse):
phone_number = self._get_param('PhoneNumber')
subject = self._get_param('Subject')
message_attributes = self._get_map_prefix('MessageAttributes.entry',
key_end='Name',
value_end='Value')
try:
message_attributes = self._parse_message_attributes()
except InvalidParameterValue as e:
return self._error(e.description), dict(status=e.code)
if phone_number is not None:
# Check phone is correct syntax (e164)