Skip to content

Commit

Permalink
Rename use_naplm to use_platform_napalm_drive
Browse files Browse the repository at this point in the history
Raise exception if both use_platform_sluf and use_platform_napalm_driver are set to true
  • Loading branch information
johanek committed Apr 6, 2021
1 parent 2c1f649 commit a96a4ff
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,12 @@ Arguments:
(defaults to False)
use_platform_slug: Use the NetBox platform slug for the platform attribute of a Host
(defaults to False)
use_napalm: Use the Netbox platform napalm driver setting for the platform attribute
of a Host (defaults to False)
use_platform_napalm_driver: Use the Netbox platform napalm driver setting for the platform attribute of a Host
(defaults to False)
```

Only one of use_platform_slug and use_platform_napalm_driver can be set to true.

# Useful Links

- [nornir_netbox documentation](https://nornir-netbox.readthedocs.io)
Expand Down
24 changes: 17 additions & 7 deletions nornir_netbox/plugins/inventory/netbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,9 @@ class NetBoxInventory2:
(defaults to False)
use_platform_slug: Use the NetBox platform slug for the platform attribute of a Host
(defaults to False)
use_napalm: Use the Netbox platform napalm driver setting for the platform attribute
of a Host (defaults to False)
use_platform_napalm_driver: Use the Netbox platform napalm driver setting for the
platform attribute of a Host
(defaults to False)
"""

def __init__(
Expand All @@ -200,7 +201,7 @@ def __init__(
filter_parameters: Optional[Dict[str, Any]] = None,
include_vms: bool = False,
use_platform_slug: bool = False,
use_napalm: bool = False,
use_platform_napalm_driver: bool = False,
**kwargs: Any,
) -> None:
filter_parameters = filter_parameters or {}
Expand All @@ -214,15 +215,20 @@ def __init__(
self.filter_parameters = filter_parameters
self.include_vms = include_vms
self.use_platform_slug = use_platform_slug
self.use_napalm = use_napalm
self.use_platform_napalm_driver = use_platform_napalm_driver

self.session = requests.Session()
self.session.headers.update({"Authorization": f"Token {nb_token}"})
self.session.verify = ssl_verify

if self.use_platform_slug and self.use_platform_napalm_driver:
raise ValueError(
"Only one of use_platform_slug and use_platform_napalm_driver can be set to true"
)

def load(self) -> Inventory:

if self.use_napalm:
if self.use_platform_napalm_driver:
platforms: List[Dict[str, Any]] = []
platforms = self._get_resources(
url=f"{self.nb_url}/api/dcim/platforms/?limit=0", params={}
Expand Down Expand Up @@ -266,9 +272,13 @@ def load(self) -> Inventory:

if isinstance(device["platform"], dict) and self.use_platform_slug:
platform = device["platform"].get("slug")
elif isinstance(device["platform"], dict) and self.use_napalm:
elif (
isinstance(device["platform"], dict) and self.use_platform_napalm_driver
):
platform = [
d for d in platforms if device["platform"]["slug"] == d["slug"]
platform
for platform in platforms
if device["platform"]["slug"] == platform["slug"]
][0]["napalm_driver"]
elif isinstance(device["platform"], dict):
platform = device["platform"].get("name")
Expand Down
22 changes: 19 additions & 3 deletions tests/test_netbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,27 @@ def test_inventory_use_platform_slug_include_vms(self, requests_mock, version):
assert expected == inv.dict()

@pytest.mark.parametrize("version", ["2.8.9"])
def test_inventory_use_napalm(self, requests_mock, version):
inv = get_inv(requests_mock, self.plugin, False, version, use_napalm=True)
def test_inventory_use_platform_napalm_driver(self, requests_mock, version):
inv = get_inv(
requests_mock, self.plugin, False, version, use_platform_napalm_driver=True
)
with open(
f"{BASE_PATH}/{self.plugin.__name__}/{version}/expected_use_napalm.json",
f"{BASE_PATH}/{self.plugin.__name__}/{version}/expected_use_platform_napalm_driver.json",
"r",
) as f:
expected = json.load(f)
assert expected == inv.dict()

@pytest.mark.parametrize("version", ["2.8.9"])
def test_inventory_multiple_platform_sources_raises_exception(
self, requests_mock, version
):
with pytest.raises(ValueError) as e_info:
inv = get_inv(
requests_mock,
self.plugin,
False,
version,
use_platform_slug=True,
use_platform_napalm_driver=True,
)

0 comments on commit a96a4ff

Please sign in to comment.