<s> from __future__ import unicode_literals <EOL> import datetime <EOL> import uuid <EOL> import json <EOL> import boto . sns <EOL> import requests <EOL> import six <EOL> from moto . compat import OrderedDict <EOL> from moto . core import BaseBackend <EOL> from moto . core . utils import iso_8601_datetime_with_milliseconds <EOL> from moto . sqs import sqs_backends <EOL> from . exceptions import SNSNotFoundError <EOL> from . utils import make_arn_for_topic , make_arn_for_subscription <EOL> DEFAULT_ACCOUNT_ID = <NUM_LIT> <EOL> DEFAULT_PAGE_SIZE = <NUM_LIT:100> <EOL> class Topic ( object ) : <EOL> def __init__ ( self , name , sns_backend ) : <EOL> self . name = name <EOL> self . sns_backend = sns_backend <EOL> self . account_id = DEFAULT_ACCOUNT_ID <EOL> self . display_name = "<STR_LIT>" <EOL> self . policy = DEFAULT_TOPIC_POLICY <EOL> self . delivery_policy = "<STR_LIT>" <EOL> self . effective_delivery_policy = DEFAULT_EFFECTIVE_DELIVERY_POLICY <EOL> self . arn = make_arn_for_topic ( self . account_id , name , sns_backend . region_name ) <EOL> self . subscriptions_pending = <NUM_LIT:0> <EOL> self . subscriptions_confimed = <NUM_LIT:0> <EOL> self . subscriptions_deleted = <NUM_LIT:0> <EOL> def publish ( self , message ) : <EOL> message_id = six . text_type ( uuid . uuid4 ( ) ) <EOL> subscriptions , _ = self . sns_backend . list_subscriptions ( self . arn ) <EOL> for subscription in subscriptions : <EOL> subscription . publish ( message , message_id ) <EOL> return message_id <EOL> def get_cfn_attribute ( self , attribute_name ) : <EOL> from moto . cloudformation . exceptions import UnformattedGetAttTemplateException <EOL> if attribute_name == '<STR_LIT>' : <EOL> return self . name <EOL> raise UnformattedGetAttTemplateException ( ) <EOL> @ property <EOL> def physical_resource_id ( self ) : <EOL> return self . arn <EOL> @ classmethod <EOL> def create_from_cloudformation_json ( cls , resource_name , cloudformation_json , region_name ) : <EOL> sns_backend = sns_backends [ region_name ] <EOL> properties = cloudformation_json [ '<STR_LIT>' ] <EOL> topic = sns_backend . create_topic ( <EOL> properties . get ( "<STR_LIT>" ) <EOL> ) <EOL> for subscription in properties . get ( "<STR_LIT>" , [ ] ) : <EOL> sns_backend . subscribe ( topic . arn , subscription [ '<STR_LIT>' ] , subscription [ '<STR_LIT>' ] ) <EOL> return topic <EOL> class Subscription ( object ) : <EOL> def __init__ ( self , topic , endpoint , protocol ) : <EOL> self . topic = topic <EOL> self . endpoint = endpoint <EOL> self . protocol = protocol <EOL> self . arn = make_arn_for_subscription ( self . topic . arn ) <EOL> def publish ( self , message , message_id ) : <EOL> if self . protocol == '<STR_LIT>' : <EOL> queue_name = self . endpoint . split ( "<STR_LIT::>" ) [ - <NUM_LIT:1> ] <EOL> region = self . endpoint . split ( "<STR_LIT::>" ) [ <NUM_LIT:3> ] <EOL> sqs_backends [ region ] . send_message ( queue_name , message ) <EOL> elif self . protocol in [ '<STR_LIT:http>' , '<STR_LIT>' ] : <EOL> post_data = self . get_post_data ( message , message_id ) <EOL> requests . post ( self . endpoint , data = post_data ) <EOL> def get_post_data ( self , message , message_id ) : <EOL> return { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : message_id , <EOL> "<STR_LIT>" : self . topic . arn , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : message , <EOL> "<STR_LIT>" : iso_8601_datetime_with_milliseconds ( datetime . datetime . now ( ) ) , <EOL> "<STR_LIT>" : "<STR_LIT:1>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> class PlatformApplication ( object ) : <EOL> def __init__ ( self , region , name , platform , attributes ) : <EOL> self . region = region <EOL> self . name = name <EOL> self . platform = platform <EOL> self . attributes = attributes <EOL> @ property <EOL> def arn ( self ) : <EOL> return "<STR_LIT>" . format ( <EOL> region = self . region , <EOL> platform = self . platform , <EOL> name = self . name , <EOL> ) <EOL> class PlatformEndpoint ( object ) : <EOL> def __init__ ( self , region , application , custom_user_data , token , attributes ) : <EOL> self . region = region <EOL> self . application = application <EOL> self . custom_user_data = custom_user_data <EOL> self . token = token <EOL> self . attributes = attributes <EOL> self . id = uuid . uuid4 ( ) <EOL> @ property <EOL> def arn ( self ) : <EOL> return "<STR_LIT>" . format ( <EOL> region = self . region , <EOL> platform = self . application . platform , <EOL> name = self . application . name , <EOL> id = self . id , <EOL> ) <EOL> def publish ( self , message ) : <EOL> message_id = six . text_type ( uuid . uuid4 ( ) ) <EOL> return message_id <EOL> class SNSBackend ( BaseBackend ) : <EOL> def __init__ ( self , region_name ) : <EOL> super ( SNSBackend , self ) . __init__ ( ) <EOL> self . topics = OrderedDict ( ) <EOL> self . subscriptions = OrderedDict ( ) <EOL> self . applications = { } <EOL> self . platform_endpoints = { } <EOL> self . region_name = region_name <EOL> def reset ( self ) : <EOL> region_name = self . region_name <EOL> self . __dict__ = { } <EOL> self . __init__ ( region_name ) <EOL> def create_topic ( self , name ) : <EOL> topic = Topic ( name , self ) <EOL> self . topics [ topic . arn ] = topic <EOL> return topic <EOL> def _get_values_nexttoken ( self , values_map , next_token = None ) : <EOL> if next_token is None : <EOL> next_token = <NUM_LIT:0> <EOL> next_token = int ( next_token ) <EOL> values = list ( values_map . values ( ) ) [ next_token : next_token + DEFAULT_PAGE_SIZE ] <EOL> if len ( values ) == DEFAULT_PAGE_SIZE : <EOL> next_token = next_token + DEFAULT_PAGE_SIZE <EOL> else : <EOL> next_token = None <EOL> return values , next_token <EOL> def list_topics ( self , next_token = None ) : <EOL> return self . _get_values_nexttoken ( self . topics , next_token ) <EOL> def delete_topic ( self , arn ) : <EOL> self . topics . pop ( arn ) <EOL> def get_topic ( self , arn ) : <EOL> try : <EOL> return self . topics [ arn ] <EOL> except KeyError : <EOL> raise SNSNotFoundError ( "<STR_LIT>" . format ( arn ) ) <EOL> def set_topic_attribute ( self , topic_arn , attribute_name , attribute_value ) : <EOL> topic = self . get_topic ( topic_arn ) <EOL> setattr ( topic , attribute_name , attribute_value ) <EOL> def subscribe ( self , topic_arn , endpoint , protocol ) : <EOL> topic = self . get_topic ( topic_arn ) <EOL> subscription = Subscription ( topic , endpoint , protocol ) <EOL> self . subscriptions [ subscription . arn ] = subscription <EOL> return subscription <EOL> def unsubscribe ( self , subscription_arn ) : <EOL> self . subscriptions . pop ( subscription_arn ) <EOL> def list_subscriptions ( self , topic_arn = None , next_token = None ) : <EOL> if topic_arn : <EOL> topic = self . get_topic ( topic_arn ) <EOL> filtered = OrderedDict ( [ ( k , sub ) for k , sub in self . subscriptions . items ( ) if sub . topic == topic ] ) <EOL> return self . _get_values_nexttoken ( filtered , next_token ) <EOL> else : <EOL> return self . _get_values_nexttoken ( self . subscriptions , next_token ) <EOL> def publish ( self , arn , message ) : <EOL> try : <EOL> topic = self . get_topic ( arn ) <EOL> message_id = topic . publish ( message ) <EOL> except SNSNotFoundError : <EOL> endpoint = self . get_endpoint ( arn ) <EOL> message_id = endpoint . publish ( message ) <EOL> return message_id <EOL> def create_platform_application ( self , region , name , platform , attributes ) : <EOL> application = PlatformApplication ( region , name , platform , attributes ) <EOL> self . applications [ application . arn ] = application <EOL> return application <EOL> def get_application ( self , arn ) : <EOL> try : <EOL> return self . applications [ arn ] <EOL> except KeyError : <EOL> raise SNSNotFoundError ( "<STR_LIT>" . format ( arn ) ) <EOL> def set_application_attributes ( self , arn , attributes ) : <EOL> application = self . get_application ( arn ) <EOL> application . attributes . update ( attributes ) <EOL> return application <EOL> def list_platform_applications ( self ) : <EOL> return self . applications . values ( ) <EOL> def delete_platform_application ( self , platform_arn ) : <EOL> self . applications . pop ( platform_arn ) <EOL> def create_platform_endpoint ( self , region , application , custom_user_data , token , attributes ) : <EOL> platform_endpoint = PlatformEndpoint ( region , application , custom_user_data , token , attributes ) <EOL> self . platform_endpoints [ platform_endpoint . arn ] = platform_endpoint <EOL> return platform_endpoint <EOL> def list_endpoints_by_platform_application ( self , application_arn ) : <EOL> return [ <EOL> endpoint for endpoint <EOL> in self . platform_endpoints . values ( ) <EOL> if endpoint . application . arn == application_arn <EOL> ] <EOL> def get_endpoint ( self , arn ) : <EOL> try : <EOL> return self . platform_endpoints [ arn ] <EOL> except KeyError : <EOL> raise SNSNotFoundError ( "<STR_LIT>" . format ( arn ) ) <EOL> def set_endpoint_attributes ( self , arn , attributes ) : <EOL> endpoint = self . get_endpoint ( arn ) <EOL> endpoint . attributes . update ( attributes ) <EOL> return endpoint <EOL> sns_backends = { } <EOL> for region in boto . sns . regions ( ) : <EOL> sns_backends [ region . name ] = SNSBackend ( region . name ) <EOL> DEFAULT_TOPIC_POLICY = json . dumps ( { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT:*>" <EOL> } , <EOL> "<STR_LIT>" : [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } <EOL> } ] <EOL> } ) <EOL> DEFAULT_EFFECTIVE_DELIVERY_POLICY = json . dumps ( { <EOL> '<STR_LIT:http>' : { <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:20> , <EOL> '<STR_LIT>' : <NUM_LIT:20> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:3> , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> } <EOL> } ) </s>
