From bf6725b8c1707dff28bbdb06c6819034fb03077b Mon Sep 17 00:00:00 2001 From: Robert Latta <robert.latta@geant.org> Date: Wed, 24 Mar 2021 11:04:27 +0000 Subject: [PATCH] allowed search by circuit id or name --- circuit_tree.py | 135 ++++++++++++++++++++++++++++-------------------- 1 file changed, 79 insertions(+), 56 deletions(-) diff --git a/circuit_tree.py b/circuit_tree.py index 1d392e17..06e262e9 100644 --- a/circuit_tree.py +++ b/circuit_tree.py @@ -23,8 +23,8 @@ NAV_PROPS = [ @click.command() @click.option('-c', 'carriers', is_flag=True) @click.option('-l', 'local', is_flag=True) -@click.argument('root_circuit_id', type=click.INT, required=True) -def cli(carriers, local, root_circuit_id): +@click.argument('root_circuit_identifier', type=click.STRING, required=True) +def cli(carriers, local, root_circuit_identifier): if local: if carriers: children_prop = 'carrier-circuits' @@ -40,62 +40,85 @@ def cli(carriers, local, root_circuit_id): childid = 'subcircuitid' NAV_PROPS.append(CIRCUIT_PROPERTIES['SubCircuits']) - def _get_childcircuit_tree(circuit_id): - circuit = ds.get_entity_by_id( - 'circuit', circuit_id, navigation_properties=NAV_PROPS) - # print(circuit["id"]) - _tree = [ - f'{circuit["id"]} -- {circuit["name"]} -- ' - f'prod: {circuit["product"]["name"]} -- ' - f'spd: {circuit["speed"]["name"]} -- ' - f'status: {InventoryStatus(circuit["inventorystatusid"]).name}' - ] - - if circuit[children_prop]: - - children = [] - for child_circuit in circuit[children_prop]: - children.append(_get_childcircuit_tree( - child_circuit[childid] - )) - _tree.append(children) - else: - _tree.append([]) - return _tree - - def _get_childcircuit_tree_local(circuit_id, r): - circuit = r.get(f'ims:circuit_hierarchy:{circuit_id}') - if not circuit: - return None - circuit = json.loads(circuit)[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, r) - if v: - children.append(v) - _tree.append(children) - else: - _tree.append([]) - return _tree - if local: - tree = _get_childcircuit_tree_local( - root_circuit_id, - redis.StrictRedis( - host='localhost', db=0, decode_responses=True, - encoding='utf-8') - ) + + r = redis.StrictRedis( + host='localhost', db=0, decode_responses=True, + encoding='utf-8') + + 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)[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 + + try: + root_circuit_identifier = int(root_circuit_identifier) + except ValueError: + for k in r.scan_iter('ims:circuit_hierarchy:*', count=2000): + ch = r.get(k) + details = json.loads(ch)[0] + if root_circuit_identifier.lower() == details['name'].lower(): + root_circuit_identifier = details['id'] + break + else: + print(f'No circuit found for: {root_circuit_identifier}') + exit(0) + tree = _get_childcircuit_tree_local(root_circuit_identifier) else: - tree = _get_childcircuit_tree(root_circuit_id) + + def _get_childcircuit_tree(circuit_id): + circuit = ds.get_entity_by_id( + 'circuit', circuit_id, navigation_properties=NAV_PROPS) + _tree = [ + f'{circuit["id"]} -- {circuit["name"]} -- ' + f'prod: {circuit["product"]["name"]} -- ' + f'spd: {circuit["speed"]["name"]} -- ' + f'status: {InventoryStatus(circuit["inventorystatusid"]).name}' + ] + + if circuit[children_prop]: + + children = [] + for child_circuit in circuit[children_prop]: + children.append(_get_childcircuit_tree( + child_circuit[childid] + )) + _tree.append(children) + else: + _tree.append([]) + return _tree + + try: + root_circuit_identifier = int(root_circuit_identifier) + except ValueError: + root_circuit = \ + ds.get_entity_by_name('circuit', root_circuit_identifier) + if root_circuit: + root_circuit_identifier = root_circuit['id'] + else: + print(f'No circuit found for: {root_circuit_identifier}') + exit(0) + + tree = _get_childcircuit_tree(root_circuit_identifier) print(format_tree( tree, format_node=itemgetter(0), get_children=itemgetter(1)) ) -- GitLab