Skip to content

Commit

Permalink
add support for api inventory too
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffkala committed Sep 19, 2024
1 parent 4ec328f commit ec03284
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 257 deletions.
2 changes: 1 addition & 1 deletion examples/basic_with_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
task=dispatcher,
logger=LOGGER,
method="get_config",
obj=nr_host,
obj=nr_obj,
framework="netmiko",
backup_file="./ios.cfg",
remove_lines=None,
Expand Down
241 changes: 0 additions & 241 deletions ios.cfg

This file was deleted.

6 changes: 4 additions & 2 deletions nornir_nautobot/plugins/inventory/nautobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,12 @@ def devices(self) -> list:
if self._devices is None:
# Check for filters. Cannot pass an empty dictionary to the filter method
if self.filter_parameters is None:
self._devices = self.pynautobot_obj.dcim.devices.all()
self._devices = self.pynautobot_obj.dcim.devices.filter(include=["config_context"])
else:
try:
self._devices = self.pynautobot_obj.dcim.devices.filter(**self.filter_parameters)
self._devices = self.pynautobot_obj.dcim.devices.filter(
**self.filter_parameters, include=["config_context"]
)
except pynautobot.core.query.RequestError as err:
print(f"Error in the query filters: {err.error}. Please verify the parameters.")
sys.exit(1)
Expand Down
20 changes: 18 additions & 2 deletions nornir_nautobot/plugins/tasks/dispatcher/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,19 @@ def _get_tcp_port(cls, obj) -> int:
custom_field = obj.cf.get("tcp_port")
if isinstance(custom_field, int):
return custom_field
# NautobotORMInventory (nautobot-plugin-nornir) inventory.
if hasattr(obj, "get_config_context"):
config_context = obj.get_config_context().get("tcp_port")
if isinstance(config_context, int):
return config_context
# NautobotInventory (nornir-nautobot) inventory.
if obj.data["pynautobot_dictionary"].get("custom_fields"):
custom_field = obj.data["pynautobot_dictionary"]["custom_fields"].get("tcp_port")
if isinstance(custom_field, int):
return custom_field
if obj.data["pynautobot_dictionary"].get("config_context"):
config_context = obj.data["pynautobot_dictionary"]["config_context"].get("tcp_port")
if isinstance(config_context, int):
return config_context
return cls.tcp_port

@classmethod
Expand All @@ -62,11 +70,19 @@ def _get_config_injections(cls, obj) -> list:
custom_field = obj.cf.get("config_injections")
if isinstance(custom_field, str):
return custom_field.split(",")
# NautobotORMInventory (nautobot-plugin-nornir) inventory.
if hasattr(obj, "get_config_context"):
config_context = obj.get_config_context().get("config_injections")
if isinstance(config_context, str):
return config_context.split(",")
# NautobotInventory (nornir-nautobot) inventory.
if obj.data["pynautobot_dictionary"].get("custom_fields"):
custom_field = obj.data["pynautobot_dictionary"]["custom_fields"].get("config_injections")
if isinstance(custom_field, str):
return custom_field.split(",")
if obj.data["pynautobot_dictionary"].get("config_context"):
config_context = obj.data["pynautobot_dictionary"]["config_context"].get("config_injections")
if isinstance(config_context, str):
return config_context.split(",")
return cls.config_injections

@classmethod
Expand Down
22 changes: 11 additions & 11 deletions tests/unit/test_nautobot_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,47 +31,47 @@
},
{
"fixture_path": f"{HERE}/mocks/01_get_devices.json",
"url": "http://mock.example.com/api/dcim/devices/?depth=1",
"url": "http://mock.example.com/api/dcim/devices/?depth=1&include=config_context",
"method": "get",
},
{
"fixture_path": f"{HERE}/mocks/01_get_devices.json",
"url": "https://mock.example.com/api/dcim/devices/?depth=1",
"url": "https://mock.example.com/api/dcim/devices/?depth=1&include=config_context",
"method": "get",
},
{
"fixture_path": f"{HERE}/mocks/01_get_devices.json",
"url": "https://mock.example.com/api/dcim/devices/?depth=1&limit=0",
"url": "https://mock.example.com/api/dcim/devices/?depth=1&limit=0&include=config_context",
"method": "get",
},
{
"fixture_path": f"{HERE}/mocks/02_get_device1.json",
"url": "http://mock.example.com/api/dcim/devices/?name=den-dist01",
"url": "http://mock.example.com/api/dcim/devices/?name=den-dist01&include=config_context",
"method": "get",
},
{
"fixture_path": f"{HERE}/mocks/03_get_device2.json",
"url": "http://mock.example.com/api/dcim/devices/?name=den-dist02",
"url": "http://mock.example.com/api/dcim/devices/?name=den-dist02&include=config_context",
"method": "get",
},
{
"fixture_path": f"{HERE}/mocks/04_get_device3.json",
"url": "http://mock.example.com/api/dcim/devices/?name=den-wan01",
"url": "http://mock.example.com/api/dcim/devices/?name=den-wan01&include=config_context",
"method": "get",
},
{
"fixture_path": f"{HERE}/mocks/05_get_locations_filtered.json",
"url": "http://mock.example.com/api/dcim/devices/?depth=1&location=msp",
"url": "http://mock.example.com/api/dcim/devices/?depth=1&location=msp&include=config_context",
"method": "get",
},
{
"fixture_path": f"{HERE}/mocks/06_get_device_msp-rtr01.json",
"url": "http://mock.example.com/api/dcim/devices/?name=msp-rtr01",
"url": "http://mock.example.com/api/dcim/devices/?name=msp-rtr01&include=config_context",
"method": "get",
},
{
"fixture_path": f"{HERE}/mocks/07_get_device_msp-rtr02.json",
"url": "http://mock.example.com/api/dcim/devices/?name=msp-rtr02",
"url": "http://mock.example.com/api/dcim/devices/?name=msp-rtr02&include=config_context",
"method": "get",
},
]
Expand Down Expand Up @@ -163,7 +163,7 @@ def test_devices(nornir_nautobot_class):
pynautobot_obj = pynautobot.api(url="http://mock.example.com", token="0123456789abcdef01234567890")
expected_devices = []
for device in ["den-dist01", "den-dist02", "den-wan01"]:
expected_devices.append(pynautobot_obj.dcim.devices.get(name=device))
expected_devices.append(pynautobot_obj.dcim.devices.get(name=device, include=["config_context"]))

assert nornir_nautobot_class.devices == expected_devices

Expand All @@ -179,7 +179,7 @@ def test_filter_devices():
pynautobot_obj = pynautobot.api(url="http://mock.example.com", token="0123456789abcdef01234567890")
expected_devices = []
for device in ["msp-rtr01", "msp-rtr02"]:
expected_devices.append(pynautobot_obj.dcim.devices.get(name=device))
expected_devices.append(pynautobot_obj.dcim.devices.get(name=device, include=["config_context"]))

assert test_class.devices == expected_devices

Expand Down

0 comments on commit ec03284

Please sign in to comment.