Skip to content

Commit

Permalink
Fixes #10950: Fix validation of VDC primary IPs
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Dec 9, 2022
1 parent 3bc9586 commit b2f34ce
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/release-notes/version-3.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

* [#10946](https://github.com/netbox-community/netbox/issues/10946) - Fix AttributeError exception when viewing a device with a primary IP and no platform assigned
* [#10948](https://github.com/netbox-community/netbox/issues/10948) - Linkify primary IPs for VDCs
* [#10950](https://github.com/netbox-community/netbox/issues/10950) - Fix validation of VDC primary IPs
* [#10957](https://github.com/netbox-community/netbox/issues/10957) - Add missing VDCs column to interface tables
* [#10973](https://github.com/netbox-community/netbox/issues/10973) - Fix device links in VDC table
* [#10980](https://github.com/netbox-community/netbox/issues/10980) - Fix view tabs for plugin objects
Expand Down
18 changes: 18 additions & 0 deletions netbox/dcim/forms/model_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1705,6 +1705,24 @@ class VirtualDeviceContextForm(TenancyForm, NetBoxModelForm):
'rack_id': '$rack',
}
)
primary_ip4 = DynamicModelChoiceField(
queryset=IPAddress.objects.all(),
label='Primary IPv4',
required=False,
query_params={
'device_id': '$device',
'family': '4',
}
)
primary_ip6 = DynamicModelChoiceField(
queryset=IPAddress.objects.all(),
label='Primary IPv6',
required=False,
query_params={
'device_id': '$device',
'family': '6',
}
)

fieldsets = (
('Assigned Device', ('region', 'site_group', 'site', 'location', 'rack', 'device')),
Expand Down
18 changes: 17 additions & 1 deletion netbox/dcim/models/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from functools import cached_property

from django.apps import apps
from django.contrib.contenttypes.fields import GenericRelation
from django.core.exceptions import ValidationError
from django.core.validators import MaxValueValidator, MinValueValidator
Expand Down Expand Up @@ -1175,3 +1174,20 @@ def primary_ip(self):
return self.primary_ip4
else:
return None

def clean(self):
super().clean()

# Validate primary IPv4/v6 assignment
for primary_ip, family in ((self.primary_ip4, 4), (self.primary_ip6, 6)):
if not primary_ip:
continue
if primary_ip.family != family:
raise ValidationError({
f'primary_ip{family}': f"{primary_ip} is not an IPv{family} address."
})
device_interfaces = self.device.vc_interfaces(if_master=False)
if primary_ip.assigned_object not in device_interfaces:
raise ValidationError({
f'primary_ip{family}': _('Primary IP address must belong to an interface on the assigned device.')
})

0 comments on commit b2f34ce

Please sign in to comment.