Skip to content

Commit

Permalink
Merge pull request #5 from wvandeun/fix_platform_attribute
Browse files Browse the repository at this point in the history
fix issue with platform attribute since NetBox v2.4
  • Loading branch information
wvandeun authored Sep 4, 2020
2 parents 303e644 + 5b7eb3d commit 4e41775
Show file tree
Hide file tree
Showing 15 changed files with 817 additions and 122 deletions.
21 changes: 17 additions & 4 deletions nornir_netbox/plugins/inventory/netbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,20 @@ def load(self) -> Inventory:
data["site"] = device.get("site", {}).get("slug")
data["role"] = device.get("device_role", {}).get("slug")
data["model"] = device.get("device_type", {}).get("slug")
if device.get("platform"):
platform = device.get("platform", {}).get("slug")
platform = (
device["platform"]["slug"]
if isinstance(device["platform"], dict)
else device["platform"]
)
else:
data["site"] = device.get("site", {}).get("name")
data["role"] = device.get("device_role")
data["model"] = device.get("device_type")
platform = device.get("platform")
platform = (
device["platform"]["name"]
if isinstance(device["platform"], dict)
else device["platform"]
)

hostname = None
if device.get("primary_ip"):
Expand Down Expand Up @@ -190,10 +197,16 @@ def load(self) -> Inventory:
if device.get("name") is not None:
hostname = device["name"]

platform = (
device["platform"]["name"]
if isinstance(device["platform"], dict)
else device["platform"]
)

host = Host(
name=device.get("name") or str(device.get("id")),
hostname=hostname,
platform=device.get("platform"),
platform=platform,
data=data,
)

Expand Down
94 changes: 94 additions & 0 deletions tests/NBInventory/2.3.5/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"hosts": {
"1-Core": {
"name": "1-Core",
"port": null,
"hostname": "10.0.1.1",
"username": null,
"password": null,
"platform": "junos",
"data": {
"serial": "",
"vendor": "Juniper",
"asset_tag": null,
"site": "sunnyvale-ca",
"role": "rt",
"model": "mx480"
},
"connection_options": {
},
"groups": []
},
"2-Distribution": {
"name": "2-Distribution",
"port": null,
"hostname": "172.16.2.1",
"username": null,
"password": null,
"platform": null,
"data": {
"serial": "",
"vendor": "Juniper",
"asset_tag": null,
"site": "sunnyvale-ca",
"role": "rt",
"model": "ex4550-32f"
},
"connection_options": {
},
"groups": []
},
"3-Access": {
"name": "3-Access",
"port": null,
"hostname": "192.168.3.1",
"username": null,
"password": null,
"platform": "ios",
"data": {
"user_defined": 1,
"serial": "",
"vendor": "Cisco",
"asset_tag": null,
"site": "san-jose-ca",
"role": "sw",
"model": "3650-48tq-l"
},
"connection_options": {
},
"groups": []
},
"4": {
"name": "4",
"port": null,
"hostname": "10.0.1.4",
"username": null,
"password": null,
"platform": "junos",
"data": {
"serial": "",
"vendor": "Juniper",
"asset_tag": null,
"site": "sunnyvale-ca",
"role": "rt",
"model": "mx480"
},
"connection_options": {
},
"groups": []
}
},
"groups": {
},
"defaults": {
"port": null,
"hostname": null,
"username": null,
"password": null,
"platform": null,
"data": {
},
"connection_options": {
}
}
}
94 changes: 94 additions & 0 deletions tests/NBInventory/2.8.9/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"hosts": {
"1-Core": {
"name": "1-Core",
"port": null,
"hostname": "10.0.1.1",
"username": null,
"password": null,
"platform": "junos",
"data": {
"serial": "",
"vendor": "Juniper",
"asset_tag": null,
"site": "sunnyvale-ca",
"role": "rt",
"model": "mx480"
},
"connection_options": {
},
"groups": []
},
"2-Distribution": {
"name": "2-Distribution",
"port": null,
"hostname": "172.16.2.1",
"username": null,
"password": null,
"platform": null,
"data": {
"serial": "",
"vendor": "Juniper",
"asset_tag": null,
"site": "sunnyvale-ca",
"role": "rt",
"model": "ex4550-32f"
},
"connection_options": {
},
"groups": []
},
"3-Access": {
"name": "3-Access",
"port": null,
"hostname": "192.168.3.1",
"username": null,
"password": null,
"platform": "ios",
"data": {
"user_defined": 1,
"serial": "",
"vendor": "Cisco",
"asset_tag": null,
"site": "san-jose-ca",
"role": "sw",
"model": "3650-48tq-l"
},
"connection_options": {
},
"groups": []
},
"4": {
"name": "4",
"port": null,
"hostname": "10.0.1.4",
"username": null,
"password": null,
"platform": "junos",
"data": {
"serial": "",
"vendor": "Juniper",
"asset_tag": null,
"site": "sunnyvale-ca",
"role": "rt",
"model": "mx480"
},
"connection_options": {
},
"groups": []
}
},
"groups": {
},
"defaults": {
"port": null,
"hostname": null,
"username": null,
"password": null,
"platform": null,
"data": {
},
"connection_options": {
}
}
}
87 changes: 0 additions & 87 deletions tests/NBInventory/expected.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"slug": "rt"
},
"tenant": null,
"platform": null,
"platform": "junos",
"serial": "",
"asset_tag": null,
"site": {
Expand Down Expand Up @@ -65,7 +65,7 @@
},
"name":"1-Core",
"hostname": "10.0.1.1",
"platform": null,
"platform": "junos",
"port": null,
"username": null,
"password": null,
Expand Down Expand Up @@ -168,7 +168,7 @@
"slug": "sw"
},
"tenant": null,
"platform": null,
"platform": "ios",
"serial": "",
"asset_tag": null,
"site": {
Expand Down Expand Up @@ -211,7 +211,7 @@
},
"name":"3-Access",
"hostname": "192.168.3.1",
"platform": null,
"platform": "ios",
"port": null,
"username": null,
"password": null,
Expand Down Expand Up @@ -242,7 +242,7 @@
"slug": "rt"
},
"tenant": null,
"platform": null,
"platform": "junos",
"serial": "",
"asset_tag": null,
"site": {
Expand Down Expand Up @@ -283,7 +283,7 @@
},
"name":"4",
"hostname": "10.0.1.4",
"platform": null,
"platform": "junos",
"port": null,
"username": null,
"password": null,
Expand Down
Loading

0 comments on commit 4e41775

Please sign in to comment.