def updateActions(self, actions): '''Update the actions on this account based on the supplied actions. Actions should a dictionary of Amazon Simple Notification Service topic names, and their associated subscriptions.''' # First, we need a SNS Connection to make this changes conn = SNSConnection(**self.kwargs) # Now make sure each subscription is registered to the topic for name, subscriptions in actions.items(): logger.info('Creating topic %s' % name) # Try to make a topic try: arn = conn.create_topic(name)['CreateTopicResponse'][ 'CreateTopicResult']['TopicArn'] self.actions[name] = arn except KeyError: raise EmitterException('Bad response creating topic %s' % name) if len(subscriptions) == 0: raise EmitterException('No subscriptions for action %s' % name) # Now try to arrange for subscriptions # Oddly enough, calling create_topic doesn't have any effect # if the topic already exists, but calling subscribe() for an # existing subscription causes a second subscription to be added # So, we have to get a list of current subscriptions, and then # make sure to only add the subscription if it's currently there logger.info('Getting a list of current subscriptions...') current = conn.get_all_subscriptions_by_topic(arn) current = current['ListSubscriptionsByTopicResponse'] current = current['ListSubscriptionsByTopicResult'] current = current['Subscriptions'] current = set(s['Endpoint'] for s in current) # For all desired subscriptions not present, subscribe for s in subscriptions: if s['endpoint'] not in current: logger.info('Adding %s to action %s' % (s['endpoint'], name)) conn.subscribe(arn, s.get('protocol', 'email'), s['endpoint']) else: logger.info('%s already subscribed to action' % s['endpoint']) # Check for subscriptions that are active, but not listed... activeUnlisted = set(current) - set( [s['endpoint'] for s in subscriptions]) for s in activeUnlisted: logger.warn('Subscript "%s" active, but not listed in config' % s)
def updateActions(self, actions): '''Update the actions on this account based on the supplied actions. Actions should a dictionary of Amazon Simple Notification Service topic names, and their associated subscriptions.''' # First, we need a SNS Connection to make this changes conn = SNSConnection(**self.kwargs) # Now make sure each subscription is registered to the topic for name, subscriptions in actions.items(): logger.info('Creating topic %s' % name) # Try to make a topic try: arn = conn.create_topic(name)['CreateTopicResponse']['CreateTopicResult']['TopicArn'] self.actions[name] = arn except KeyError: raise EmitterException('Bad response creating topic %s' % name) if len(subscriptions) == 0: raise EmitterException('No subscriptions for action %s' % name) # Now try to arrange for subscriptions # Oddly enough, calling create_topic doesn't have any effect # if the topic already exists, but calling subscribe() for an # existing subscription causes a second subscription to be added # So, we have to get a list of current subscriptions, and then # make sure to only add the subscription if it's currently there logger.info('Getting a list of current subscriptions...') current = conn.get_all_subscriptions_by_topic(arn) current = current['ListSubscriptionsByTopicResponse'] current = current['ListSubscriptionsByTopicResult'] current = current['Subscriptions'] current = set(s['Endpoint'] for s in current) # For all desired subscriptions not present, subscribe for s in subscriptions: if s['endpoint'] not in current: logger.info('Adding %s to action %s' % (s['endpoint'], name)) conn.subscribe(arn, s.get('protocol', 'email'), s['endpoint']) else: logger.info('%s already subscribed to action' % s['endpoint']) # Check for subscriptions that are active, but not listed... activeUnlisted = set(current) - set([s['endpoint'] for s in subscriptions]) for s in activeUnlisted: logger.warn('Subscript "%s" active, but not listed in config' % s)
def delete_sns_subscription(): """ Deletes the SNS email subscription """ con = SNSConnection(aws_secret_access_key=AWS_SECRET_ACCESS_KEY, aws_access_key_id=AWS_ACCESS_KEY, region=RegionInfo(name=REGION, endpoint='sns.%s.amazonaws.com' % REGION)) topic_arn = get_topic_arn() subscriptions = con.get_all_subscriptions_by_topic(topic_arn)['ListSubscriptionsByTopicResponse']\ ['ListSubscriptionsByTopicResult']\ ['Subscriptions'] for s in subscriptions: try: print "Unsubscribing %s" % s con.unsubscribe(s['SubscriptionArn']) except: print "Could not unsubscribe %s" % s con.close()