From 01f20fc7c035c4155f5b94da80433d4756b9b5e0 Mon Sep 17 00:00:00 2001
From: Robert Latta <robert.latta@geant.org>
Date: Wed, 5 May 2021 10:54:08 +0000
Subject: [PATCH] fixed carriers; added max-depth option

---
 inventory_provider/routes/testing.py | 110 ++++++++++++++-------------
 1 file changed, 58 insertions(+), 52 deletions(-)

diff --git a/inventory_provider/routes/testing.py b/inventory_provider/routes/testing.py
index ec782eb9..4cd035f5 100644
--- a/inventory_provider/routes/testing.py
+++ b/inventory_provider/routes/testing.py
@@ -122,64 +122,70 @@ def circuit_tree(root_identifier: str):
         strtobool(request.args.get('carriers', default='false', type=str))
     interface_ = \
         strtobool(request.args.get('interface', default='false', type=str))
-
+    max_depth = request.args.get('max-depth', default=0, type=int)
     if carriers:
         children_prop = 'carrier-circuits'
     else:
         children_prop = 'sub-circuits'
 
-        config = current_app.config["INVENTORY_PROVIDER_CONFIG"]
-        r = worker_common.get_current_redis(config)
-
-        def _get_childcircuit_tree_local(circuit_id):
-            circuit = r.get(f'ims:circuit_hierarchy:{circuit_id}')
-            if not circuit:
-                return None
-            circuit = json.loads(circuit.decode('utf-8'))[0]
-            _tree = [
-                f'{circuit["id"]} -- {circuit["name"]} -- '
-                f'prod: {circuit["product"]} -- '
-                f'spd: {circuit["speed"]} -- '
-                f'status: {circuit["status"]}'
-            ]
-
-            if circuit.get(children_prop, None):
-
-                children = []
-                for child_id in circuit[children_prop]:
-                    v = _get_childcircuit_tree_local(child_id)
-                    if v:
-                        children.append(v)
-                _tree.append(children)
-            else:
-                _tree.append([])
-            return _tree
+    config = current_app.config["INVENTORY_PROVIDER_CONFIG"]
+    r = worker_common.get_current_redis(config)
 
-        if interface_:
+    def _get_childcircuit_tree_local(circuit_id, depth):
+        circuit = r.get(f'ims:circuit_hierarchy:{circuit_id}')
+        if not circuit:
+            return None
+        circuit = json.loads(circuit.decode('utf-8'))[0]
+        _tree = [
+            f'{circuit["id"]} -- {circuit["name"]} -- '
+            f'prod: {circuit["product"]} -- '
+            f'spd: {circuit["speed"]} -- '
+            f'status: {circuit["status"]}'
+        ]
+
+        if circuit.get(children_prop, None):
+            if max_depth != 0 and depth >= max_depth:
+                _tree.append([['...', []]])
+                return _tree
+
+            children = []
+            for child_id in circuit[children_prop]:
+                depth += 1
+                v = _get_childcircuit_tree_local(child_id, depth)
+                if v:
+                    children.append(v)
+            _tree.append(children)
+        else:
+            _tree.append([])
+        return _tree
 
-            if_services = r.get(f'ims:interface_services:{root_identifier}')
-            if if_services:
-                logger.debug('1')
-                root_identifiers = [s['id'] for s in json.loads(if_services)]
-                children = []
-                for id_ in root_identifiers:
-                    children.append(_get_childcircuit_tree_local(id_))
+    if interface_:
 
-                tree = [root_identifier, children]
-            else:
-                return "Nothing found"
+        if_services = r.get(f'ims:interface_services:{root_identifier}')
+        if if_services:
+            root_identifiers = [s['id'] for s in json.loads(if_services)]
+            children = []
+            for id_ in root_identifiers:
+                children.append(_get_childcircuit_tree_local(id_, 1))
+
+            tree = [root_identifier, children]
         else:
-            try:
-                root_identifier = int(root_identifier)
-            except ValueError:
-                for k in r.scan_iter('ims:circuit_hierarchy:*', count=2000):
-                    ch = r.get(k.decode('utf-8'))
-                    details = json.loads(ch.decode('utf-8'))[0]
-                    if root_identifier.lower() == details['name'].lower():
-                        root_identifier = details['id']
-                        break
-                else:
-                    return f'No circuit found for: {root_identifier}'
-
-            tree = _get_childcircuit_tree_local(root_identifier)
-        return f'<pre>{format_tree(tree, format_node = itemgetter(0), get_children = itemgetter(1))}</pre>'
+            return "Nothing found"
+    else:
+        try:
+            root_identifier = int(root_identifier)
+        except ValueError:
+            for k in r.scan_iter('ims:circuit_hierarchy:*', count=2000):
+                ch = r.get(k.decode('utf-8'))
+                details = json.loads(ch.decode('utf-8'))[0]
+                if root_identifier.lower() == details['name'].lower():
+                    root_identifier = details['id']
+                    break
+            else:
+                return f'No circuit found for: {root_identifier}'
+        tree = _get_childcircuit_tree_local(root_identifier, 1)
+    f = format_tree(
+        tree,
+        format_node=itemgetter(0),
+        get_children=itemgetter(1))
+    return f'<pre>{f}</pre>'
-- 
GitLab