diff --git a/inventory_provider/db/opsdb.py b/inventory_provider/db/opsdb.py index 49d771a232af7e9b78f830b8e1bf663a12d93882..d77303e415073fa0515482fdeb34b6fc9bb6174e 100644 --- a/inventory_provider/db/opsdb.py +++ b/inventory_provider/db/opsdb.py @@ -246,7 +246,7 @@ def get_equipment_location_data(connection): # pragma: no cover def get_coriant_path(connection, equipment_name, card_id, port_number): - circuit_query = """ + base_query = """ SELECT vcc.absid, @@ -268,7 +268,26 @@ SELECT LEFT JOIN equipment_card eqc_b ON eqc_b.absid = vcc.PTR_card_b LEFT JOIN pop pop_a ON pop_a.absid = vcc.PTR_pop_a LEFT JOIN pop pop_b ON pop_b.absid = vcc.PTR_pop_b + """ + def _fields2rsp(row): + return { + 'absid': row['absid'], + 'category': row['category'], + 'circuit_type': row['circuit_type'], + 'service_type': row['service_type'], + 'peering_type': row['peering_type'], + 'a': { + 'name': row['equipment_name_a'], + 'pop': row['pop_name_a'] + }, + 'b': { + 'name': row['equipment_name_b'], + 'pop': row['pop_name_b'] + } + } + + circuit_query = base_query + """ WHERE vcc.status = 'Operational' AND ( (eq_a.name = %(equipment_name)s @@ -282,31 +301,9 @@ WHERE vcc.status = 'Operational' ORDER BY FIELD(vcc.circuit_type, 'path') DESC """ - parent_query = """ -SELECT - - parent.absid, - parent.category, - parent.circuit_type, - parent.service_type, - parent.peering_type, - eq_a.name as equipment_name_a, - eq_b.name as equipment_name_b, - eqc_a.card_id as card_id_a, - eqc_a.card_id as card_id_b, - pop_a.name as pop_name_a, - pop_b.name as pop_name_b - - FROM vcircuitconns parent - LEFT JOIN equipment eq_a ON eq_a.absid = parent.PTR_equip_a - LEFT JOIN equipment eq_b ON eq_b.absid = parent.PTR_equip_b - LEFT JOIN equipment_card eqc_a ON eqc_a.absid = parent.PTR_card_a - LEFT JOIN equipment_card eqc_b ON eqc_b.absid = parent.PTR_card_b - LEFT JOIN pop pop_a ON pop_a.absid = parent.PTR_pop_a - LEFT JOIN pop pop_b ON pop_b.absid = parent.PTR_pop_b - JOIN circuit_glue - ON circuit_glue.PTR_circuit = parent.absid - + parent_query = base_query + """ +JOIN circuit_glue + ON circuit_glue.PTR_circuit = vcc.absid WHERE circuit_glue.PTR_component = %s AND circuit_type = 'Path' """ @@ -349,6 +346,7 @@ AND circuit_type = 'Path' 'port_number': port_number } + with db.cursor(connection) as crs: crs.execute(circuit_query, args) r = _convert_to_dict(crs) @@ -356,9 +354,8 @@ AND circuit_type = 'Path' return None circuit = r[0] if circuit['circuit_type'].lower() == 'path': - return circuit + return _fields2rsp(circuit) crs.execute(parent_query, [circuit['absid']]) r = _convert_to_dict(crs) - print("PARENT") - return r[0] if r else None + return _fields2rsp(r[0]) if r else None