Ejemplo n.º 1
0
        def _subscribe_topic_activity(self, task):
            activity_data = { "topic_arn": None, "email": {"endpoint" : None, "SubscriptionArn" : None}, "sms": {"endpoint" : None, "SubscriptionArn" : None} }
            if task:
                input = json.loads(task)
                activity_data["email"]["endpoint"] = input["email"]
                activity_data["sms"]["endpoint"] = input["sms"]
            else:
                self.fail(reason=json.dumps({"reason", "Didn't receive any input!", "detail", "" }))
                return False, "Didn't receive any input!"
 
            sns_client = sns.SNSConnection(aws_access_key_id=ACCESS, aws_secret_access_key=SECRET)
 
            # Create the topic and get the ARN
            result, activity_data["topic_arn"] = self._create_topic(sns_client)
            if result:           
                    # Subscribe the user to the topic, using either or both endpoints.
                    for protocol in ["email", "sms"]:
                        ep = activity_data[protocol]["endpoint"]
                        if (ep):
                            print("About to subscribe protocol: " + protocol + " ep: " + ep)
                            response = sns_client.subscribe(activity_data["topic_arn"], protocol, ep)
                            print(response)
                            activity_data[protocol]["SubscriptionArn"] = response['SubscribeResponse']['SubscribeResult']["SubscriptionArn"]
                    # If at least one subscription arn is set, consider this a success.
                    if (activity_data["email"]["SubscriptionArn"] != None) or (activity_data["sms"]["SubscriptionArn"] != None):
                        self.complete(result=json.dumps(activity_data))
                        return True, json.dumps(activity_data)
                    else:
                        self.fail(reason=json.dumps({ "reason" : "Couldn't subscribe to SNS topic", "detail" : "" }))
                        return False, "Couldn't subscribe to SNS topic"
            else:
                return False, "Couldn't create SNS topic"
Ejemplo n.º 2
0
        def _wait_for_confirmation_activity(self, task):
            if task:
                subscription_data = json.loads(task)
            else:
                self.fail(reason=json.dumps({"reason", "Didn't receive any input!", "detail", "" }))
                return
 
            sns_client = sns.SNSConnection(aws_access_key_id=ACCESS, aws_secret_access_key=SECRET)
            topic = sns_client.get_topic_attributes(subscription_data["topic_arn"])
 
            if topic:
                subscription_confirmed = False
            else:
                self.fail(result=json.dumps({ "reason" : "Couldn't get SWF topic ARN", "detail" : "Topic ARN: %s" % subscription_data["topic_arn"] }))
                return
 
            # Loop through all of the subscriptions to this topic until we get some indication that a subscription was confirmed.
            while not subscription_confirmed:
                for sub in sns_client.get_all_subscriptions_by_topic(subscription_data["topic_arn"])["ListSubscriptionsByTopicResponse"]["ListSubscriptionsByTopicResult"]["Subscriptions"]:
                    if subscription_data[sub["Protocol"]]["endpoint"] == sub["Endpoint"]:
                        # this is one of the endpoints we're interested in. Is it subscribed?
                        if sub["SubscriptionArn"] != 'PendingConfirmation':
                            subscription_data[sub["Protocol"]]["subscription_arn"] = sub["SubscriptionArn"]
                            subscription_confirmed = True
 
            self.complete(result=json.dumps(subscription_data))
            return True, json.dumps(subscription_data)
Ejemplo n.º 3
0
    def __init__(self, topicName, level=logging.info):
        self.snsConn = sns.SNSConnection(credenciales.access_key_id,
                                         credenciales.secret_access_key)
        self.loggingTopicArn = None
        topics = self.snsConn.get_all_topics(
        )["ListTopicsResponse"]["ListTopicsResult"]["Topics"]

        for t in topics:
            if t["TopicArn"].split(':')[5] == topicName:
                self.loggingTopicArn = t["TopicArn"]
                break

        logging.Logger.__init__(self, level)
Ejemplo n.º 4
0
def sns_connection(region_name=settings.AWS_SNS_REGION_NAME):
    """Returns an SNSConnection that is already authenticated.

    us-west-2 is oregon, the region we use by default.

    @raises IndexError
    """
    region = filter(lambda x: x.name == region_name, sns.regions())[0]

    return sns.SNSConnection(
        aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
        aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
        region=region)
Ejemplo n.º 5
0
        def _send_result_activity(self, task):
            if task:
                subscription_data = json.loads(task)
            else:
                self.fail(reason=json.dumps({"reason", "Didn't receive any input!", "detail", "" }))
                return
            
            sns_client = sns.SNSConnection(aws_access_key_id=ACCESS, aws_secret_access_key=SECRET)
            results = "Thanks, you've successfully confirmed registration, and your workflow is complete!"
            # send the message via SNS
 
            sns_client.publish(topic=subscription_data["topic_arn"], message=results)
            self.complete(result=results)
            return True
Ejemplo n.º 6
0
lfilter = lambda func, xs: functools.reduce(
    lambda x, y: x + [y] if func(y) else x, xs, [])


def compose(*functions):
    return functools.reduce(lambda f, g: lambda x: f(g(x)), functions,
                            lambda x: x)


load_dotenv(find_dotenv())

logger = logging.getLogger()
logger.setLevel(logging.INFO)

sNSConnection = sns.SNSConnection(
    aws_access_key_id=os.environ['AWS_KEY'],
    aws_secret_access_key=os.environ['AWS_SECRET'],
    region=[r for r in sns.regions() if r.name == os.environ['AWS_REGION']][0])

s3Connection = S3Connection(aws_access_key_id=os.environ['AWS_KEY'],
                            aws_secret_access_key=os.environ['AWS_SECRET'])


def elapsed_lambda(d, percentiles=[50, 90, 95, 99]):
    series = pd.Series({
        'min': np.min(d['elapsed']),
        'max': np.max(d['elapsed']),
        'mean': np.mean(d['elapsed']),
        'std': np.std(d['elapsed'])
    })

    perc = lmap(lambda x: pd.Series({str(x): np.percentile(d['elapsed'], x)}),
Ejemplo n.º 7
0
def get_connection_sns():
    region = sns.connect_to_region(settings.AWS_REGION_NAME, aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
                                   aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY).region
    return sns.SNSConnection(aws_access_key_id=settings.AWS_ACCESS_KEY_ID, region=region,
                             aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY)