Skip to content

Commit

Permalink
Add use_napalm parameter to NetBoxInventory2
Browse files Browse the repository at this point in the history
  • Loading branch information
johanek committed Mar 10, 2021
1 parent 59450e8 commit 6632a73
Show file tree
Hide file tree
Showing 6 changed files with 429 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ 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)
```

# Useful Links
Expand Down
13 changes: 13 additions & 0 deletions nornir_netbox/plugins/inventory/netbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ 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)
"""

def __init__(
Expand All @@ -198,6 +200,7 @@ def __init__(
filter_parameters: Optional[Dict[str, Any]] = None,
include_vms: bool = False,
use_platform_slug: bool = False,
use_napalm: bool = False,
**kwargs: Any,
) -> None:
filter_parameters = filter_parameters or {}
Expand All @@ -211,13 +214,19 @@ 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.session = requests.Session()
self.session.headers.update({"Authorization": f"Token {nb_token}"})
self.session.verify = ssl_verify

def load(self) -> Inventory:

platforms: List[Dict[str, Any]] = []
platforms = self._get_resources(
url=f"{self.nb_url}/api/dcim/platforms/?limit=0", params={}
)

nb_devices: List[Dict[str, Any]] = []

nb_devices = self._get_resources(
Expand Down Expand Up @@ -256,6 +265,10 @@ 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:
platform = [
d for d in platforms if device["platform"]["slug"] == d["slug"]
][0]["napalm_driver"]
elif isinstance(device["platform"], dict):
platform = device["platform"].get("name")
else:
Expand Down
319 changes: 319 additions & 0 deletions tests/NetBoxInventory2/2.8.9/expected_use_napalm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,319 @@
{
"hosts": {
"1-Core": {
"name": "1-Core",
"connection_options": {},
"groups": [],
"data": {
"id": 1,
"name": "1-Core",
"display_name": "1-Core",
"device_type": {
"id": 11,
"url": "http://localhost:8080/api/dcim/device-types/11/",
"manufacturer": {
"id": 3,
"url": "http://localhost:8080/api/dcim/manufacturers/3/",
"name": "Juniper",
"slug": "juniper"
},
"model": "MX480",
"slug": "mx480"
},
"device_role": {
"id": 1,
"url": "http://localhost:8080/api/dcim/device-roles/1/",
"name": "Router",
"slug": "rt"
},
"tenant": null,
"platform": {
"id": 1,
"url": "http://localhost:8080/api/dcim/platforms/1/",
"name": "juniper_junos",
"slug": "junos"
},
"serial": "",
"asset_tag": null,
"site": {
"id": 3,
"url": "http://localhost:8080/api/dcim/sites/3/",
"name": "Sunnyvale, CA",
"slug": "sunnyvale-ca"
},
"rack": null,
"position": null,
"face": null,
"parent_device": null,
"status": {
"value": 1,
"label": "Active"
},
"primary_ip": {
"id": 1,
"url": "http://localhost:8080/api/ipam/ip-addresses/1/",
"family": 4,
"address": "10.0.1.1/32"
},
"primary_ip4": {
"id": 1,
"url": "http://localhost:8080/api/ipam/ip-addresses/1/",
"family": 4,
"address": "10.0.1.1/32"
},
"primary_ip6": null,
"cluster": null,
"virtual_chassis": null,
"vc_position": null,
"vc_priority": null,
"comments": "",
"custom_fields": {},
"created": "2018-07-12",
"last_updated": "2018-07-12T11:53:54.742412Z"
},
"hostname": "10.0.1.1",
"port": null,
"username": null,
"password": null,
"platform": "napalm_junos"
},
"2-Distribution": {
"name": "2-Distribution",
"connection_options": {},
"groups": [],
"data": {
"id": 2,
"name": "2-Distribution",
"display_name": "2-Distribution",
"device_type": {
"id": 9,
"url": "http://localhost:8080/api/dcim/device-types/9/",
"manufacturer": {
"id": 3,
"url": "http://localhost:8080/api/dcim/manufacturers/3/",
"name": "Juniper",
"slug": "juniper"
},
"model": "EX4550-32F",
"slug": "ex4550-32f"
},
"device_role": {
"id": 1,
"url": "http://localhost:8080/api/dcim/device-roles/1/",
"name": "Router",
"slug": "rt"
},
"tenant": null,
"platform": null,
"serial": "",
"asset_tag": null,
"site": {
"id": 3,
"url": "http://localhost:8080/api/dcim/sites/3/",
"name": "Sunnyvale, CA",
"slug": "sunnyvale-ca"
},
"rack": null,
"position": null,
"face": null,
"parent_device": null,
"status": {
"value": 1,
"label": "Active"
},
"primary_ip": {
"id": 2,
"url": "http://localhost:8080/api/ipam/ip-addresses/2/",
"family": 4,
"address": "172.16.2.1/32"
},
"primary_ip4": {
"id": 2,
"url": "http://localhost:8080/api/ipam/ip-addresses/2/",
"family": 4,
"address": "172.16.2.1/32"
},
"primary_ip6": null,
"cluster": null,
"virtual_chassis": null,
"vc_position": null,
"vc_priority": null,
"comments": "",
"custom_fields": {},
"created": "2018-07-12",
"last_updated": "2018-07-12T11:53:54.802934Z"
},
"hostname": "172.16.2.1",
"port": null,
"username": null,
"password": null,
"platform": null
},
"3-Access": {
"name": "3-Access",
"connection_options": {},
"groups": [],
"data": {
"id": 3,
"name": "3-Access",
"display_name": "3-Access",
"device_type": {
"id": 2,
"url": "http://localhost:8080/api/dcim/device-types/2/",
"manufacturer": {
"id": 2,
"url": "http://localhost:8080/api/dcim/manufacturers/2/",
"name": "Cisco",
"slug": "cisco"
},
"model": "3650-48TQ-L",
"slug": "3650-48tq-l"
},
"device_role": {
"id": 2,
"url": "http://localhost:8080/api/dcim/device-roles/2/",
"name": "Switch",
"slug": "sw"
},
"tenant": null,
"platform": {
"id": 2,
"url": "http://localhost:8080/api/dcim/platforms/2/",
"name": "cisco_ios",
"slug": "ios"
},
"serial": "",
"asset_tag": null,
"site": {
"id": 2,
"url": "http://localhost:8080/api/dcim/sites/2/",
"name": "San Jose, CA",
"slug": "san-jose-ca"
},
"rack": null,
"position": null,
"face": null,
"parent_device": null,
"status": {
"value": 1,
"label": "Active"
},
"primary_ip": {
"id": 3,
"url": "http://localhost:8080/api/ipam/ip-addresses/3/",
"family": 4,
"address": "192.168.3.1/32"
},
"primary_ip4": {
"id": 3,
"url": "http://localhost:8080/api/ipam/ip-addresses/3/",
"family": 4,
"address": "192.168.3.1/32"
},
"primary_ip6": null,
"cluster": null,
"virtual_chassis": null,
"vc_position": null,
"vc_priority": null,
"comments": "",
"custom_fields": {
"user_defined": 1
},
"created": "2018-07-12",
"last_updated": "2018-07-12T11:53:54.866133Z"
},
"hostname": "192.168.3.1",
"port": null,
"username": null,
"password": null,
"platform": "napalm_ios"
},
"4": {
"name": "4",
"connection_options": {},
"groups": [],
"data": {
"id": 4,
"name": null,
"display_name": "MX480",
"device_type": {
"id": 11,
"url": "http://localhost:8080/api/dcim/device-types/11/",
"manufacturer": {
"id": 3,
"url": "http://localhost:8080/api/dcim/manufacturers/3/",
"name": "Juniper",
"slug": "juniper"
},
"model": "MX480",
"slug": "mx480"
},
"device_role": {
"id": 1,
"url": "http://localhost:8080/api/dcim/device-roles/1/",
"name": "Router",
"slug": "rt"
},
"tenant": null,
"platform": {
"id": 1,
"url": "http://localhost:8080/api/dcim/platforms/1/",
"name": "juniper_junos",
"slug": "junos"
},
"serial": "",
"asset_tag": null,
"site": {
"id": 3,
"url": "http://localhost:8080/api/dcim/sites/3/",
"name": "Sunnyvale, CA",
"slug": "sunnyvale-ca"
},
"rack": null,
"position": null,
"face": null,
"parent_device": null,
"status": {
"value": 1,
"label": "Active"
},
"primary_ip": {
"id": 4,
"url": "http://localhost:8080/api/ipam/ip-addresses/4/",
"family": 4,
"address": "10.0.1.4/32"
},
"primary_ip4": {
"id": 1,
"url": "http://localhost:8080/api/ipam/ip-addresses/4/",
"family": 4,
"address": "10.0.1.4/32"
},
"primary_ip6": null,
"cluster": null,
"virtual_chassis": null,
"vc_position": null,
"vc_priority": null,
"comments": "",
"custom_fields": {},
"created": "2018-07-12",
"last_updated": "2018-07-12T11:53:54.742412Z"
},
"hostname": "10.0.1.4",
"port": null,
"username": null,
"password": null,
"platform": "napalm_junos"
}
},
"groups": {},
"defaults": {
"data": {},
"connection_options": {},
"hostname": null,
"port": null,
"username": null,
"password": null,
"platform": null
}
}
Loading

0 comments on commit 6632a73

Please sign in to comment.