diff --git a/inventory_provider/routes/common.py b/inventory_provider/routes/common.py index 1d00f9318a56d1c9ff21b99fc3f216cfe660357f..7a84df255501f5d225279d696b71747325bb0348 100644 --- a/inventory_provider/routes/common.py +++ b/inventory_provider/routes/common.py @@ -1,12 +1,44 @@ import functools import logging +from collections import OrderedDict +import requests from flask import request, Response, current_app, g from inventory_provider.tasks import common as tasks_common logger = logging.getLogger(__name__) +def ims_hostname_decorator(field): + """ + Decorator to convert host names to various formats to try to match what is + found in IMS before executing the decorated function. + :param field: name of the field containing hostname + :return: result of decorated function + """ + + suffix = '.geant.net' + + def wrapper(func): + def inner(*args, **kwargs): + orig_val = kwargs[field] + values_to_try = [] + if orig_val.endswith(suffix): + values_to_try.append(orig_val[:-len(suffix)].upper()) + values_to_try.append(orig_val.upper()) + values_to_try.append(orig_val) + values_to_try.append(orig_val.lower()) + + for val in list(OrderedDict.fromkeys(values_to_try)): + kwargs[field] = val + res = func(*args, **kwargs) + if res.status_code != requests.codes.not_found: + return res + return res + return inner + return wrapper + + def get_current_redis(): if 'current_redis_db' in g: latch = tasks_common.get_latch(g.current_redis_db)