Beispiel #1
0
def add_task(event, context):
    logging.info('event: {}'.format(event))

    try:
        body = get_body(event)
    except json.JSONDecodeError:
        return {
            'statusCode': 400,
            'body': json.dumps({'error': 'Bad parameter(s) in request'})
        }

    try:
        task = TaskModel(scheduled=body['scheduled'],
                         task_type=body['task_type'],
                         invoke_method=body['invoke_method'],
                         call_arn_url=body['call_arn_url'],
                         parameters=body['parameters'],
                         data=body['data'])

        repo = TaskRepository()
        repo.add_task(task)

        return {'statusCode': 200, 'body': json.dumps(task.__dict__)}
    except Exception as e:
        logging.error(e)
        return {
            'statusCode': 500,
            'body': json.dumps({'error': 'Internal Error: {}'.format(e)})
        }
    def test_sns_task_executer(self):
        task_id = uuid.uuid4()
        task = TaskModel(
            task_id,
            scheduled=datetime.datetime.now(),
            task_type='send_gmail',
            invoke_method='SNS',
            call_arn_url='arn:aws:sns:us-east-1:00000000:test',
            data={},
        )
        repo = TaskRepository()
        repo.add_task(task)

        passed = True
        try:
            task_executer(event={}, context={})
        except:
            passed = False

        self.assertTrue(passed, 'Execute SNS task with CloudWatch failed.')
    def test_rest_task_executer(self):
        task_id = uuid.uuid4()
        task = TaskModel(
            task_id,
            scheduled=datetime.datetime.now(),
            task_type='send_gmail',
            invoke_method='REST',
            call_arn_url='https://serverless.com',
            data={},
        )
        repo = TaskRepository()
        repo.add_task(task)

        passed = True
        try:
            task_executer(event={}, context={})
        except:
            passed = False

        self.assertTrue(passed, 'Execute REST task with CloudWatch failed.')
Beispiel #4
0
def handler(event, context):
    logging.info('event: {}'.format(event))

    message = json.loads(event['Records'][0]['Sns']['Message'])
    method = message['method']
    body = message['body']

    repo = TaskRepository()
    if message['method'] == 'ADD':
        task = TaskModel(
            task_id=uuid.uuid4(),
            scheduled=datetime.datetime.strptime(body['scheduled'], '%Y-%m-%d %H:%M:%S'),
            task_type=body['task_type'],
            invoke_method=body['invoke_method'],
            call_arn_url=body['call_arn_url'],
            parameters=body['parameters'],
            data=body['data'])
        repo.add_task(task)

    elif method == 'UPDATE':
        task = repo.get_task_by_task_id(body['task_id'])

        if task is not None:
            for key, value in body.items():
                setattr(task, key, value)
            repo.update_task(task)
        else:
            logging.error('Entry not found')

    elif method == 'DELETE':
        task = repo.get_task_by_task_id(body['id'])
        if task is not None:
            repo.delete_task_by_task_id(task.task_id)
        else:
            logging.error('Entry not found')

    else:
        logging.error('Unknown method')
Beispiel #5
0
    def test_task_manager_delete_task(self):
        task_id = uuid.uuid4()
        scheduled = datetime.datetime.utcnow()
        new_task = TaskModel(task_id, scheduled)

        repo = TaskRepository()
        created_task = repo.add_task(new_task)

        message = {'method': 'DELETE', 'body': {'id': created_task.task_id}}
        event = {'Records': [{'Sns': {'Message': json.dumps(message)}}]}

        passed = True
        try:
            task_manager(event, {})
        except:
            passed = False

        self.assertTrue(passed, 'Delete task through SNS failed.')
Beispiel #6
0
    def test_task_manager_update_task(self):
        task_id = uuid.uuid4()
        scheduled = datetime.datetime.utcnow()
        new_task = TaskModel(task_id, scheduled)
        repo = TaskRepository()
        created_task = repo.add_task(new_task)

        created_task.call_arn_url = 'https://google.com'
        message = {'method': 'UPDATE', 'body': created_task.__dict__}
        event = {'Records': [{'Sns': {'Message': json.dumps(message)}}]}

        passed = True
        try:
            task_manager(event, {})
        except:
            passed = False

        self.assertTrue(passed, 'Update task through SNS failed.')