From f82c5fa16e71762ca91abab56714158463aeadb9 Mon Sep 17 00:00:00 2001
From: Robert Latta <robert.latta@geant.org>
Date: Thu, 30 Jul 2020 12:16:46 +0000
Subject: [PATCH] added decorator to help with node names in IMS

---
 inventory_provider/routes/common.py | 32 +++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/inventory_provider/routes/common.py b/inventory_provider/routes/common.py
index 1d00f931..7a84df25 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)
-- 
GitLab