From ef9952fc389b185c1848aa435244b375fa616a85 Mon Sep 17 00:00:00 2001
From: Robert Latta <robert.latta@geant.org>
Date: Tue, 13 Apr 2021 10:22:03 +0000
Subject: [PATCH] added UNKNOWN location if equipment not found

---
 inventory_provider/db/ims_data.py  | 13 +++++++++++++
 inventory_provider/tasks/worker.py | 16 ++++++++++++++--
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/inventory_provider/db/ims_data.py b/inventory_provider/db/ims_data.py
index df291327..83c90fa4 100644
--- a/inventory_provider/db/ims_data.py
+++ b/inventory_provider/db/ims_data.py
@@ -274,6 +274,19 @@ def get_node_locations(ds: IMS):
                 }
             })
 
+    yield ('UNKNOWN_LOC', {
+        'equipment-name': 'UNKNOWN',
+        'status': 'unknown',
+        'pop': {
+            'name': 'UNKNOWN',
+            'city': 'UNKNOWN',
+            'country': 'UNKNOWN',
+            'abbreviation': 'UNKNOWN',
+            'longitude': 0,
+            'latitude': 0,
+        }
+    })
+
 
 # End of Dashboard V3 stuff
 
diff --git a/inventory_provider/tasks/worker.py b/inventory_provider/tasks/worker.py
index 0b3b97f8..cb946230 100644
--- a/inventory_provider/tasks/worker.py
+++ b/inventory_provider/tasks/worker.py
@@ -472,6 +472,7 @@ def update_circuit_hierarchy_and_port_id_services(self, use_current=False):
 
     locations = {k: v for k, v in ims_data.get_node_locations(ds1)}
     tls_names = list(ims_data.get_service_types(ds1))
+    customer_contacts = ims_data.get_customer_service_emails(ds1)
     hierarchy = None
     port_id_details = defaultdict(list)
     port_id_services = defaultdict(list)
@@ -572,7 +573,11 @@ def update_circuit_hierarchy_and_port_id_services(self, use_current=False):
                     'name': c['name'],
                     'status': c['status'],
                     'circuit_type': 'service',
-                    'project': c['project']
+                    'project': c['project'],
+                    'contacts': customer_contacts.get(
+                        circ['customerid'],
+                        []
+                    )
                 }
             elif c['sub-circuits']:
                 for sub in c['sub-circuits']:
@@ -583,7 +588,14 @@ def update_circuit_hierarchy_and_port_id_services(self, use_current=False):
 
     def _format_service(s):
         pd_a = port_id_details[s['port_a_id']][0]
-        loc_a = locations[pd_a['equipment_name']]['pop']
+        location_a = locations.get(pd_a['equipment_name'], None)
+        if location_a:
+            loc_a = location_a['pop']
+        else:
+            loc_a = locations['UNKNOWN_LOC']['pop']
+            logger.warning(
+                f'Unable to find location for {pd_a["equipment_name"]} - '
+                f'Service ID {s["id"]}')
         s['pop_name'] = loc_a['name']
         s['pop_abbreviation'] = loc_a['abbreviation']
         s['equipment'] = pd_a['equipment_name']
-- 
GitLab