Exemplo n.º 1
0
def lambda_handler(event, context):
    formatter = logging.Formatter(
        '[%(levelname)s]\t%(asctime)s.%(msecs)dZ\t%(aws_request_id)s\t[%(module)s#%(funcName)s %(lineno)d]\t%(message)s'
    )

    logger = logging.getLogger()
    for handler in logger.handlers:
        handler.setFormatter(formatter)
        handler.setLevel(logging.DEBUG)

    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    try:
        corp = Corporation(DYNAMO_HOST, DYNAMO_PORT)
        req = RequestDecorator(event)
    except Exception as e:
        raise e

    corporation_id = req.get_identity_id()
    #username = req.get_username()
    if corporation_id:
        corp.set_corporation_id(corporation_id)

    path_params = req.get_path_params()
    if "user_id" in path_params:
        user_id = path_params["user_id"]

    status_code = 200
    ret = ""
    # /corps
    try:
        if req.get_method() == "GET":
            # /corporation
            if "users" in req.get_path():
                ret = corp.list_users()
            else:
                ret = corp.show()

        elif req.get_method() == "POST":
            body = req.get_body()
            if corporation_id:
                body.update({"corporation_id": corporation_id})
                ret = corp.create_user(**body)

        #elif req.get_method() == "PUT":
        #  name = req.get_body()["name"]
        #  ret = corp.update_corp(name, start_on, complete_on)

        elif req.get_method() == "DELETE":
            if "/corporation/users/" in req.get_path() and "user_id" in locals(
            ):
                ret = corp.delete_user(user_id)

        elif req.get_method() == "OPTIONS":
            ret = []
        logger.info("processing successfully. return value: {}".format(ret))

    except ActionDeniedError as e:
        status_code = 403
        ret = e
        logger.error("permission denied.")
        logger.exception(e)
    except Exception as e:
        status_code = 400
        ret = e
        logger.error("invalid request?? processing failed...")
        logger.exception(e)

    return make_response(status_code=status_code, body=ret)
Exemplo n.º 2
0
def lambda_handler(event, context):
    formatter = logging.Formatter(
        '[%(levelname)s]\t%(asctime)s.%(msecs)dZ\t%(aws_request_id)s\t[%(module)s#%(funcName)s %(lineno)d]\t%(message)s'
    )

    logger = logging.getLogger()
    for handler in logger.handlers:
        handler.setFormatter(formatter)
        handler.setLevel(logging.DEBUG)

    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    try:
        work = Work(DYNAMO_HOST, DYNAMO_PORT)
        req = RequestDecorator(event)
    except Exception as e:
        raise e

    user_id = req.get_identity_id()
    username = req.get_username()
    if user_id and username:
        work.set_user_id(user_id)

    path_params = req.get_path_params()
    if "project_id" in path_params:
        project_id = path_params["project_id"]
        work.set_project_id(project_id)
        logger.info("requested project_id: {}".format(project_id))

    if "place_id" in path_params:
        place_id = path_params["place_id"]
        work.set_place_id(place_id)
        logger.info("requested place_id: {}".format(place_id))

    status_code = 200
    ret = ""

    try:
        if req.get_method() == "GET":
            if "place_id" in locals():
                if "children" in req.get_path():
                    # /projects/{project_id}/places/{place_id}/children
                    ret = work.list_children(place_id)
                else:
                    # /projects/{project_id}/places/{place_id}
                    ret = work.show_place(place_id)
            # /projects/{project_id}/places
            else:
                ret = work.list_children(project_id)

        elif req.get_method() == "POST":
            name = req.get_body()["name"]
            # /projects/{project_id}/places/{place_id}
            if "place_id" in locals():
                ret = work.create_place(name, place_id)

            # /projects/{project_id}/places
            else:
                ret = work.create_place(name)

        # /projects/{project_id}/places/{place_id}
        elif req.get_method() == "PUT":
            name = req.get_body()["name"]
            ret = work.update_place(name)

        # /projects/{project_id}/places/{place_id}
        elif req.get_method() == "DELETE":
            ret = work.delete_place(place_id)

        elif req.get_method() == "OPTIONS":
            ret = []
        logger.info("processing successfully. return value: {}".format(ret))

    except ActionDeniedError as e:
        status_code = 403
        ret = e
        logger.error("permission denied.")
        logger.exception(e)
    except Exception as e:
        status_code = 400
        ret = e
        logger.error("invalid request?? processing failed...")
        logger.exception(e)

    return make_response(status_code=status_code, body=ret)
Exemplo n.º 3
0
def lambda_handler(event, context):
    formatter = logging.Formatter(
        '[%(levelname)s]\t%(asctime)s.%(msecs)dZ\t%(aws_request_id)s\t[%(module)s#%(funcName)s %(lineno)d]\t%(message)s'
    )

    logger = logging.getLogger()
    for handler in logger.handlers:
        handler.setFormatter(formatter)
        handler.setLevel(logging.DEBUG)

    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    try:
        project = Project(DYNAMO_HOST, DYNAMO_PORT)
        req = RequestDecorator(event)
    except Exception as e:
        raise e

    user_id = req.get_identity_id()
    username = req.get_username()
    if user_id and username:
        project.set_user_id(user_id)

    path_params = req.get_path_params()
    project_id = None
    if "project_id" in path_params:
        project_id = path_params["project_id"]
        project.set_project_id(project_id)
        logger.info("requested project_id: {}".format(project_id))

    status_code = 200
    ret = ""
    # /projects
    try:
        if req.get_method() == "GET":
            # /projects/xxxx-xxxx-xxxx-xxxx
            if project_id:
                ret = project.show_project()

            else:
                ret = project.list_projects()

        # /projects
        elif req.get_method() == "POST":
            if "import" in req.get_path():
                csv = req.get_body()["csv"]
                ret = project.import_csv(csv, base64enc=True)

            else:
                name = req.get_body()["name"]
                start_on = req.get_body()["start_on"]
                complete_on = req.get_body()["complete_on"]
                ret = project.create_project(name, start_on, complete_on)

        elif req.get_method() == "PUT":
            name = req.get_body()["name"]
            start_on = req.get_body()["start_on"]
            complete_on = req.get_body()["complete_on"]
            ret = project.update_project(name, start_on, complete_on)

        elif req.get_method() == "DELETE":
            ret = project.delete_project()

        elif req.get_method() == "OPTIONS":
            ret = []
        logger.info("processing successfully. return value: {}".format(ret))

    except ActionDeniedError as e:
        status_code = 403
        ret = e
        logger.error("permission denied.")
        logger.exception(e)
    except Exception as e:
        status_code = 400
        ret = e
        logger.error("invalid request?? processing failed...")
        logger.exception(e)

    return make_response(status_code=status_code, body=ret)