Skip to content

Commit 4f90642

Browse files
authored
Update nb_inventory to support NetBox 3.5 (#999)
* Enable CI for 3.5
1 parent 8fa45c8 commit 4f90642

14 files changed

+1322
-1310
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ jobs:
8080
NETBOX_DOCKER_VERSION: 2.3.0
8181
- VERSION: "v3.4"
8282
NETBOX_DOCKER_VERSION: 2.5.3
83-
#- VERSION: "v3.5"
84-
# NETBOX_DOCKER_VERSION: 2.6.1
83+
- VERSION: "v3.5"
84+
NETBOX_DOCKER_VERSION: 2.6.1
8585
# If we want to integration test wiht all supported Python:
8686
#python-version: ["3.9", "3.10", "3.11"]
8787

plugins/inventory/nb_inventory.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,23 +1498,39 @@ def fetch_api_docs(self):
14981498
cached_api_version = openapi.get("info", {}).get("version")
14991499

15001500
if netbox_api_version != cached_api_version:
1501-
openapi = self._fetch_information(
1502-
self.api_endpoint + "/api/docs/?format=openapi"
1503-
)
1501+
if version.parse(netbox_api_version) >= version.parse("3.5.0"):
1502+
endpoint_url = self.api_endpoint + "/api/schema/?format=json"
1503+
else:
1504+
endpoint_url = self.api_endpoint + "/api/docs/?format=openapi"
15041505

1506+
openapi = self._fetch_information(endpoint_url)
15051507
with open(tmp_file, "w") as file:
15061508
json.dump(openapi, file)
15071509

1508-
self.api_version = version.parse(openapi["info"]["version"])
1509-
self.allowed_device_query_parameters = [
1510-
p["name"] for p in openapi["paths"]["/dcim/devices/"]["get"]["parameters"]
1511-
]
1512-
self.allowed_vm_query_parameters = [
1513-
p["name"]
1514-
for p in openapi["paths"]["/virtualization/virtual-machines/"]["get"][
1515-
"parameters"
1510+
self.api_version = version.parse(netbox_api_version)
1511+
1512+
if self.api_version >= version.parse("3.5.0"):
1513+
self.allowed_device_query_parameters = [
1514+
p["name"]
1515+
for p in openapi["paths"]["/api/dcim/devices/"]["get"]["parameters"]
1516+
]
1517+
self.allowed_vm_query_parameters = [
1518+
p["name"]
1519+
for p in openapi["paths"]["/api/virtualization/virtual-machines/"][
1520+
"get"
1521+
]["parameters"]
1522+
]
1523+
else:
1524+
self.allowed_device_query_parameters = [
1525+
p["name"]
1526+
for p in openapi["paths"]["/dcim/devices/"]["get"]["parameters"]
1527+
]
1528+
self.allowed_vm_query_parameters = [
1529+
p["name"]
1530+
for p in openapi["paths"]["/virtualization/virtual-machines/"]["get"][
1531+
"parameters"
1532+
]
15161533
]
1517-
]
15181534

15191535
def validate_query_parameter(self, parameter, allowed_query_parameters):
15201536
if not (isinstance(parameter, dict) and len(parameter) == 1):

poetry.lock

Lines changed: 1015 additions & 1015 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration/targets/inventory-v3.5/files/test-inventory-jinja2-filter.json

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@
237237
"parent-region"
238238
],
239239
"role": "core-switch",
240+
"serial": "",
240241
"services": [
241242
{
242243
"comments": "",
@@ -268,8 +269,7 @@
268269
"label": "Active",
269270
"value": "active"
270271
},
271-
"tags": [],
272-
"serial": ""
272+
"tags": []
273273
},
274274
"TestDeviceR1": {
275275
"config_context": {},
@@ -288,17 +288,18 @@
288288
"parent-region"
289289
],
290290
"role": "core-switch",
291+
"serial": "FAB12345678",
291292
"services": [],
292293
"site": "test-site",
293294
"site_groups": [],
294295
"status": {
295296
"label": "Active",
296297
"value": "active"
297298
},
298-
"tags": [],
299-
"serial": "FAB12345678"
299+
"tags": []
300300
},
301301
"test100": {
302+
"asset_tag": "123456789",
302303
"config_context": {
303304
"ntp_servers": [
304305
"pool.ntp.org"
@@ -540,6 +541,7 @@
540541
"parent-region"
541542
],
542543
"role": "core-switch",
544+
"serial": "FAB01234567",
543545
"services": [
544546
{
545547
"comments": "",
@@ -607,9 +609,7 @@
607609
"label": "Active",
608610
"value": "active"
609611
},
610-
"tags": [],
611-
"serial": "FAB01234567",
612-
"asset_tag": "123456789"
612+
"tags": []
613613
},
614614
"test100-vm": {
615615
"cluster": "Test Cluster",
@@ -965,25 +965,25 @@
965965
},
966966
"all": {
967967
"children": [
968-
"cluster_Test_Cluster",
969-
"cluster_group_test_cluster_group",
970-
"cluster_type_test_cluster_type",
971-
"device_type_cisco_test",
972-
"device_type_nexus_parent",
973-
"is_virtual",
974-
"manufacturer_cisco",
975-
"rack_Test_Rack",
968+
"ungrouped",
969+
"site_test_site2",
976970
"region_other_region",
977971
"region_parent_region",
978-
"role_core_switch",
979-
"service_http",
980-
"service_ssh",
981-
"service_telnet",
982972
"site_group_other_site_group",
983973
"site_group_parent_site_group",
984-
"site_test_site2",
974+
"role_core_switch",
975+
"device_type_nexus_parent",
976+
"manufacturer_cisco",
977+
"service_telnet",
985978
"status_active",
986-
"ungrouped"
979+
"rack_Test_Rack",
980+
"device_type_cisco_test",
981+
"service_ssh",
982+
"service_http",
983+
"cluster_Test_Cluster",
984+
"cluster_group_test_cluster_group",
985+
"cluster_type_test_cluster_type",
986+
"is_virtual"
987987
]
988988
},
989989
"cluster_Test_Cluster": {
@@ -1010,14 +1010,6 @@
10101010
"test103-vm"
10111011
]
10121012
},
1013-
"is_virtual": {
1014-
"hosts": [
1015-
"test100-vm",
1016-
"test101-vm",
1017-
"test102-vm",
1018-
"test103-vm"
1019-
]
1020-
},
10211013
"device_type_cisco_test": {
10221014
"hosts": [
10231015
"TestDeviceR1",
@@ -1029,6 +1021,14 @@
10291021
"Test Nexus One"
10301022
]
10311023
},
1024+
"is_virtual": {
1025+
"hosts": [
1026+
"test100-vm",
1027+
"test101-vm",
1028+
"test102-vm",
1029+
"test103-vm"
1030+
]
1031+
},
10321032
"location_parent_rack_group": {
10331033
"children": [
10341034
"location_test_rack_group"
@@ -1091,14 +1091,14 @@
10911091
]
10921092
},
10931093
"site_test_site": {
1094+
"children": [
1095+
"location_parent_rack_group"
1096+
],
10941097
"hosts": [
10951098
"test100-vm",
10961099
"test101-vm",
10971100
"test102-vm",
10981101
"test103-vm"
1099-
],
1100-
"children": [
1101-
"location_parent_rack_group"
11021102
]
11031103
},
11041104
"status_active": {

0 commit comments

Comments
 (0)