Skip to content

Commit

Permalink
feat: Implement custom fields
Browse files Browse the repository at this point in the history
Add custom fields to lables on IPs, VMs and devices
  • Loading branch information
Felix Peters authored and FlxPeters committed Sep 14, 2022
1 parent 32a26f6 commit bbea5d0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
8 changes: 3 additions & 5 deletions netbox_prometheus_sd/api/serializers.py
Original file line number Diff line number Diff line change
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_custom_fields(obj, labels)

if hasattr(obj, "device_role") and obj.device_role is not None:
labels["role"] = obj.device_role.name
Expand All @@ -47,11 +48,6 @@ def get_labels(self, obj):
labels["site"] = obj.site.name
labels["site_slug"] = obj.site.slug

if hasattr(obj, "custom_field_data") and obj.custom_field_data is not None:
for key, value in obj.custom_field_data.items():
if value is not None:
labels["custom_field_" + key.lower()] = value

return labels.get_labels()


Expand Down Expand Up @@ -80,6 +76,7 @@ def get_labels(self, obj):
utils.extract_cluster(obj, labels)
utils.extract_services(obj, labels)
utils.extract_contacts(obj, labels)
utils.extract_custom_fields(obj, labels)

if hasattr(obj, "role") and obj.role is not None:
labels["role"] = obj.role.name
Expand Down Expand Up @@ -121,5 +118,6 @@ def get_labels(self, obj):

utils.extract_tags(obj, labels)
utils.extract_tenant(obj, labels)
utils.extract_custom_fields(obj, labels)

return labels.get_labels()
6 changes: 6 additions & 0 deletions netbox_prometheus_sd/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,9 @@ def extract_contacts(obj, labels: LabelDict):
labels[f"contact_{contact.priority}_comments"] = contact.contact.comments
if hasattr(contact, "role") and contact.role is not None:
labels[f"contact_{contact.priority}_role"] = contact.role.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():
if value is not None:
labels["custom_field_" + key.lower()] = value
11 changes: 9 additions & 2 deletions netbox_prometheus_sd/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ def test_vm_full_to_target(self):
self.assertDictContainsSubset(
{"__meta_netbox_primary_ip6": "2001:db8:1701::2"}, data["labels"]
)


self.assertDictContainsSubset(
{"__meta_netbox_custom_field_customer_id": "foobar-123"}, data["labels"]
)


class PrometheusDeviceSerializerTests(TestCase):
Expand Down Expand Up @@ -129,6 +130,9 @@ def test_device_full_to_target(self):
self.assertDictContainsSubset(
{"__meta_netbox_tenant_slug": "acme"}, data["labels"]
)
self.assertDictContainsSubset(
{"__meta_netbox_custom_field_customer_id": "foobar-123"}, data["labels"]
)


class PrometheusIPAddressSerializerTests(TestCase):
Expand Down Expand Up @@ -180,3 +184,6 @@ def test_ip_full_to_target(self):
self.assertDictContainsSubset(
{"__meta_netbox_tenant_group_slug": "federation"}, data["labels"]
)
self.assertDictContainsSubset(
{"__meta_netbox_custom_field_customer_id": "foobar-123"}, data["labels"]
)
6 changes: 6 additions & 0 deletions netbox_prometheus_sd/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ def build_tenant():
return Tenant.objects.get_or_create(name="Acme Corp.", slug="acme")[0]


def build_custom_fields():
return { "customer_id": "foobar-123"}

def build_minimal_vm(name):
return VirtualMachine.objects.get_or_create(name=name, cluster=build_cluster())[0]


def build_vm_full(name):
vm = build_minimal_vm(name=name)
vm.tenant = build_tenant()
vm.custom_field_data = build_custom_fields()
vm.role = DeviceRole.objects.get_or_create(name="VM", slug="vm", vm_role=True)[0]
vm.platform = Platform.objects.get_or_create(
name="Ubuntu 20.04", slug="ubuntu-20.04"
Expand Down Expand Up @@ -65,6 +69,7 @@ def build_minimal_device(name):
def build_device_full(name):
device = build_minimal_device(name)
device.tenant = build_tenant()
device.custom_field_data = build_custom_fields()
device.platform = Platform.objects.get_or_create(name="Junos", slug="junos")[0]
device.primary_ip4 = IPAddress.objects.get_or_create(address="192.168.0.1/24")[0]
device.primary_ip6 = IPAddress.objects.get_or_create(address="2001:db8:1701::2/64")[
Expand All @@ -81,6 +86,7 @@ def build_minimal_ip(address):

def build_full_ip(address, dns_name=""):
ip = build_minimal_ip(address=address)
ip.custom_field_data = build_custom_fields()
ip.tenant = Tenant.objects.get_or_create(
name="Starfleet",
slug="starfleet",
Expand Down

0 comments on commit bbea5d0

Please sign in to comment.