diff --git a/inventory_provider/tasks/worker.py b/inventory_provider/tasks/worker.py index 8a85d7e6ad1cac9db894b2730755addb6057b00f..d0613236b21c8b987a92b94a878751ea14f7a9c4 100644 --- a/inventory_provider/tasks/worker.py +++ b/inventory_provider/tasks/worker.py @@ -199,7 +199,7 @@ def update_interfaces_to_services(self): rp.execute() -def _unmanaged_interfaces(self): +def _unmanaged_interfaces(): def _convert(d): # the config file keys are more readable than @@ -215,18 +215,6 @@ def _unmanaged_interfaces(self): _convert, InventoryTask.config.get('unmanaged-interfaces', [])) - # if interfaces: - # r = get_next_redis(InventoryTask.config) - # rp = r.pipeline() - # for ifc in interfaces: - # rp.set( - # f'reverse_interface_addresses:{ifc["name"]}', - # json.dumps(ifc)) - # rp.set( - # f'subnets:{ifc["interface address"]}', - # json.dumps([ifc])) - # rp.execute() - @app.task(base=InventoryTask, bind=True, name='update_access_services') @log_task_entry_and_exit @@ -710,6 +698,10 @@ def _build_subnet_db(update_callback=lambda s: None): entry = subnets.setdefault(ifc['interface address'], []) entry.append(ifc) + for ifc in _unmanaged_interfaces(): + entry = subnets.setdefault(ifc['interface address'], []) + entry.append(ifc) + update_callback('saving {} subnets'.format(len(subnets))) rp = r.pipeline() diff --git a/test/conftest.py b/test/conftest.py index 4bafdb12d30e117bc0de5cd261e1d1bdec8f240a..2984ae3785845bc100471a30a207177601be2da3 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -54,6 +54,20 @@ def data_config_filename(): "password": "ims_password" }, "managed-routers": "bogus url", + "unmanaged-interfaces": [ + { + "address": "99.99.99.99", + "network": "99.99.99.0/24", + "interface": "ABC/0/0/0", + "router": "bogus.host.name" + }, + { + "address": "999.999.999.99", + "network": "999.999.999.0/24", + "interface": "ZZZ/9/a/x:0.123", + "router": "another.bogus.host.name" + } + ] } f.write(json.dumps(config).encode('utf-8')) diff --git a/test/test_worker_utils.py b/test/test_worker_utils.py index 198bc43b2591fe5f40eb381c9763adcaf73a939f..f18c826456595a9e084787c8cb982eb47561dcb6 100644 --- a/test/test_worker_utils.py +++ b/test/test_worker_utils.py @@ -11,6 +11,7 @@ import jsonschema from inventory_provider.tasks import worker from inventory_provider.tasks import common from inventory_provider.routes import msr +from inventory_provider import config def backend_db(): @@ -23,7 +24,7 @@ def backend_db(): }).db -def test_build_subnet_db(mocked_worker_module): +def test_build_subnet_db(mocked_worker_module, data_config_filename): """ Verify that valid reverse subnet objects are created. @@ -52,6 +53,16 @@ def test_build_subnet_db(mocked_worker_module): 'items': {"$ref": "#/definitions/interface"}, } + all_subnet_interfaces = set() + unmanaged_interfaces = set() + with open(data_config_filename) as f: + params = config.load(f) + for ifc in params.get('unmanaged-interfaces', []): + ifc_key = (f'{ifc["router"].lower()}' + f':{ifc["interface"].lower()}' + f':{ifc["network"]}') + unmanaged_interfaces.add(ifc_key) + db = backend_db() # also forces initialization def _x(k): @@ -80,8 +91,16 @@ def test_build_subnet_db(mocked_worker_module): for ifc in value: assert ifc['interface address'] == address + ifc_key = (f'{ifc["router"]}' + f':{ifc["interface name"]}' + f':{ifc["interface address"]}') + + all_subnet_interfaces.add(ifc_key) + assert found_record + assert unmanaged_interfaces <= all_subnet_interfaces + def test_build_juniper_peering_db(mocked_worker_module): """