8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-23 20:03:11 +00:00

Utilise des ranges en lieu et place des slash

This commit is contained in:
Gabriel Detraz 2017-08-29 00:35:03 +02:00 committed by root
parent 8108767260
commit 27d6823532
4 changed files with 51 additions and 16 deletions

View file

@ -143,7 +143,8 @@ class DelMachineTypeForm(Form):
class IpTypeForm(ModelForm): class IpTypeForm(ModelForm):
class Meta: class Meta:
model = IpType model = IpType
fields = ['type','extension','need_infra','domaine_ip','domaine_range', 'vlan'] fields = ['type','extension','need_infra','domaine_ip_start','domaine_ip_stop', 'vlan']
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(IpTypeForm, self).__init__(*args, **kwargs) super(IpTypeForm, self).__init__(*args, **kwargs)

View file

@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2017-08-28 21:22
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('machines', '0051_iptype_vlan'),
]
operations = [
migrations.RenameField(
model_name='iptype',
old_name='domaine_ip',
new_name='domaine_ip_start',
),
migrations.RemoveField(
model_name='iptype',
name='domaine_range',
),
migrations.AddField(
model_name='iptype',
name='domaine_ip_stop',
field=models.GenericIPAddressField(default='255.255.254.254', protocol='IPv4'),
preserve_default=False,
),
]

View file

@ -27,7 +27,7 @@ from django.forms import ValidationError
from django.utils.functional import cached_property from django.utils.functional import cached_property
from django.utils import timezone from django.utils import timezone
from macaddress.fields import MACAddressField from macaddress.fields import MACAddressField
from netaddr import mac_bare, EUI, IPSet, IPNetwork from netaddr import mac_bare, EUI, IPSet, IPRange, IPNetwork, IPAddress
from django.core.validators import MinValueValidator,MaxValueValidator from django.core.validators import MinValueValidator,MaxValueValidator
import re import re
from reversion import revisions as reversion from reversion import revisions as reversion
@ -65,21 +65,17 @@ class IpType(models.Model):
type = models.CharField(max_length=255) type = models.CharField(max_length=255)
extension = models.ForeignKey('Extension', on_delete=models.PROTECT) extension = models.ForeignKey('Extension', on_delete=models.PROTECT)
need_infra = models.BooleanField(default=False) need_infra = models.BooleanField(default=False)
domaine_ip = models.GenericIPAddressField(protocol='IPv4') domaine_ip_start = models.GenericIPAddressField(protocol='IPv4')
domaine_range = models.IntegerField(validators=[MinValueValidator(16), MaxValueValidator(32)]) domaine_ip_stop = models.GenericIPAddressField(protocol='IPv4')
vlan = models.ForeignKey('Vlan', on_delete=models.PROTECT, blank=True, null=True) vlan = models.ForeignKey('Vlan', on_delete=models.PROTECT, blank=True, null=True)
@cached_property @cached_property
def network(self): def ip_range(self):
return str(self.domaine_ip) + '/' + str(self.domaine_range) return IPRange(self.domaine_ip_start, end=self.domaine_ip_stop)
@cached_property
def ip_network(self):
return IPNetwork(self.network)
@cached_property @cached_property
def ip_set(self): def ip_set(self):
return IPSet(self.ip_network) return IPSet(self.ip_range)
@cached_property @cached_property
def ip_set_as_str(self): def ip_set_as_str(self):
@ -93,7 +89,10 @@ class IpType(models.Model):
def gen_ip_range(self): def gen_ip_range(self):
# Creation du range d'ip dans les objets iplist # Creation du range d'ip dans les objets iplist
ip_obj = [IpList(ip_type=self, ipv4=str(ip)) for ip in self.ip_network.iter_hosts()] networks = []
for net in self.ip_range.cidrs():
networks += net.iter_hosts()
ip_obj = [IpList(ip_type=self, ipv4=str(ip)) for ip in networks]
IpList.objects.bulk_create(ip_obj) IpList.objects.bulk_create(ip_obj)
return return
@ -105,6 +104,11 @@ class IpType(models.Model):
ip.delete() ip.delete()
def clean(self): def clean(self):
if IPAddress(self.domaine_ip_start) > IPAddress(self.domaine_ip_stop):
raise ValidationError("Domaine end doit être après start...")
# On ne crée pas plus grand qu'un /16
if self.ip_range.size > 65536:
raise ValidationError("Le range est trop gros, vous ne devez pas créer plus grand qu'un /16")
# On check que les / ne se recoupent pas # On check que les / ne se recoupent pas
for element in IpType.objects.all().exclude(pk=self.pk): for element in IpType.objects.all().exclude(pk=self.pk):
if not self.ip_set.isdisjoint(element.ip_set): if not self.ip_set.isdisjoint(element.ip_set):

View file

@ -28,8 +28,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
<th>Type d'ip</th> <th>Type d'ip</th>
<th>Extension</th> <th>Extension</th>
<th>Nécessite l'autorisation infra</th> <th>Nécessite l'autorisation infra</th>
<th>Domaine d'ip</th> <th>Début</th>
<th>Range</th> <th>Fin</th>
<th>Sur vlan</th> <th>Sur vlan</th>
<th></th> <th></th>
<th></th> <th></th>
@ -40,8 +40,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
<td>{{ type.type }}</td> <td>{{ type.type }}</td>
<td>{{ type.extension }}</td> <td>{{ type.extension }}</td>
<td>{{ type.need_infra }}</td> <td>{{ type.need_infra }}</td>
<td>{{ type.domaine_ip }}</td> <td>{{ type.domaine_ip_start }}</td>
<td>{{ type.domaine_range }}</td> <td>{{ type.domaine_ip_stop }}</td>
<td>{{ type.vlan }}</td> <td>{{ type.vlan }}</td>
<td class="text-right"> <td class="text-right">
{% if is_infra %} {% if is_infra %}