Skip to content

Commit

Permalink
Use Device.role instead of Device.device_role where available (Netbox…
Browse files Browse the repository at this point in the history
… >=3.6.0) (#133)

Fixes #132

Signed-off-by: Brian Candler <[email protected]>
  • Loading branch information
candlerb authored Sep 5, 2023
1 parent 912fa4e commit ff2b024
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
- v3.3.2
- v3.4.4
- v3.5.3
- v3.6.0
- master

steps:
Expand Down
5 changes: 4 additions & 1 deletion netbox_prometheus_sd/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ def get_labels(self, obj):
utils.extract_rack(obj, labels)
utils.extract_custom_fields(obj, labels)

if hasattr(obj, "device_role") and obj.device_role is not None:
if hasattr(obj, "role") and obj.role is not None:
labels["role"] = obj.role.name
labels["role_slug"] = obj.role.slug
elif hasattr(obj, "device_role") and obj.device_role is not None: # netbox <3.6.0
labels["role"] = obj.device_role.name
labels["role_slug"] = obj.device_role.slug

Expand Down
2 changes: 1 addition & 1 deletion netbox_prometheus_sd/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class VirtualMachineViewSet(
class DeviceViewSet(NetboxPrometheusSDModelViewSet): # pylint: disable=too-many-ancestors
queryset = Device.objects.prefetch_related(
"device_type__manufacturer",
"device_role",
"role" if hasattr(Device, "role") else "device_role",
"tenant",
"platform",
"site",
Expand Down
7 changes: 4 additions & 3 deletions netbox_prometheus_sd/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,9 @@ def build_vm_full(name):


def build_minimal_device(name):
role_attr = "role" if hasattr(Device, "role") else "device_role"
return Device.objects.get_or_create(
name=name,
device_role=DeviceRole.objects.get_or_create(name="Firewall", slug="firewall")[
0
],
device_type=DeviceType.objects.get_or_create(
model="SRX",
slug="srx",
Expand All @@ -105,6 +103,9 @@ def build_minimal_device(name):
)[0],
)[0],
site=Site.objects.get_or_create(name="Site", slug="site")[0],
**{
role_attr: DeviceRole.objects.get_or_create(name="Firewall", slug="firewall")[0],
}
)[0]

def build_device_config_context_no_array(name):
Expand Down

0 comments on commit ff2b024

Please sign in to comment.