diff --git a/inventory_provider/routes/ims_classifier.py b/inventory_provider/routes/ims_classifier.py index c55a784ae3c7c7d285f0e7946434c921055d85ea..00e90c1d38a7ac47666346df3a9ce6c92ebed880 100644 --- a/inventory_provider/routes/ims_classifier.py +++ b/inventory_provider/routes/ims_classifier.py @@ -371,8 +371,8 @@ def peer_info(address_str: str) -> Response: # canonicalize the input address first ... try: - obj = ipaddress.ip_address(address_str) - address_str = obj.exploded + address = ipaddress.ip_address(address_str) + address_str = address.exploded except ValueError: raise ClassifierProcessingError( f'unable to parse {address_str} as an ip address') @@ -419,12 +419,6 @@ def peer_info(address_str: str) -> Response: if asn_group_info: result['asn'] = asn_group_info - try: - address = ipaddress.ip_address(address_str) - except ValueError: - raise ClassifierProcessingError( - f'unable to parse {address_str} as an ip address') - for interface in find_interfaces(address): ims_equipment = get_ims_equipment_name(interface["router"]) ims_interface = get_ims_interface(interface["interface name"]) @@ -435,16 +429,14 @@ def peer_info(address_str: str) -> Response: r ) - result['interfaces'].append( - { - 'interface': interface, - 'services': services_and_locs['services'] - } - ) + t = {'interface': interface} + if services_and_locs.get('services', None): + t['services'] = services_and_locs['services'] + result['interfaces'].append(t) result['locations'].extend(services_and_locs['locations']) snmp_info = r.get( - f'snmp-peerings:remote:{address}') + f'snmp-peerings:remote:{address_str}') if snmp_info: snmp_info = json.loads(snmp_info.decode('utf-8')) result['snmp'] = [ @@ -455,6 +447,8 @@ def peer_info(address_str: str) -> Response: } for h in snmp_info] result['locations'] = _remove_duplicates_from_list(result['locations']) + if not result.get('interfaces', None): + result.pop('interfaces', None) result = json.dumps(result) # cache this data for the next call r.set(cache_key, result.encode('utf-8'))