"""Docstring.""" import os import boto3 from botocore.exceptions import ClientError import neo4j client = boto3.client('sns') neo4j_url = os.getenv('NEO4J_URL') neo4j_username = os.getenv('NEO4J_USERNAME') neo4j_password = os.getenv('NEO4J_PASSWORD') neo4j_driver = neo4j.GraphDatabase.driver( neo4j_url, auth=( neo4j_username, neo4j_password ), max_retry_time=30 ) def main(): try: identity_ids = get_identity_ids() except Exception as e: raise e finally: neo4j_driver.close() # or else neo4j spews unseless errors out for identity_id in identity_ids: sns_topic_arn = f'arn:aws:sns:us-east-1:437795906767:prod-push-notifications-{identity_id}' # noqa:E501 missing = [ x for x in get_subscriptions(sns_topic_arn) if not subscription_exists(x) ] if missing: print(f'ERROR: {sns_topic_arn}') for m in missing: print(m['SubscriptionArn']) def subscription_exists(sns_subscription): try: client.get_endpoint_attributes( EndpointArn=sns_subscription['Endpoint'] ) except ClientError as e: if e.response['Error']['Code'] == 'NotFound': return False raise e return True def get_subscriptions(sns_topic_arn, next_token=None, previous_subs=[]): result = None try: if not next_token: result = client.list_subscriptions_by_topic( TopicArn=sns_topic_arn ) else: result = client.list_subscriptions_by_topic( TopicArn=sns_topic_arn, NextToken=next_token ) except ClientError as e: if e.response['Error']['Code'] == 'NotFound' and\ e.response['Error']['Message'] == 'Topic does not exist': print(f'ERROR: topic {sns_topic_arn} not found!') return [] next_token = result.get('NextToken', None) total_subs = previous_subs + result['Subscriptions'] if not next_token: return total_subs return get_subscriptions(sns_topic_arn, next_token, total_subs) def get_identity_ids(): session = neo4j_driver.session(access_mode='READ') query = ''' MATCH(i:Identity)-[]-(d:Device) RETURN DISTINCT(i.id) AS id ORDER BY i.id ASC ''' query_results = session.run(query) return [x['id'] for x in query_results] if __name__ == '__main__': main()