Skip to content

Commit

Permalink
Merge pull request #19 from 4dn-dcic/0.3.3
Browse files Browse the repository at this point in the history
0.3.3
  • Loading branch information
KorayKirli authored Jul 16, 2018
2 parents 34991e5 + ac9db21 commit 2fd51a0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dcicutils/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Version information."""

# The following line *must* be the last in the module, exactly as formatted:
__version__ = "0.3.2"
__version__ = "0.3.3"
29 changes: 28 additions & 1 deletion dcicutils/ff_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
import copy
import boto3
from uuid import UUID
from dcicutils import s3_utils, submit_utils
from dcicutils import (
s3_utils,
submit_utils,
es_utils
)
import requests
# urlparse import differs between py2 and 3
if sys.version_info[0] < 3:
Expand Down Expand Up @@ -330,6 +334,29 @@ def delete_field(obj_id, del_field, key=None, ff_env=None):
return get_response_json(response)


def get_es_metadata(uuid, schema_name, es_client=None, key=None, ff_env=None):
"""
Given string item uuid and schema name (e.g. "file_fastq"), will return a
dictionary response of the full ES ecord for that item (or an empty
dictionary if the item doesn't exist/ is not indexed)
You can pass in an Elasticsearch client (initialized by create_es_client)
through the es_client param to save init time.
Same auth mechanism as the other metadata functions
"""
from elasticsearch.exceptions import TransportError
if es_client is None:
# need to know ES server location and item type
auth = get_authentication_with_server(key, ff_env)
health_res = authorized_request(auth['server'] + '/health', auth=auth, verb='GET')
es_url = get_response_json(health_res)['elasticsearch']
es_client = es_utils.create_es_client(es_url, use_aws_auth=True)
try:
es_res = es_client.get(index=schema_name, doc_type=schema_name, id=uuid)
except TransportError:
return {}
return es_res.get('_source', {})


#####################
# Utility functions #
#####################
Expand Down
26 changes: 26 additions & 0 deletions test/test_ff_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,32 @@ def get_search_generator(integrated_ff):
assert len(all_gen3_uuids) == len(all_gen3)


@pytest.mark.integrated
def test_get_es_metadata(integrated_ff):
from dcicutils import es_utils
# use this test biosource
test_item = '331111bc-8535-4448-903e-854af460b254'
res = ff_utils.get_es_metadata(test_item, 'biosource', key=integrated_ff['ff_key'])
assert res['uuid'] == test_item
assert res['item_type'] == 'biosource'
assert isinstance(res['embedded'], dict)
assert isinstance(res['links'], dict)

# you can also pass in your own elasticsearch client
# ugly here because we need to get it from health page
health_res = ff_utils.authorized_request(integrated_ff['ff_key']['server'] + '/health',
auth=integrated_ff['ff_key'])
es_url = ff_utils.get_response_json(health_res)['elasticsearch']
es_client = es_utils.create_es_client(es_url, use_aws_auth=True)
res2 = ff_utils.get_es_metadata(test_item, 'biosource', es_client=es_client,
key=integrated_ff['ff_key'])
assert res2['uuid'] == res['uuid']

# bad item returns empty dict
res = ff_utils.get_es_metadata('blahblah', 'biosource', key=integrated_ff['ff_key'])
assert res == {}


@pytest.mark.integrated
def test_fdn_connection(integrated_ff):
connection = ff_utils.fdn_connection(key={'default': integrated_ff['ff_key']})
Expand Down

0 comments on commit 2fd51a0

Please sign in to comment.