Skip to content

Commit

Permalink
Merge pull request #40 from wvandeun/fix_nornir_defaults
Browse files Browse the repository at this point in the history
Fix nornir inventory defaults
  • Loading branch information
jvanderaa authored Oct 14, 2021
2 parents 0ef2d9f + b965ab6 commit 0af6eb1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
6 changes: 4 additions & 2 deletions nornir_nautobot/plugins/inventory/nautobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
logger = logging.getLogger(__name__)


def _set_host(data: Dict[str, Any], name: str, groups, host, host_platform) -> Host:
def _set_host(data: Dict[str, Any], name: str, groups, host, defaults: Defaults) -> Host:
host_platform = getattr(data["pynautobot_object"].platform, "slug", None)
connection_option = {}
for key, value in data.get("connection_options", {}).items():
connection_option[key] = ConnectionOptions(
Expand All @@ -44,6 +45,7 @@ def _set_host(data: Dict[str, Any], name: str, groups, host, host_platform) -> H
platform=host_platform,
data=data,
groups=groups,
defaults=defaults,
connection_options=connection_option,
)

Expand Down Expand Up @@ -160,7 +162,7 @@ def load(self) -> Inventory:
name=host["name"],
groups=host["groups"],
host=host,
host_platform=getattr(device.platform, "slug", None),
defaults=defaults,
)

return Inventory(hosts=hosts, groups=groups, defaults=defaults)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nornir-nautobot"
version = "2.1.1"
version = "2.1.2"
description = "Nornir Nautobot"
authors = ["Network to Code, LLC <[email protected]>"]
readme = "README.md"
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/test_nautobot_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,30 @@ def test_nornir_nautobot_device_count():
assert len(test_nornir.inventory.hosts) == 3


def test_nornir_nautobot_with_defaults():
"""
Tests that nornir defaults are getting applied to NautobotInventory hosts
"""
with Mocker() as mock:
load_api_calls(mock)
nr_obj = InitNornir(
inventory={
"plugin": "NautobotInventory",
"options": {
"nautobot_url": "http://mock.example.com",
"nautobot_token": "0123456789abcdef01234567890",
"pynautobot_dict": False,
},
},
logging={"enabled": False},
)
nr_obj.inventory.defaults.username = "username"
nr_obj.inventory.defaults.password = "password"

assert nr_obj.inventory.hosts["den-dist01"].username == nr_obj.inventory.defaults.username
assert nr_obj.inventory.hosts["den-dist02"].password == nr_obj.inventory.defaults.password


@pytest.mark.parametrize(
"device, expected_platform", [("den-dist01", None), ("den-wan01", "ios"), ("den-dist02", "ios")]
)
Expand Down

0 comments on commit 0af6eb1

Please sign in to comment.