You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to validate configuration context data for devices and I was wondering if it's possible to do that with custom validation rules?
I'm trying to check that certain fields/values are defined in the configuration context data before it is applied to a device.
Currently, I'm doing something along these lines but I'm not sure if that is the right approach for what I want to do.
from extras.validators import CustomValidator
from typing import Any
import logging
logger = logging.getLogger('netbox.plugins')
class DeviceAddrsValidator(CustomValidator):
def validate(self, instance: Any, request: Any) -> None:
"""
Validate modifications to devices and their configuration context data.
Args:
instance: The device object being validated
request: The HTTP request object
Returns:
None
Raises:
ValidationError: If validation fails
"""
from dcim.models import Interface
from dcim.models import Device
from django.db.models import QuerySet
ifaces = Interface.objects.filter(device_id=instance.id)
conf_ctx = instance.get_config_context()
roles = ['Leaf', 'Spine', 'Switch', 'oob-mgmt-switch']
if 'inband_management' in config_context:
inband_mgmt_present = True
inband_iface_name = conf_ctx['inband_management']['interface']
if isinstance(inband_iface_name, list):
self.fail(f"There can only be one inband-management interface")
else:
inband_mgmt_present = False
if 'dns' in conf_ctx:
if not isinstance(dns['servers'], list):
self.fail(f"No DNS servers in config context")
else:
self.fail(f"DNS data not in config context")
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi all,
I'm trying to validate configuration context data for devices and I was wondering if it's possible to do that with custom validation rules?
I'm trying to check that certain fields/values are defined in the configuration context data before it is applied to a device.
Currently, I'm doing something along these lines but I'm not sure if that is the right approach for what I want to do.
Beta Was this translation helpful? Give feedback.
All reactions