예제 #1
0
def update(event, context):
    """Update item in the collection."""

    try:

        # Determine if required env var for region is present.
        val.check_region()

        # Determine if required env var for DynamoDB table is present.
        val.check_dynamodb()

        # Check for the url {id}.
        note_id = val.check_id(event)

        # Determine if required property body is present.
        val.check_body(event)

        # Attempt to parse JSON.
        data = val.check_json(event)

        # Determine if required properties are present.
        val.check_props(data)

        # Set up resource and environment. This is where we keep
        # *aaS provider resources away from biz logic.
        region = os.environ["AWS_DEFAULT_REGION"]
        table = os.environ["DYNAMODB_TABLE"]

        # Determine which DynamoDB host we need (local/remote)?
        host = val.check_dynamodb_host()
        conn = boto3.resource("dynamodb", region, endpoint_url=host)
        conn_table = conn.Table(table)

        # Build our model and update.
        note = NoteModel(conn_table)
        item = note.update(note_id, data)

        logger.info("Note updated: {}".format(item))
        return respond(200, item)

    except ex.AwsRegionNotSetException as exc:
        return respond(500, {"error": str(exc)})

    except ex.DynamoDbTableNotSetException as exc:
        return respond(500, {"error": str(exc)})

    except ex.RequestUrlIdNotSetException as exc:
        return respond(400, {"error": str(exc)})

    except ex.RequestBodyNotSetException as exc:
        return respond(400, {"error": str(exc)})

    except ex.RequestBodyNotJsonException as exc:
        return respond(400, {"error": str(exc)})

    except ex.RequiredPropertiesNotSetException as exc:
        return respond(400, {"error": str(exc)})

    except Exception as exc:
        return respond(500, {"error": str(exc)})
예제 #2
0
def search_by_notebook(event, context):
    """Return the collection of items based on query."""

    try:

        # Determine if required env var for region is present.
        val.check_region()

        # Determine if required env var for DynamoDB table is present.
        val.check_dynamodb()

        # Check for the url {id}.
        notebook = val.check_id(event)

        # Set up resource and environment. This is where we keep
        # *aaS provider resources away from biz logic.
        region = os.environ["AWS_DEFAULT_REGION"]
        table = os.environ["DYNAMODB_TABLE"]

        # Determine which DynamoDB host we need (local/remote)?
        host = val.check_dynamodb_host()
        conn = boto3.resource("dynamodb", region, endpoint_url=host)
        conn_table = conn.Table(table)

        # Build our model and read.
        note = NoteModel(conn_table)
        items = note.search_by_notebook(notebook)

        logger.info("Notes for notebook found: {} [{}]".format(
            notebook, len(items)))
        return respond(200, items)

    except ex.AwsRegionNotSetException as exc:
        return respond(500, {"error": str(exc)})

    except ex.DynamoDbTableNotSetException as exc:
        return respond(500, {"error": str(exc)})

    except ex.RequestUrlIdNotSetException as exc:
        return respond(400, {"error": str(exc)})

    except Exception as exc:
        return respond(500, {"error": str(exc)})
예제 #3
0
def test_local_host_returned_when_env_var_dynamodb_host_not_found(monkeypatch, config):
    host = val.check_dynamodb_host()

    assert host == config["aws"]["dynamodb"]["localHost"]
예제 #4
0
def test_remote_host_returned_when_env_var_dynamodb_host_found(monkeypatch, config):
    monkeypatch.setenv("DYNAMODB_HOST", "some remote host")
    host = val.check_dynamodb_host()

    assert host != config["aws"]["dynamodb"]["localHost"]