From 518ee85d9e6cbd35d2cd05471474f296d7452fd6 Mon Sep 17 00:00:00 2001 From: yash-pal1 Date: Tue, 10 Oct 2023 17:58:07 +0530 Subject: [PATCH] validation of ip address for device and virtual machine --- netbox/ipam/forms/bulk_import.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/netbox/ipam/forms/bulk_import.py b/netbox/ipam/forms/bulk_import.py index 661d28990..a6ec544cf 100644 --- a/netbox/ipam/forms/bulk_import.py +++ b/netbox/ipam/forms/bulk_import.py @@ -516,7 +516,21 @@ class ServiceImportForm(NetBoxModelImportForm): class Meta: model = Service - fields = ('device', 'virtual_machine', 'ipaddresses', 'name', 'protocol', 'ports', 'description', 'comments', 'tags') + fields = ( + 'device', 'virtual_machine', 'ipaddresses', 'name', 'protocol', 'ports', 'description', 'comments', 'tags') + + def clean_ipaddresses(self): + + device = self.cleaned_data.get('device') + virtual_machine = self.cleaned_data.get('virtual_machine') + + for ip_address in self.cleaned_data.get('ipaddresses'): + if device and ip_address != device.primary_ip4: + raise forms.ValidationError("Device should assign to be an ip address") + if virtual_machine and ip_address != virtual_machine.primary_ip4: + raise forms.ValidationError("Virtual Machine should assign to be an ip address") + + return self.cleaned_data class L2VPNImportForm(NetBoxModelImportForm):