Skip to content

Commit

Permalink
Adding rack label device serializer 2 (#110)
Browse files Browse the repository at this point in the history
* Adding rack label to device serialized if existing (with extract method)

* Trying to add tests for Rack Label in Device serializer

* Fixing unit test for rack label

Co-authored-by: Arthur Outhenin-Chalandre <arthur.outhenin-chalandre@proton.ch>

---------

Co-authored-by: geg347 <geg347@users.noreply.gihub.com>
Co-authored-by: Arthur Outhenin-Chalandre <arthur.outhenin-chalandre@proton.ch>
  • Loading branch information
3 people committed Apr 11, 2023
1 parent 5ac784c commit 50e5f3d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions netbox_prometheus_sd/api/serializers.py
Expand Up @@ -34,6 +34,7 @@ def get_labels(self, obj):
utils.extract_cluster(obj, labels)
utils.extract_services(obj, labels)
utils.extract_contacts(obj, labels)
utils.extract_rack(obj, labels)
utils.extract_custom_fields(obj, labels)

if hasattr(obj, "device_role") and obj.device_role is not None:
Expand Down
5 changes: 5 additions & 0 deletions netbox_prometheus_sd/api/utils.py
Expand Up @@ -89,6 +89,11 @@ def extract_contacts(obj, labels: LabelDict):
if hasattr(contact, "role") and contact.role is not None:
labels[f"contact_{contact.priority}_role"] = contact.role.name

def extract_rack(obj, labels: LabelDict):
"""Extract rack"""
if hasattr(obj, "rack") and obj.rack:
labels["rack"] = obj.rack.name

def extract_custom_fields(obj, labels: LabelDict):
if hasattr(obj, "custom_field_data") and obj.custom_field_data is not None:
for key, value in obj.custom_field_data.items():
Expand Down
1 change: 1 addition & 0 deletions netbox_prometheus_sd/tests/test_serializers.py 100644 → 100755
Expand Up @@ -151,6 +151,7 @@ def test_device_full_to_target(self):
self.assertDictContainsSubset(
{"__meta_netbox_primary_ip6": "2001:db8:1701::2"}, data["labels"]
)
self.assertDictContainsSubset({"__meta_netbox_rack": "R01B01"}, data["labels"])
self.assertDictContainsSubset(
{"__meta_netbox_tenant": "Acme Corp."}, data["labels"]
)
Expand Down
5 changes: 4 additions & 1 deletion netbox_prometheus_sd/tests/utils.py
@@ -1,6 +1,6 @@
from dcim.models.devices import DeviceType, Manufacturer
from dcim.models.sites import Site
from dcim.models import Device, DeviceRole, Platform
from dcim.models import Device, DeviceRole, Platform, Rack

from ipam.models import IPAddress
from tenancy.models import Tenant, TenantGroup
Expand Down Expand Up @@ -99,6 +99,9 @@ def build_device_full(name):
device.primary_ip6 = IPAddress.objects.get_or_create(address="2001:db8:1701::2/64")[
0
]
device.rack = Rack.objects.get_or_create(
name="R01B01", site=Site.objects.get_or_create(name="Site", slug="site")[0]
)[0]
device.tags.add("Tag1")
device.tags.add("Tag 2")
return device
Expand Down

0 comments on commit 50e5f3d

Please sign in to comment.