diff --git a/lib/query.py b/lib/query.py index 216fba28b9c9322a318def53ee9aa0660182e389..d0ffbeeb1e732b24a5f8d3292bf5d0c646063d43 100644 --- a/lib/query.py +++ b/lib/query.py @@ -7,6 +7,33 @@ from os.path import expanduser, exists import os import xmlrpc.client from configparser import ConfigParser +import re +import logging + + +def _hide_password(message): + url_regex = r'^(\w*://\w*:)\w*(@\w*\.\w*.\w*)' + return re.sub(url_regex, '\g<1>*****@\g<2>', message) + + +class LTAClientExistsAlready(Exception): + pass + + +class LTAClientError(Exception): + pass + + +def lta_on_error_print(func): + def wrapper(*args, **kwargs): + try: + return func(*args, **kwargs) + except xmlrpc.client.Fault as error: + logging.error('error sending request on LTA rpc with code %s: %s', error.faultCode, + _hide_password(error.faultString)) + raise LTAClientError() + + return wrapper def parse_config(path=None): @@ -109,6 +136,7 @@ def _call_idservice(source, userlabel=None): return response +@lta_on_error_print def create_unique_id(source, userlabel=None): """ Creates a new unique numeric identifier in the LTA catalog for the given source name. @@ -123,6 +151,7 @@ def create_unique_id(source, userlabel=None): return response.get('id') +@lta_on_error_print def get_unique_id(source, userlabel): """ Queries an existing numeric ID from the LTA catalog based on it's userlabel (which had @@ -137,9 +166,20 @@ def get_unique_id(source, userlabel): return response.get('id') +@lta_on_error_print def get_dataproduct_sip(projectname, dataproductid): - return client.GetSip(projectname, dataproductid).get("sip") + response = client.GetSip(projectname, dataproductid) + error = response.get('error') + if error: + raise LTAClientError(_hide_password(error)) + return response.get('sip') +@lta_on_error_print def get_dataproduct_ids(projectname, sasid): - return client.GetDataProductIDS(projectname, sasid).get("ids") + response = client.GetDataProductIDS(projectname, sasid) + error = response.get('error') + if error: + raise LTAClientError(_hide_password(error)) + + return response.get("ids")