From 3fdc1c37e56761d2b1ab06f9c54de0af5423bdd0 Mon Sep 17 00:00:00 2001
From: Erik Reid <erik.reid@geant.org>
Date: Thu, 30 Jan 2020 16:32:51 +0100
Subject: [PATCH] added a prototype lg endpoint

---
 inventory_provider/__init__.py |  3 ++
 inventory_provider/db/opsdb.py | 58 ++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)

diff --git a/inventory_provider/__init__.py b/inventory_provider/__init__.py
index f96a6c43..f862a653 100644
--- a/inventory_provider/__init__.py
+++ b/inventory_provider/__init__.py
@@ -58,6 +58,9 @@ def create_app():
     from inventory_provider.routes import poller
     app.register_blueprint(poller.routes, url_prefix='/poller')
 
+    from inventory_provider.routes import lg
+    app.register_blueprint(lg.routes, url_prefix='/lg')
+
     if app.config.get('ENABLE_TESTING_ROUTES', False):
         from inventory_provider.routes import testing
         app.register_blueprint(testing.routes, url_prefix='/testing')
diff --git a/inventory_provider/db/opsdb.py b/inventory_provider/db/opsdb.py
index d73b94eb..41536113 100644
--- a/inventory_provider/db/opsdb.py
+++ b/inventory_provider/db/opsdb.py
@@ -297,6 +297,64 @@ ORDER BY FIELD(e.status, 'Operational', 'Installed')
         return list([_row2rsp(r) for r in rows])
 
 
+def lookup_lg_routers(connection):
+
+    # this is literally the definition of the view vlg_routers
+    # (cf. https://jira.software.geant.org/projects/LGR/issues/LGR-73)
+    # but with the city added and filtering on names instead of absid's
+    # for readability
+
+    INTERNAL_POP_NAMES = {
+        'Cambridge OC',
+        'DANTE Lab',
+        'Amsterdam GEANT Office'
+    }
+
+    query = """
+SELECT
+    e.name AS router_name,
+    p.name AS pop_name,
+    p.city AS pop_city,
+    p.abbreviation AS pop_abbreviation,
+    g.latitude AS pop_latitude,
+    g.longitude AS pop_longitude,
+    p.country_code AS pop_country_code,
+    g.country AS pop_country,
+    g.city AS pop_city
+FROM
+    opsdb.equipment e
+    LEFT JOIN opsdb.pop p ON p.absid = e.PTR_pop
+    LEFT JOIN opsdb.geocoding g ON g.absid = p.PTR_geocoding
+    LEFT JOIN opsdb.organisation o ON o.absid = e.PTR_owner
+WHERE
+    e.model LIKE 'mx%'
+    AND e.status = 'Operational'
+    AND o.name = 'DANTE / GEANT'
+    AND NOT (e.name REGEXP 'vpn-proxy|vrr|taas')
+    """
+
+    def _row2rsp(row):
+        print(row)
+        return {
+            'equipment-name': row['router_name'],
+            'type': 'INTERNAL' if row['pop_name'] in INTERNAL_POP_NAMES else 'CORE',
+            'pop': {
+                'name': row['pop_name'],
+                'city': row['pop_city'],
+                'country': row['pop_country'],
+                'country code': row['pop_country_code'],
+                'abbreviation': row['pop_abbreviation'],
+                'longitude': row['pop_longitude'],
+                'latitude': row['pop_latitude'],
+            }
+        }
+
+    with db.cursor(connection) as crs:
+        crs.execute(query)
+        rows = _convert_to_dict(crs)
+        return list([_row2rsp(r) for r in rows])
+
+
 def lookup_coriant_path(connection, equipment_name, card_id, port_number):
 
     base_query = """
-- 
GitLab