devxoul
7/20/2015 - 2:59 PM

Enable Amazon SNS endpoint.

Enable Amazon SNS endpoint.

from boto.sns import connect_to_region

AWS_ACCESS_KEY = ''
AWS_SECRET_KEY = ''
AWS_REGION = ''
ARN = ''


sns = connect_to_region(AWS_REGION,
                        aws_access_key_id=AWS_ACCESS_KEY,
                        aws_secret_access_key=AWS_SECRET_KEY)


def search(*tokens):
    arns = []
    for token in tokens:
        response = sns.create_platform_endpoint(ARN, token)
        arns.append(response['CreatePlatformEndpointResponse']
                            ['CreatePlatformEndpointResult']
                            ['EndpointArn'])
    return arns


def enable(*tokens):
    arns = search(*tokens)
    for arn in arns:
        sns.set_endpoint_attributes(arn, {'Enabled': True})
        response = sns.get_endpoint_attributes(arn)
        response = response['GetEndpointAttributesResponse']
        response = response['GetEndpointAttributesResult']
        response = response['Attributes']
        enabled = response['Enabled']
        print 'Enabled: {}'.format(enabled)


def prune():
    next_token = None

    while True:
        response = sns.list_endpoints_by_platform_application(ARN, next_token)
        response = response['ListEndpointsByPlatformApplicationResponse']
        response = response['ListEndpointsByPlatformApplicationResult']
        endpoints = response['Endpoints']
        next_token = response['NextToken']
        if not next_token:
            return

        for endpoint in endpoints:
            if endpoint['Attributes']['Enabled'] == 'false':
                sns.delete_endpoint(endpoint['EndpointArn'])
                print "Delete " + endpoint['Attributes']['Token']

enable('token')