Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc/prefix #255

Merged
merged 2 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ The `type` tells Indexd which client to use for that external service. In this c

Indexd itself can be configured to append a prefix to the typical UUID in order to aide in the distributed resolution capabilities mentioned above. Specifically, we can add a prefix such as `dg.4GH5/` which may represent one instance of Indexd. For distributed resolution purposes, we can then create `hints` that let the central resolver know where to go when it receives a GUID with a prefix of `dg.4GH5/`.

The prefix that a given Indexd instance uses is specified in the `DEFAULT_PREFIX` configuration in the settings file. In order to ensure that this gets used and aliases get created, specify `PREPEND_PREFIX` to `True` and `ADD_PREFIX_ALIAS` to `True` as well.
The prefix that a given Indexd instance uses is specified in the `DEFAULT_PREFIX` configuration in the settings file. In order to ensure that this gets used, set `PREPEND_PREFIX` to `True`. Note that the prefix will only be prepended to GUIDs generated for new records that are indexed _without_ providing a GUID.

The `ADD_PREFIX_ALIAS` configuration represents a different way of using the prefix: if set to `True`, instead of prepending the prefix to the GUID, indexd will create an alias of the form `<prefix><GUID>` for this record. Note that you should NOT set both `ADD_PREFIX_ALIAS` and `PREPEND_PREFIX` to `True`, or aliases will be created as `<prefix><prefix><GUID>`.

## Use Cases For Indexing Data

Expand Down
12 changes: 6 additions & 6 deletions indexd/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ def app_init(app, settings=None):
app.register_blueprint(index_urls_blueprint, url_prefix="/_query/urls")


def get_app():
def get_app(settings=None):
app = flask.Flask("indexd")

if "INDEXD_SETTINGS" in os.environ:
sys.path.append(os.environ["INDEXD_SETTINGS"])

settings = None
try:
from local_settings import settings
except ImportError:
pass
if not settings:
try:
from local_settings import settings
except ImportError:
pass

app_init(app, settings)

Expand Down
8 changes: 7 additions & 1 deletion indexd/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@
CONFIG["JSONIFY_PRETTYPRINT_REGULAR"] = False
AUTO_MIGRATE = True

# - DEFAULT_PREFIX: prefix to be prepended.
# - PREPEND_PREFIX: the prefix is preprended to the generated GUID when a
# new record is created WITHOUT a provided GUID.
# - ADD_PREFIX_ALIAS: aliases are created for new records - "<PREFIX><GUID>".
# Do NOT set both ADD_PREFIX_ALIAS and PREPEND_PREFIX to True, or aliases
# will be created as "<PREFIX><PREFIX><GUID>".
CONFIG["INDEX"] = {
"driver": SQLAlchemyIndexDriver(
"sqlite:///index.sq3",
auto_migrate=AUTO_MIGRATE,
echo=True,
index_config={
"DEFAULT_PREFIX": "testprefix:",
"ADD_PREFIX_ALIAS": True,
"PREPEND_PREFIX": True,
"ADD_PREFIX_ALIAS": False,
},
)
}
Expand Down
8 changes: 5 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
import base64
import pytest

import importlib

# indexd_server and indexd_client is needed as fixtures
from cdisutilstest.code.conftest import indexd_server, indexd_client # noqa
from cdisutilstest.code.indexd_fixture import clear_database


from indexd import auth
import importlib
from tests import default_test_settings


@pytest.fixture
Expand All @@ -18,7 +19,8 @@ def app():
from indexd import default_settings

importlib.reload(default_settings)
yield get_app()

yield get_app(default_test_settings.settings)
try:
clear_database()

Expand Down
22 changes: 22 additions & 0 deletions tests/default_test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from indexd.default_settings import *
from indexd.index.drivers.alchemy import SQLAlchemyIndexDriver

# override the default settings for INDEX because we want to test
# both PREPEND_PREFIX and ADD_PREFIX_ALIAS, which should not both
# be set to True in production environments
CONFIG["INDEX"] = {
"driver": SQLAlchemyIndexDriver(
"sqlite:///index.sq3",
auto_migrate=True,
echo=True,
index_config={
"DEFAULT_PREFIX": "testprefix:",
"PREPEND_PREFIX": True,
"ADD_PREFIX_ALIAS": True,
},
)
}

settings = {"config": CONFIG, "auth": AUTH}

settings["config"]["TEST_DB"] = "postgres://postgres@localhost/test_migration_db"
1 change: 0 additions & 1 deletion tests/test_aliases_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import json
import urllib.parse

from indexd import get_app

# Test fixtures and helper functions
# =============================================
Expand Down
22 changes: 22 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,13 @@ def test_get_id(client, user):


def test_index_prepend_prefix(client, user):
"""
For index_config =
{
"DEFAULT_PREFIX": "testprefix:",
"PREPEND_PREFIX": True
}
"""
data = get_doc()

res_1 = client.post("/index/", json=data, headers=user)
Expand Down Expand Up @@ -1129,6 +1136,21 @@ def test_index_get_global_endpoint(client, user):
assert rec["urls"] == data["urls"]
assert rec["hashes"]["md5"] == data["hashes"]["md5"]


def test_index_add_prefix_alias(client, user):
"""
For index_config =
{
"DEFAULT_PREFIX": "testprefix:",
"ADD_PREFIX_ALIAS": True
}
"""
data = get_doc()

res = client.post("/index/", json=data, headers=user)
assert res.status_code == 200
rec = res.json

res_2 = client.get("/testprefix:" + rec["did"])
assert res_2.status_code == 200
rec_2 = res_2.json
Expand Down
2 changes: 1 addition & 1 deletion tests/test_schema_migration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import uuid
from tests.test_settings import settings
from tests.default_test_settings import settings
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
import sqlite3
Expand Down
3 changes: 0 additions & 3 deletions tests/test_settings.py

This file was deleted.