diff --git a/inventory_provider/routes/poller.py b/inventory_provider/routes/poller.py index f5468a2add8901c09605d0fde810e71ea6558a42..7aca8050d3a849383fbdd1487e80b4652cfc6719 100644 --- a/inventory_provider/routes/poller.py +++ b/inventory_provider/routes/poller.py @@ -134,28 +134,30 @@ def _load_snmp_indexes(hostname=None): return result -def _load_interface_bundles(hostname=None, lab=False): +def _load_interface_bundles(hostname=None): result = dict() - base_key = 'netconf-interface-bundles' - if lab: - base_key = f'lab:{base_key}' + def _load_docs(key_pattern): + for doc in common.load_json_docs( + config_params=current_app.config['INVENTORY_PROVIDER_CONFIG'], + key_pattern=key_pattern, + num_threads=20): - key_pattern = f'{base_key}:{hostname}:*' \ - if hostname else '{base_key}:*' + m = re.match( + r'.*netconf-interface-bundles:([^:]+):(.+)', doc['key']) + assert m - for doc in common.load_json_docs( - config_params=current_app.config['INVENTORY_PROVIDER_CONFIG'], - key_pattern=key_pattern, - num_threads=20): + router = m.group(1) + interface = m.group(2) + result.setdefault(router, dict()) + result[router][interface] = doc['value'] - m = re.match(r'.*netconf-interface-bundles:([^:]+):(.+)', doc['key']) - assert m + base_key = 'netconf-interface-bundles' + base_key_pattern = f'{base_key}:{hostname}:*' \ + if hostname else f'{base_key}:*' - router = m.group(1) - interface = m.group(2) - result.setdefault(router, dict()) - result[router][interface] = doc['value'] + _load_docs(base_key_pattern) + _load_docs(f'lab:{base_key_pattern}') return result @@ -193,26 +195,42 @@ def _load_services(hostname=None): def _load_interfaces(hostname): - key_pattern = f'netconf:{hostname}*' if hostname else 'netconf:*' - for doc in common.load_xml_docs( - config_params=current_app.config['INVENTORY_PROVIDER_CONFIG'], - key_pattern=key_pattern, - num_threads=10): - - router = doc['key'][len('netconf:'):] + """ + loads basic interface data for production & lab routers - for ifc in juniper.list_interfaces(doc['value']): - if not ifc['description']: - continue + :param hostname: + :return: + """ + def _load_docs(key_pattern): + + m = re.match(r'^(.*netconf:).+', key_pattern) + assert m # sanity + key_prefix_len = len(m.group(1)) + assert key_prefix_len >= len('netconf:') # sanity + + for doc in common.load_xml_docs( + config_params=current_app.config['INVENTORY_PROVIDER_CONFIG'], + key_pattern=key_pattern, + num_threads=10): + + router = doc['key'][key_prefix_len:] + + for ifc in juniper.list_interfaces(doc['value']): + if not ifc['description']: + continue + + yield { + 'router': router, + 'name': ifc['name'], + 'bundle': ifc['bundle'], + 'bundle-parents': [], + 'description': ifc['description'], + 'circuits': [] + } - yield { - 'router': router, - 'name': ifc['name'], - 'bundle': ifc['bundle'], - 'bundle-parents': [], - 'description': ifc['description'], - 'circuits': [] - } + base_key_pattern = f'netconf:{hostname}*' if hostname else 'netconf:*' + yield from _load_docs(base_key_pattern) + yield from _load_docs(f'lab:{base_key_pattern}') def _add_bundle_parents(interfaces, hostname=None): @@ -224,7 +242,6 @@ def _add_bundle_parents(interfaces, hostname=None): :return: generator with bundle-parents populated in each element """ bundles = _load_interface_bundles(hostname) - bundles.update(_load_interface_bundles(hostname, lab=True)) for ifc in interfaces: router_bundle = bundles.get(ifc['router'], None)