Skip to content

Commit a4cabd0

Browse files
authored
Inventory: Quickfix the RackGroup API URL change (#496)
1 parent 93bcdf9 commit a4cabd0

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

plugins/inventory/nb_inventory.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@
249249
from itertools import chain
250250
from collections import defaultdict
251251
from ipaddress import ip_interface
252+
from packaging import specifiers, version
252253

253254
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable
254255
from ansible.module_utils.ansible_release import __version__ as ansible_version
@@ -369,7 +370,7 @@ def query_string(value, separator="&"):
369370
if chunk_size < 1:
370371
chunk_size = 1
371372

372-
if self.api_version == "2.6":
373+
if self.api_version in specifiers.SpecifierSet("~=2.6.0"):
373374
# Issue netbox-community/netbox#3507 was fixed in v2.7.5
374375
# If using NetBox v2.7.0-v2.7.4 will have to manually set max_uri_length to 0,
375376
# but it's probably faster to keep fetch_all: True
@@ -812,7 +813,16 @@ def get_role_for_rack(rack):
812813
self.racks_role_lookup = dict(map(get_role_for_rack, racks))
813814

814815
def refresh_rack_groups_lookup(self):
815-
url = self.api_endpoint + "/api/dcim/rack-groups/?limit=0"
816+
if self.api_version >= version.parse("2.11"):
817+
# In NetBox v2.11 Breaking Changes:
818+
# The RackGroup model has been renamed to Location
819+
# (see netbox-community/netbox#4971).
820+
# Its REST API endpoint has changed from /api/dcim/rack-groups/
821+
# to /api/dcim/locations/
822+
# https://netbox.readthedocs.io/en/stable/release-notes/#v2110-2021-04-16
823+
url = self.api_endpoint + "/api/dcim/locations/?limit=0"
824+
else:
825+
url = self.api_endpoint + "/api/dcim/rack-groups/?limit=0"
816826
rack_groups = self.get_resource_list(api_url=url)
817827
self.rack_groups_lookup = dict(
818828
(rack_group["id"], rack_group["slug"]) for rack_group in rack_groups
@@ -1115,7 +1125,7 @@ def fetch_api_docs(self):
11151125
self.api_endpoint + "/api/docs/?format=openapi"
11161126
)
11171127

1118-
self.api_version = openapi["info"]["version"]
1128+
self.api_version = version.parse(openapi["info"]["version"])
11191129
self.allowed_device_query_parameters = [
11201130
p["name"] for p in openapi["paths"]["/dcim/devices/"]["get"]["parameters"]
11211131
]

poetry.lock

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

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ mock = "^4.0.2"
2424
antsibull = "^0.25.0"
2525
importlib-metadata = "1.7.0"
2626
pylint = "^2.6.0"
27+
packaging = "^20.9"
2728

2829
[tool.poetry.dev-dependencies]
2930

tests/unit/inventory/test_nb_inventory.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import os
1111
from functools import partial
1212
from unittest.mock import patch, MagicMock, Mock, call
13+
from packaging import version
1314

1415
try:
1516
from ansible_collections.netbox.netbox.plugins.inventory.nb_inventory import (
@@ -40,7 +41,7 @@ def inventory_fixture(
4041
inventory.api_endpoint = "https://netbox.test.endpoint:1234"
4142

4243
# Fill in data that is fetched dynamically
43-
inventory.api_version = None
44+
inventory.api_version = version.Version("2.0")
4445
inventory.allowed_device_query_parameters = allowed_device_query_parameters_fixture
4546
inventory.allowed_vm_query_parameters = allowed_vm_query_parameters_fixture
4647

0 commit comments

Comments
 (0)