Skip to content
Snippets Groups Projects
Commit 4894ecfa authored by Robert Latta's avatar Robert Latta
Browse files

extra functionality

parent eb0eb0fd
No related branches found
No related tags found
No related merge requests found
...@@ -4,12 +4,13 @@ from operator import itemgetter ...@@ -4,12 +4,13 @@ from operator import itemgetter
import click import click
import redis import redis
from click import BadParameter
from tree_format import format_tree from tree_format import format_tree
from inventory_provider.db.ims import IMS, CIRCUIT_PROPERTIES, InventoryStatus from inventory_provider.db.ims import IMS, CIRCUIT_PROPERTIES, InventoryStatus
username = 'TEST05' username = 'TEST05'
password = '' password = 'robert_Testing250'
bt = os.getenv('IMS_BT') bt = os.getenv('IMS_BT')
ds = IMS('http://83.97.94.128:2080/api', username, password, bt) ds = IMS('http://83.97.94.128:2080/api', username, password, bt)
...@@ -23,8 +24,11 @@ NAV_PROPS = [ ...@@ -23,8 +24,11 @@ NAV_PROPS = [
@click.command() @click.command()
@click.option('-c', 'carriers', is_flag=True) @click.option('-c', 'carriers', is_flag=True)
@click.option('-l', 'local', is_flag=True) @click.option('-l', 'local', is_flag=True)
@click.argument('root_circuit_identifier', type=click.STRING, required=True) @click.option('-i', 'interface_', is_flag=True)
def cli(carriers, local, root_circuit_identifier): @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 local:
if carriers: if carriers:
children_prop = 'carrier-circuits' children_prop = 'carrier-circuits'
...@@ -41,12 +45,12 @@ def cli(carriers, local, root_circuit_identifier): ...@@ -41,12 +45,12 @@ def cli(carriers, local, root_circuit_identifier):
NAV_PROPS.append(CIRCUIT_PROPERTIES['SubCircuits']) NAV_PROPS.append(CIRCUIT_PROPERTIES['SubCircuits'])
if local: if local:
r = redis.StrictRedis( r = redis.StrictRedis(
host='localhost', db=0, decode_responses=True, host='localhost', db=0, decode_responses=True,
encoding='utf-8') encoding='utf-8')
def _get_childcircuit_tree_local(circuit_id): def _get_childcircuit_tree_local(circuit_id):
circuit = r.get(f'ims:circuit_hierarchy:{circuit_id}') circuit = r.get(f'ims:circuit_hierarchy:{circuit_id}')
if not circuit: if not circuit:
return None return None
...@@ -70,22 +74,37 @@ def cli(carriers, local, root_circuit_identifier): ...@@ -70,22 +74,37 @@ def cli(carriers, local, root_circuit_identifier):
_tree.append([]) _tree.append([])
return _tree return _tree
try: if interface_:
root_circuit_identifier = int(root_circuit_identifier)
except ValueError: if_services = r.get(f'ims:interface_services:{root_identifier}')
for k in r.scan_iter('ims:circuit_hierarchy:*', count=2000): if if_services:
ch = r.get(k) root_identifiers = [s['id'] for s in json.loads(if_services)]
details = json.loads(ch)[0] children = []
if root_circuit_identifier.lower() == details['name'].lower(): for id_ in root_identifiers:
root_circuit_identifier = details['id'] children.append(_get_childcircuit_tree_local(id_))
break
tree = [root_identifier, children]
else: else:
print(f'No circuit found for: {root_circuit_identifier}') print(f'No circuit found for: {root_identifier}')
exit(0) 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: else:
def _get_childcircuit_tree(circuit_id): def _get_childcircuit_tree_remote(circuit_id):
circuit = ds.get_entity_by_id( circuit = ds.get_entity_by_id(
'circuit', circuit_id, navigation_properties=NAV_PROPS) 'circuit', circuit_id, navigation_properties=NAV_PROPS)
_tree = [ _tree = [
...@@ -99,7 +118,7 @@ def cli(carriers, local, root_circuit_identifier): ...@@ -99,7 +118,7 @@ def cli(carriers, local, root_circuit_identifier):
children = [] children = []
for child_circuit in circuit[children_prop]: for child_circuit in circuit[children_prop]:
children.append(_get_childcircuit_tree( children.append(_get_childcircuit_tree_remote(
child_circuit[childid] child_circuit[childid]
)) ))
_tree.append(children) _tree.append(children)
...@@ -108,17 +127,17 @@ def cli(carriers, local, root_circuit_identifier): ...@@ -108,17 +127,17 @@ def cli(carriers, local, root_circuit_identifier):
return _tree return _tree
try: try:
root_circuit_identifier = int(root_circuit_identifier) root_identifier = int(root_identifier)
except ValueError: except ValueError:
root_circuit = \ root_circuit = \
ds.get_entity_by_name('circuit', root_circuit_identifier) ds.get_entity_by_name('circuit', root_identifier)
if root_circuit: if root_circuit:
root_circuit_identifier = root_circuit['id'] root_identifier = root_circuit['id']
else: else:
print(f'No circuit found for: {root_circuit_identifier}') print(f'No circuit found for: {root_identifier}')
exit(0) exit(0)
tree = _get_childcircuit_tree(root_circuit_identifier) tree = _get_childcircuit_tree_remote(root_identifier)
print(format_tree( print(format_tree(
tree, format_node=itemgetter(0), get_children=itemgetter(1)) tree, format_node=itemgetter(0), get_children=itemgetter(1))
) )
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment