From 77f99e34ff58175a8d738e87991aaf4411bea040 Mon Sep 17 00:00:00 2001 From: cktricky Date: Sat, 19 May 2018 22:00:31 -0300 Subject: [PATCH] cool, now we can delete sns topics --- libs/sns.py | 19 +++++++++++++++++++ modules/sns.py | 11 +++++++++++ 2 files changed, 30 insertions(+) diff --git a/libs/sns.py b/libs/sns.py index 400ca00..3faabec 100644 --- a/libs/sns.py +++ b/libs/sns.py @@ -57,3 +57,22 @@ def list_sns_subscribers(topic,region): print("Unexpected error: {}" .format(e)) except KeyboardInterrupt: print("CTRL-C received, exiting...") + +def delete_sns_topic(topic, region): + try: + client = boto3.client('sns', region_name=region) + action = client.delete_topic(TopicArn=topic) + print("Deleted Topic: {}".format(topic)) + except botocore.exceptions.ClientError as e: + if e.response['Error']['Code'] == 'InvalidClientTokenId': + sys.exit("The AWS KEY IS INVALID. Exiting") + if e.response['Error']['Code'] == 'AccessDenied': + print('{} : Is NOT a root key' .format(AWS_ACCESS_KEY_ID)) + elif e.response['Error']['Code'] == 'SubscriptionRequiredException': + print('{} : Has permissions but isnt signed up for service - usually means you have a root account' .format(AWS_ACCESS_KEY_ID)) + elif e.response['Error']['Code'] == 'InvalidParameter': + print('The region you provided ({}) is invalid for the Topic ARN. Are you sure this topic exists in this region?'.format(region)) + else: + print("Unexpected error: {}" .format(e)) + except KeyboardInterrupt: + print("CTRL-C received, exiting...") diff --git a/modules/sns.py b/modules/sns.py index 8a8e42a..3f37609 100644 --- a/modules/sns.py +++ b/modules/sns.py @@ -21,3 +21,14 @@ def module_sns_list_subscribers(*args): list_sns_subscribers(args[0][0], args[0][1]) except IndexError: print("Please provide a topic arn *AND* region, ex: -a arn:aws:sns:us-east-1:123456789123:sometopic,us-east-1") + +def module_sns_delete_topic(*args): + ''' + SNS delete a topic. Takes two arguments - the topic arn and the region. + python3 weirdAAL.py -m sns_delete_topic -a arn:aws:sns:us-east-1:123456789123:sometopic,us-east-1 + ''' + try: + if args[0][0] and args[0][1]: + delete_sns_topic(args[0][0], args[0][1]) + except IndexError: + print("Please provide a topic arn *AND* region, ex: -a arn:aws:sns:us-east-1:123456789123:sometopic,us-east-1")