diff --git a/circuit_tree.py b/circuit_tree.py index 06e262e96cc363a3f377d7c292932ea3f6815a6f..7a85cafbd5a585fc38f87699d6c58c307d72c4be 100644 --- a/circuit_tree.py +++ b/circuit_tree.py @@ -4,12 +4,13 @@ from operator import itemgetter import click import redis +from click import BadParameter from tree_format import format_tree from inventory_provider.db.ims import IMS, CIRCUIT_PROPERTIES, InventoryStatus username = 'TEST05' -password = '' +password = 'robert_Testing250' bt = os.getenv('IMS_BT') ds = IMS('http://83.97.94.128:2080/api', username, password, bt) @@ -23,8 +24,11 @@ NAV_PROPS = [ @click.command() @click.option('-c', 'carriers', is_flag=True) @click.option('-l', 'local', is_flag=True) -@click.argument('root_circuit_identifier', type=click.STRING, required=True) -def cli(carriers, local, root_circuit_identifier): +@click.option('-i', 'interface_', is_flag=True) +@click.argument('root_identifier', type=click.STRING, required=True) +def cli(carriers, local, interface_, root_identifier): + if interface_ and not local: + raise BadParameter('-i flag only works with -l flag') if local: if carriers: children_prop = 'carrier-circuits' @@ -41,12 +45,12 @@ def cli(carriers, local, root_circuit_identifier): NAV_PROPS.append(CIRCUIT_PROPERTIES['SubCircuits']) if local: - 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 @@ -70,22 +74,37 @@ def cli(carriers, local, root_circuit_identifier): _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 + if interface_: + + 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_)) + + tree = [root_identifier, children] else: - print(f'No circuit found for: {root_circuit_identifier}') + print(f'No circuit found for: {root_identifier}') exit(0) - tree = _get_childcircuit_tree_local(root_circuit_identifier) + else: + try: + root_identifier = int(root_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_identifier.lower() == details['name'].lower(): + root_identifier = details['id'] + break + else: + print(f'No circuit found for: {root_identifier}') + exit(0) + + tree = _get_childcircuit_tree_local(root_identifier) else: - def _get_childcircuit_tree(circuit_id): + def _get_childcircuit_tree_remote(circuit_id): circuit = ds.get_entity_by_id( 'circuit', circuit_id, navigation_properties=NAV_PROPS) _tree = [ @@ -99,7 +118,7 @@ def cli(carriers, local, root_circuit_identifier): children = [] for child_circuit in circuit[children_prop]: - children.append(_get_childcircuit_tree( + children.append(_get_childcircuit_tree_remote( child_circuit[childid] )) _tree.append(children) @@ -108,17 +127,17 @@ def cli(carriers, local, root_circuit_identifier): return _tree try: - root_circuit_identifier = int(root_circuit_identifier) + root_identifier = int(root_identifier) except ValueError: root_circuit = \ - ds.get_entity_by_name('circuit', root_circuit_identifier) + ds.get_entity_by_name('circuit', root_identifier) if root_circuit: - root_circuit_identifier = root_circuit['id'] + root_identifier = root_circuit['id'] else: - print(f'No circuit found for: {root_circuit_identifier}') + print(f'No circuit found for: {root_identifier}') exit(0) - tree = _get_childcircuit_tree(root_circuit_identifier) + tree = _get_childcircuit_tree_remote(root_identifier) print(format_tree( tree, format_node=itemgetter(0), get_children=itemgetter(1)) )