Support SNS subscription attributes (#1087)

* remove code for local test

* Add SNS set_subscription_attributes and get_subscription_attributes
This commit is contained in:
Toshiya Kawasaki 2017-09-08 03:19:34 +09:00 committed by Jack Danger
commit 0c3708a8e7
5 changed files with 142 additions and 3 deletions

View file

@ -24,3 +24,11 @@ class SnsEndpointDisabled(RESTError):
def __init__(self, message):
super(SnsEndpointDisabled, self).__init__(
"EndpointDisabled", message)
class SNSInvalidParameter(RESTError):
code = 400
def __init__(self, message):
super(SNSInvalidParameter, self).__init__(
"InvalidParameter", message)

View file

@ -13,7 +13,7 @@ from moto.core import BaseBackend, BaseModel
from moto.core.utils import iso_8601_datetime_with_milliseconds
from moto.sqs import sqs_backends
from .exceptions import (
SNSNotFoundError, DuplicateSnsEndpointError, SnsEndpointDisabled
SNSNotFoundError, DuplicateSnsEndpointError, SnsEndpointDisabled, SNSInvalidParameter
)
from .utils import make_arn_for_topic, make_arn_for_subscription
@ -76,6 +76,7 @@ class Subscription(BaseModel):
self.endpoint = endpoint
self.protocol = protocol
self.arn = make_arn_for_subscription(self.topic.arn)
self.attributes = {}
def publish(self, message, message_id):
if self.protocol == 'sqs':
@ -301,6 +302,26 @@ class SNSBackend(BaseBackend):
raise SNSNotFoundError(
"Endpoint with arn {0} not found".format(arn))
def get_subscription_attributes(self, arn):
_subscription = [_ for _ in self.subscriptions.values() if _.arn == arn]
if not _subscription:
raise SNSNotFoundError("Subscription with arn {0} not found".format(arn))
subscription = _subscription[0]
return subscription.attributes
def set_subscription_attributes(self, arn, name, value):
if name not in ['RawMessageDelivery', 'DeliveryPolicy']:
raise SNSInvalidParameter('AttributeName')
# TODO: should do validation
_subscription = [_ for _ in self.subscriptions.values() if _.arn == arn]
if not _subscription:
raise SNSNotFoundError("Subscription with arn {0} not found".format(arn))
subscription = _subscription[0]
subscription.attributes[name] = value
sns_backends = {}
for region in boto.sns.regions():

View file

@ -445,6 +445,20 @@ class SNSResponse(BaseResponse):
template = self.response_template(DELETE_ENDPOINT_TEMPLATE)
return template.render()
def get_subscription_attributes(self):
arn = self._get_param('SubscriptionArn')
attributes = self.backend.get_subscription_attributes(arn)
template = self.response_template(GET_SUBSCRIPTION_ATTRIBUTES_TEMPLATE)
return template.render(attributes=attributes)
def set_subscription_attributes(self):
arn = self._get_param('SubscriptionArn')
attr_name = self._get_param('AttributeName')
attr_value = self._get_param('AttributeValue')
self.backend.set_subscription_attributes(arn, attr_name, attr_value)
template = self.response_template(SET_SUBSCRIPTION_ATTRIBUTES_TEMPLATE)
return template.render()
CREATE_TOPIC_TEMPLATE = """<CreateTopicResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/">
<CreateTopicResult>
@ -719,3 +733,28 @@ LIST_SUBSCRIPTIONS_BY_TOPIC_TEMPLATE = """<ListSubscriptionsByTopicResponse xmln
<RequestId>384ac68d-3775-11df-8963-01868b7c937a</RequestId>
</ResponseMetadata>
</ListSubscriptionsByTopicResponse>"""
# Not responding aws system attribetus like 'Owner' and 'SubscriptionArn'
GET_SUBSCRIPTION_ATTRIBUTES_TEMPLATE = """<GetSubscriptionAttributesResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/">
<GetSubscriptionAttributesResult>
<Attributes>
{% for name, value in attributes.items() %}
<entry>
<key>{{ name }}</key>
<value>{{ value }}</value>
</entry>
{% endfor %}
</Attributes>
</GetSubscriptionAttributesResult>
<ResponseMetadata>
<RequestId>057f074c-33a7-11df-9540-99d0768312d3</RequestId>
</ResponseMetadata>
</GetSubscriptionAttributesResponse>"""
SET_SUBSCRIPTION_ATTRIBUTES_TEMPLATE = """<SetSubscriptionAttributesResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/">
<ResponseMetadata>
<RequestId>a8763b99-33a7-11df-a9b7-05d48da6f042</RequestId>
</ResponseMetadata>
</SetSubscriptionAttributesResponse>"""