Skip to content

Commit

Permalink
Create separate config
Browse files Browse the repository at this point in the history
  • Loading branch information
BinamB committed Apr 12, 2023
1 parent e2f6f96 commit 0836cfa
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 103 deletions.
6 changes: 3 additions & 3 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
"filename": "tests/default_test_settings.py",
"hashed_secret": "afc848c316af1a89d49826c5ae9d00ed769415f3",
"is_verified": false,
"line_number": 26
"line_number": 41
}
],
"tests/postgres/migrations/test_15f2e9345ade_create_tables.py": [
Expand Down Expand Up @@ -391,9 +391,9 @@
"filename": "tests/test_drs.py",
"hashed_secret": "5666c088b494f26cd8f63ace013992f5fc391ce0",
"is_verified": false,
"line_number": 31
"line_number": 32
}
]
},
"generated_at": "2023-04-10T19:43:22Z"
"generated_at": "2023-04-12T20:10:41Z"
}
4 changes: 4 additions & 0 deletions bin/indexd_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def load_json(file_name):
if dist:
CONFIG["DIST"] = json.loads(dist)

drs_service_info = environ.get("DRS_SERVICE_INFO", None)
if drs_service_info:
CONFIG["DRS_SERVICE_INFO"] = json.loads(drs_service_info)

CONFIG["INDEX"] = {
"driver": SQLAlchemyIndexDriver(
"postgresql+psycopg2://{usr}:{psw}@{pghost}:{pgport}/{db}".format(
Expand Down
4 changes: 4 additions & 0 deletions deployment/Secrets/indexd_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def load_json(file_name):
if dist:
CONFIG["DIST"] = json.loads(dist)

drs_service_info = environ.get("DRS_SERVICE_INFO", None)
if drs_service_info:
CONFIG["DRS_SERVICE_INFO"] = json.loads(drs_service_info)

CONFIG["INDEX"] = {
"driver": SQLAlchemyIndexDriver(
"postgresql+psycopg2://{usr}:{psw}@{pghost}:{pgport}/{db}".format(
Expand Down
33 changes: 17 additions & 16 deletions indexd/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,25 @@
"hints": [],
"type": "dos",
},
{
"name": "DRS System",
"host": "https://example.com/api/ga4gh/drs/v1/",
"hints": [],
"type": {
"group": "org.ga4gh",
"artifact": "drs",
"version": "1.0.0",
},
"version": "1.0.0",
"id": "com.example",
"organization": {
"name": "Gen3",
"url": "http://example.com/",
},
},
]

CONFIG["DRS_SERVICE_INFO"] = {
"name": "DRS System",
"host": "https://example.com/api/ga4gh/drs/v1/",
"hints": [],
"type": {
"group": "org.ga4gh",
"artifact": "drs",
"version": "1.0.3",
},
"version": "1.0.3",
"id": "com.example",
"organization": {
"name": "CTDS",
"url": "http://example.com/",
},
}

AUTH = SQLAlchemyAuthDriver("sqlite:///auth.sq3")

settings = {"config": CONFIG, "auth": AUTH}
66 changes: 33 additions & 33 deletions indexd/drs/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,60 @@
from indexd.errors import UserError
from indexd.index.errors import NoRecordFound as IndexNoRecordFound
from indexd.errors import IndexdUnexpectedError
from indexd.utils import drs_service_info_id_url_reversal
from indexd.utils import reverse_url

blueprint = flask.Blueprint("drs", __name__)

blueprint.config = dict()
blueprint.index_driver = None
blueprint.dist = []
blueprint.service_info = {}


@blueprint.route("/ga4gh/drs/v1/service-info", methods=["GET"])
def get_drs_service_info():
"""
Returns DRS compliant service information
"""
drs_dist = {}

reverse_domain_name = drs_service_info_id_url_reversal(url=os.environ["HOSTNAME"])

if len(blueprint.dist) > 0:
# Check to see if the information is of type drs. If not, use the available information to return DRS compliant service information
for dist in blueprint.dist:
if (
"type" in dist
and isinstance(dist["type"], dict)
and "artifact" in dist["type"]
and dist["type"]["artifact"] == "drs"
):
drs_dist = dist
if drs_dist == {}:
drs_dist = blueprint.dist[0]

reverse_domain_name = reverse_url(url=os.environ["HOSTNAME"])

ret = {
"id": drs_dist.get("id", reverse_domain_name),
"name": drs_dist.get("name", "DRS System"),
"version": drs_dist.get("version", "1.0.0"),
"id": blueprint.service_info.get("id", reverse_domain_name),
"name": blueprint.service_info.get("name", "DRS System"),
"version": blueprint.service_info.get("version", "1.0.3"),
"type": {
"group": drs_dist.get("group", "org.ga4gh"),
"artifact": drs_dist.get("artifact", "drs"),
},
"organization": {
"name": "Gen3",
"group": blueprint.service_info.get("group", "org.ga4gh"),
"artifact": blueprint.service_info.get("artifact", "drs"),
},
}

if "type" in drs_dist and isinstance(drs_dist["type"], dict):
ret["type"]["version"] = drs_dist.get("type").get("version", "1.0.0")
ret["organization"] = {}

if "type" in blueprint.service_info and isinstance(
blueprint.service_info["type"], dict
):
ret["type"]["version"] = blueprint.service_info.get("type").get(
"version", "1.0.3"
)
else:
ret["type"]["version"] = "1.0.0"
ret["type"]["version"] = "1.0.3"

if "organization" in drs_dist and "url" in drs_dist["organization"]:
ret["organization"]["url"] = drs_dist["organization"]["url"]
if (
"organization" in blueprint.service_info
and "url" in blueprint.service_info["organization"]
):
ret["organization"]["url"] = blueprint.service_info["organization"]["url"]
else:
ret["organization"]["url"] = "https://" + os.environ["HOSTNAME"]

if (
"organization" in blueprint.service_info
and "name" in blueprint.service_info["organization"]
):
ret["organization"]["name"] = blueprint.service_info["organization"]["name"]
else:
ret["organization"]["name"] = "CTDS"

return flask.jsonify(ret), 200


Expand Down Expand Up @@ -384,5 +384,5 @@ def get_config(setup_state):

@blueprint.record
def get_config(setup_state):
if "DIST" in setup_state.app.config:
blueprint.dist = setup_state.app.config["DIST"]
if "DRS_SERVICE_INFO" in setup_state.app.config:
blueprint.service_info = setup_state.app.config["DRS_SERVICE_INFO"]
2 changes: 1 addition & 1 deletion indexd/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def migrate_database(driver, migrate_functions, current_schema_version, model):
s.add(schema_version)


def drs_service_info_id_url_reversal(url):
def reverse_url(url):
"""
Reverse the domain name for drs service-info IDs
Args:
Expand Down
15 changes: 15 additions & 0 deletions tests/default_test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@
},
]

CONFIG["DRS_SERVICE_INFO"] = {
"name": "DRS System",
"type": {
"group": "org.ga4gh",
"artifact": "drs",
"version": "1.0.3",
},
"host": "https://fictitious-commons.io/",
"version": "1.0.3",
"organization": {
"name": "CTDS",
"url": "https://fictitious-commons.io",
},
}

os.environ["PRESIGNED_FENCE_URL"] = "https://fictitious-commons.io/"
os.environ["HOSTNAME"] = "fictitious-commons.io"
settings = {"config": CONFIG, "auth": AUTH}
Expand Down
61 changes: 11 additions & 50 deletions tests/test_drs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import flask
import json
import tests.conftest
import requests
Expand Down Expand Up @@ -157,74 +158,34 @@ def test_get_drs_with_encoded_slash(client, user):
assert rec_2["self_uri"] == "drs://testprefix:" + rec_1["did"].split(":")[1]


def test_drs_service_info_without_drs_block_in_config(client):
"""
Test that the non-drs related, defualt distributions convert to drs service info friendly format
"""
expected_info = {
"id": "io.fictitious-commons",
"name": "testStage",
"type": {
"group": "org.ga4gh",
"artifact": "drs",
"version": "1.0.0",
},
"organization": {
"name": "Gen3",
"url": "https://fictitious-commons.io",
},
"version": "1.0.0",
}

res = client.get("/ga4gh/drs/v1/service-info")

assert res.status_code == 200
assert res.json == expected_info


def test_drs_service_info_endpoint(client):
"""
Test drs service endpoint with drs service info friendly distribution information
"""
app = flask.Flask(__name__)

expected_info = {
"id": "io.fictitious-commons",
"name": "DRS System",
"type": {
"group": "org.ga4gh",
"artifact": "drs",
"version": "1.0.0",
"version": "1.0.3",
},
"version": "1.3.0",
"version": "1.0.3",
"organization": {
"name": "Gen3",
"name": "CTDS",
"url": "https://fictitious-commons.io",
},
}

settings["config"]["DIST"].append(
{
"name": "DRS System",
"type": {
"group": "org.ga4gh",
"artifact": "drs",
"version": "1.0.0",
},
"host": "https://fictitious-commons.io/",
"version": "1.3.0",
"organization": {
"name": "Gen3",
"url": "https://fictitious-commons.io",
},
}
)

res = client.get("/ga4gh/drs/v1/service-info")

assert res.status_code == 200
assert res.json == expected_info


def test_drs_service_info_no_dist_in_config(client):
def test_drs_service_info_no_information_configured(client):
"""
Test drs service info endpoint when dist is not configured in the indexd config file
"""
Expand All @@ -234,16 +195,16 @@ def test_drs_service_info_no_dist_in_config(client):
"type": {
"group": "org.ga4gh",
"artifact": "drs",
"version": "1.0.0",
"version": "1.0.3",
},
"version": "1.0.0",
"version": "1.0.3",
"organization": {
"name": "Gen3",
"name": "CTDS",
"url": "https://fictitious-commons.io",
},
}

settings["config"]["DIST"].clear()
settings["config"]["DRS_SERVICE_INFO"].clear()

res = client.get("/ga4gh/drs/v1/service-info")

Expand Down

0 comments on commit 0836cfa

Please sign in to comment.