From 14d3c46cb01136bbb40073709a153c8f7b91108e Mon Sep 17 00:00:00 2001 From: Hoanh An Date: Mon, 20 Jul 2020 17:38:03 -0400 Subject: [PATCH 1/3] use big interger field with constraints for vcid --- netbox_virtual_circuit_plugin/models.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/netbox_virtual_circuit_plugin/models.py b/netbox_virtual_circuit_plugin/models.py index b7a7c38..cfe0b4e 100644 --- a/netbox_virtual_circuit_plugin/models.py +++ b/netbox_virtual_circuit_plugin/models.py @@ -1,14 +1,20 @@ from django.db import models +from django.core.validators import MaxValueValidator, MinValueValidator from ipam.models import VLAN + from .choices import VirtualCircuitStatusChoices class VirtualCircuit(models.Model): """Virtual Circuit model.""" - vcid = models.PositiveSmallIntegerField( + vcid = models.BigIntegerField( primary_key=True, verbose_name='ID', + validators=[ + MaxValueValidator(4294967295), + MinValueValidator(1), + ], ) name = models.CharField( max_length=64, @@ -16,7 +22,7 @@ class VirtualCircuit(models.Model): status = models.CharField( max_length=30, choices=VirtualCircuitStatusChoices, - default=VirtualCircuitStatusChoices.STATUS_PENDING_CONFIGURATION + default=VirtualCircuitStatusChoices.STATUS_PENDING_CONFIGURATION, ) context = models.CharField( max_length=100, From 8095a5f7d86ec47cd808dafef5e359bb826adced Mon Sep 17 00:00:00 2001 From: Hoanh An Date: Mon, 20 Jul 2020 17:38:17 -0400 Subject: [PATCH 2/3] add tests --- netbox_virtual_circuit_plugin/tests/test_models.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/netbox_virtual_circuit_plugin/tests/test_models.py b/netbox_virtual_circuit_plugin/tests/test_models.py index fb54091..921a602 100644 --- a/netbox_virtual_circuit_plugin/tests/test_models.py +++ b/netbox_virtual_circuit_plugin/tests/test_models.py @@ -15,11 +15,21 @@ def setUp(self): self.vc1_vlan1 = VirtualCircuitVLAN.objects.create(virtual_circuit=self.vc1, vlan=self.vlan1) + def test_invalid_vcid(self): + negative = VirtualCircuit(vcid=-1, name='VC -1') + self.assertRaises(ValidationError, negative.full_clean) + + zero = VirtualCircuit(vcid=0, name='VC 0') + self.assertRaises(ValidationError, zero.full_clean) + + out_of_range = VirtualCircuit(vcid=4294967296, name='VC 4294967296') + self.assertRaises(ValidationError, out_of_range.full_clean) + def test_duplicate_vlan(self): duplicate = VirtualCircuitVLAN(virtual_circuit=self.vc1, vlan=self.vlan1) self.assertRaises(ValidationError, duplicate.full_clean) - def test_create_vlan(self): + def test_create_virtual_circuit(self): vc1_vlan2 = VirtualCircuitVLAN.objects.create(virtual_circuit=self.vc1, vlan=self.vlan2) self.assertEqual(vc1_vlan2.virtual_circuit, self.vc1) self.assertEqual(vc1_vlan2.vlan, self.vlan2) From beea52e0a7c5d902962503c5672a6e1f9bb2e9c6 Mon Sep 17 00:00:00 2001 From: Hoanh An Date: Tue, 21 Jul 2020 16:07:20 -0400 Subject: [PATCH 3/3] add the migration file --- .../migrations/0003_vcid_size.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 netbox_virtual_circuit_plugin/migrations/0003_vcid_size.py diff --git a/netbox_virtual_circuit_plugin/migrations/0003_vcid_size.py b/netbox_virtual_circuit_plugin/migrations/0003_vcid_size.py new file mode 100644 index 0000000..bc603ce --- /dev/null +++ b/netbox_virtual_circuit_plugin/migrations/0003_vcid_size.py @@ -0,0 +1,19 @@ +# Generated by Django 3.0.6 on 2020-07-21 20:06 + +import django.core.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('netbox_virtual_circuit_plugin', '0002_metadata'), + ] + + operations = [ + migrations.AlterField( + model_name='virtualcircuit', + name='vcid', + field=models.BigIntegerField(primary_key=True, serialize=False, validators=[django.core.validators.MaxValueValidator(4294967295), django.core.validators.MinValueValidator(1)]), + ), + ]