Add IPv6 support for assigning a ServicePort to all device's IPs

This commit is contained in:
Iva Kaneva 2016-11-17 22:53:07 +02:00
parent 4bf59c982e
commit 53ef9aa02e
2 changed files with 9 additions and 5 deletions

View File

@ -9,7 +9,7 @@ import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('ipam', '0009_ipaddress_add_status'),
('ipam', '0010_ipaddress_help_texts'),
]
operations = [

View File

@ -448,11 +448,11 @@ class ServicePort(CreatedUpdatedModel):
A ServicePort is always associated with a specific IPAddress on a Device.
If an user wants to specify a service running on all IP Addresses on a device,
this can be done by assigning the port to the '0.0.0.0/32' IPAddress.
this can be done by assigning the port to the '0.0.0.0/32' or '::/128' IPAddress.
The combination of IPAddress, Port Number and Port Protocol is always unique for ServicePort.
If a port number + port protocol combination is assigned to '0.0.0.0/32' IPAddress,
If a port number + port protocol combination is assigned to '0.0.0.0/32' or '::/128' IPAddress,
it cannot be assigned to any other IPAddress on the same Device.
"""
@ -484,12 +484,16 @@ class ServicePort(CreatedUpdatedModel):
return None
def clean(self):
# if port is already assigned on '0.0.0.0/32'
# if port is already assigned on '0.0.0.0/32' or '::/128'
# that means it is assigned on all IPs on the device
port_assigned_on_all_ips = bool(ServicePort.objects.filter(
ip_address__address='::/128', port=self.port, protocol=self.protocol).exclude(pk=self.id))
port_assigned_on_all_v4_ips = bool(ServicePort.objects.filter(
ip_address__address='0.0.0.0/32', port=self.port, protocol=self.protocol).exclude(pk=self.id))
if port_assigned_on_all_ips:
raise ValidationError('Port already assigned on address 0.0.0.0/24')
raise ValidationError('Port already assigned on address ::/128')
elif port_assigned_on_all_v4_ips and self.ip_address.family == 4:
raise ValidationError('Port already assigned on address 0.0.0.0/32')
def save(self, *args, **kwargs):
super(ServicePort, self).save(*args, **kwargs)