Skip to content

Commit

Permalink
Added oob_ip field to Device
Browse files Browse the repository at this point in the history
Fixes #129
  • Loading branch information
k0ste committed Sep 7, 2023
1 parent ff2b024 commit dceb343
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions example/initializers/devices.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions example/initializers/interfaces.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@
enabled: true
type: virtual
name: to-server01
- device: server02
enabled: true
type: 1000base-t
name: ipmi
4 changes: 4 additions & 0 deletions example/initializers/ip_addresses.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions netbox_prometheus_sd/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions netbox_prometheus_sd/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions netbox_prometheus_sd/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ def test_vm_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_custom_field_simple": "Foobar 123"}, data["labels"]
)
Expand Down Expand Up @@ -187,6 +190,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"]
Expand Down Expand Up @@ -292,3 +298,6 @@ def test_service_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"]
)
1 change: 1 addition & 0 deletions netbox_prometheus_sd/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit dceb343

Please sign in to comment.