From 2190579f26a88596ff943aaab9815e6fbf3cb91d Mon Sep 17 00:00:00 2001 From: Konstantin Shalygin Date: Thu, 7 Sep 2023 12:28:28 +0300 Subject: [PATCH] Added oob_ip field to Device Fixes #129 --- example/initializers/devices.yml | 1 + example/initializers/interfaces.yml | 4 ++++ example/initializers/ip_addresses.yml | 4 ++++ netbox_prometheus_sd/api/serializers.py | 1 + netbox_prometheus_sd/api/utils.py | 6 ++++++ netbox_prometheus_sd/tests/test_serializers.py | 3 +++ netbox_prometheus_sd/tests/utils.py | 1 + 7 files changed, 20 insertions(+) diff --git a/example/initializers/devices.yml b/example/initializers/devices.yml index cfdea90..407fa47 100644 --- a/example/initializers/devices.yml +++ b/example/initializers/devices.yml @@ -29,6 +29,7 @@ position: 2 primary_ip4: 10.1.1.2/24 primary_ip6: 2001:db8:a000:1::2/64 + oob_ip: 10.0.0.1/24 custom_field_data: text_field: Description - name: server03 diff --git a/example/initializers/interfaces.yml b/example/initializers/interfaces.yml index 82861ce..698cc15 100644 --- a/example/initializers/interfaces.yml +++ b/example/initializers/interfaces.yml @@ -16,3 +16,7 @@ enabled: true type: virtual name: to-server01 +- device: server02 + enabled: true + type: 1000base-t + name: ipmi diff --git a/example/initializers/ip_addresses.yml b/example/initializers/ip_addresses.yml index 44a355e..40d254e 100644 --- a/example/initializers/ip_addresses.yml +++ b/example/initializers/ip_addresses.yml @@ -32,6 +32,10 @@ device: server02 interface: to-server01 status: active +- address: 10.0.0.1/24 + device: server02 + interface: ipmi + status: active - address: 10.1.1.10/24 description: reserved IP status: reserved diff --git a/netbox_prometheus_sd/api/serializers.py b/netbox_prometheus_sd/api/serializers.py index 422ee49..c2bc60b 100644 --- a/netbox_prometheus_sd/api/serializers.py +++ b/netbox_prometheus_sd/api/serializers.py @@ -73,6 +73,7 @@ def get_labels(self, obj): ) utils.extract_primary_ip(obj, labels) + utils.extract_oob_ip(obj, labels) utils.extracts_platform(obj, labels) utils.extract_tags(obj, labels) utils.extract_tenant(obj, labels) diff --git a/netbox_prometheus_sd/api/utils.py b/netbox_prometheus_sd/api/utils.py index 1b0d72b..fce95b0 100644 --- a/netbox_prometheus_sd/api/utils.py +++ b/netbox_prometheus_sd/api/utils.py @@ -61,6 +61,11 @@ def extract_primary_ip(obj, labels: LabelDict): labels["primary_ip6"] = str(IPNetwork(obj.primary_ip6.address).ip) +def extract_oob_ip(obj, labels: LabelDict): + if getattr(obj, "oob_ip", None) is not None: + labels["oob_ip"] = str(IPNetwork(obj.oob_ip.address).ip) + + def extracts_platform(obj, label: LabelDict): if hasattr(obj, "platform") and obj.platform is not None: label["platform"] = obj.platform.name @@ -124,6 +129,7 @@ def extract_prometheus_sd_config(obj, labels): def extract_parent(obj, labels: LabelDict): labels['parent'] = obj.parent.name extract_primary_ip(obj.parent, labels) + extract_oob_ip(obj.parent, labels) extract_tenant(obj.parent, labels) extract_cluster(obj.parent, labels) extract_contacts(obj.parent, labels) diff --git a/netbox_prometheus_sd/tests/test_serializers.py b/netbox_prometheus_sd/tests/test_serializers.py index 6e2d9ce..7a7e7bd 100755 --- a/netbox_prometheus_sd/tests/test_serializers.py +++ b/netbox_prometheus_sd/tests/test_serializers.py @@ -187,6 +187,9 @@ def test_device_full_to_target(self): self.assertDictContainsSubset( {"__meta_netbox_primary_ip6": "2001:db8:1701::2"}, data["labels"] ) + self.assertDictContainsSubset( + {"__meta_netbox_oob_ip": "10.0.0.1"}, data["labels"] + ) self.assertDictContainsSubset({"__meta_netbox_rack": "R01B01"}, data["labels"]) self.assertDictContainsSubset( {"__meta_netbox_tenant": "Acme Corp."}, data["labels"] diff --git a/netbox_prometheus_sd/tests/utils.py b/netbox_prometheus_sd/tests/utils.py index d1ff8e8..672e8c9 100644 --- a/netbox_prometheus_sd/tests/utils.py +++ b/netbox_prometheus_sd/tests/utils.py @@ -176,6 +176,7 @@ def build_device_full(name): device.primary_ip6 = IPAddress.objects.get_or_create(address="2001:db8:1701::2/64")[ 0 ] + device.oob_ip = IPAddress.objects.get_or_create(address="10.0.0.1/24")[0] device.rack = Rack.objects.get_or_create( name="R01B01", site=Site.objects.get_or_create(name="Site", slug="site")[0] )[0]