From 93d22c8fd02507c6fd167a660877d41963ff3dd9 Mon Sep 17 00:00:00 2001 From: koalajoe23 Date: Fri, 25 Jul 2025 08:53:31 +0200 Subject: [PATCH] feat: return parent's status for services --- netbox_prometheus_sd/api/serializers.py | 6 +++--- netbox_prometheus_sd/api/utils.py | 6 ++++++ netbox_prometheus_sd/tests/test_serializers.py | 5 +++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/netbox_prometheus_sd/api/serializers.py b/netbox_prometheus_sd/api/serializers.py index a0900ca..23195da 100644 --- a/netbox_prometheus_sd/api/serializers.py +++ b/netbox_prometheus_sd/api/serializers.py @@ -74,7 +74,6 @@ class Meta: def get_labels(self, obj): labels = LabelDict( { - "status": obj.status, "model": obj.__class__.__name__, "name": obj.name, "id": str(obj.id), @@ -94,6 +93,7 @@ def get_labels(self, obj): utils.extract_rack(obj, labels) utils.extract_custom_fields(obj, labels) utils.extract_rack_u_poistion(obj, labels) + utils.extract_status(obj, labels) if hasattr(obj, "role") and obj.role is not None: labels["role"] = obj.role.name @@ -132,7 +132,6 @@ class Meta: def get_labels(self, obj): labels = LabelDict( { - "status": obj.status, "model": obj.__class__.__name__, "name": obj.name, "id": str(obj.id), @@ -147,6 +146,7 @@ def get_labels(self, obj): utils.extract_services(obj, labels) utils.extract_contacts(obj, labels) utils.extract_custom_fields(obj, labels) + utils.extract_status(obj, labels) if hasattr(obj, "role") and obj.role is not None: labels["role"] = obj.role.name @@ -208,7 +208,6 @@ def get_labels(self, obj): """Get IP address labels""" labels = LabelDict( { - "status": obj.status, "model": obj.__class__.__name__, "ip": self.extract_ip(obj), "id": str(obj.id), @@ -217,6 +216,7 @@ def get_labels(self, obj): if obj.role: labels["role"] = obj.role + utils.extract_status(obj, labels) utils.extract_tags(obj, labels) utils.extract_tenant(obj, labels) utils.extract_custom_fields(obj, labels) diff --git a/netbox_prometheus_sd/api/utils.py b/netbox_prometheus_sd/api/utils.py index 7eedfe9..e44002e 100644 --- a/netbox_prometheus_sd/api/utils.py +++ b/netbox_prometheus_sd/api/utils.py @@ -168,6 +168,7 @@ def extract_parent(obj, labels: LabelDict): extract_tenant(obj.parent, labels) extract_cluster(obj.parent, labels) extract_contacts(obj.parent, labels) + extract_status(obj.parent, labels) def extract_service_ips(obj, labels: LabelDict): @@ -190,3 +191,8 @@ def extract_rack_u_poistion(obj, labels: LabelDict): """Extract rack U poistion""" if hasattr(obj, "position") and obj.position: labels["rack_u_position"] = str(obj.position) + +def extract_status(obj, labels: LabelDict): + """Extract status""" + if hasattr(obj, "status") and obj.status is not None: + labels["status"] = obj.status \ No newline at end of file diff --git a/netbox_prometheus_sd/tests/test_serializers.py b/netbox_prometheus_sd/tests/test_serializers.py index 8970d9f..069c7f3 100755 --- a/netbox_prometheus_sd/tests/test_serializers.py +++ b/netbox_prometheus_sd/tests/test_serializers.py @@ -493,6 +493,11 @@ def test_device_service_full_to_target(self): {"__meta_netbox_primary_ip6": "2001:db8:1701::2"}, data["labels"] ) ) + self.assertTrue( + utils.dictContainsSubset( + {"__meta_netbox_status": "active"}, data["labels"] + ) + ) def test_vm_service_full_to_target(self): vm = utils.build_vm_full("vm-full-01.example.com")