|
|
@@ -4,17 +4,22 @@ from django.contrib.contenttypes.models import ContentType
|
|
|
from dcim.models import Device, Interface, Location, Rack, Region, Site, SiteGroup
|
|
|
from extras.forms import CustomFieldModelForm
|
|
|
from extras.models import Tag
|
|
|
+from ipam.choices import *
|
|
|
from ipam.constants import *
|
|
|
+from ipam.formfields import IPNetworkFormField
|
|
|
from ipam.models import *
|
|
|
from tenancy.forms import TenancyForm
|
|
|
+from utilities.exceptions import PermissionsViolation
|
|
|
from utilities.forms import (
|
|
|
- BootstrapMixin, ContentTypeChoiceField, DatePicker, DynamicModelChoiceField, DynamicModelMultipleChoiceField,
|
|
|
- NumericArrayField, SlugField, StaticSelect, StaticSelectMultiple,
|
|
|
+ add_blank_choice, BootstrapMixin, ContentTypeChoiceField, DatePicker, DynamicModelChoiceField,
|
|
|
+ DynamicModelMultipleChoiceField, NumericArrayField, SlugField, StaticSelect, StaticSelectMultiple,
|
|
|
)
|
|
|
from virtualization.models import Cluster, ClusterGroup, VirtualMachine, VMInterface
|
|
|
|
|
|
__all__ = (
|
|
|
'AggregateForm',
|
|
|
+ 'FHRPGroupForm',
|
|
|
+ 'FHRPGroupAssignmentForm',
|
|
|
'IPAddressAssignForm',
|
|
|
'IPAddressBulkAddForm',
|
|
|
'IPAddressForm',
|
|
|
@@ -472,6 +477,76 @@ class IPAddressAssignForm(BootstrapMixin, forms.Form):
|
|
|
)
|
|
|
|
|
|
|
|
|
+class FHRPGroupForm(BootstrapMixin, CustomFieldModelForm):
|
|
|
+ tags = DynamicModelMultipleChoiceField(
|
|
|
+ queryset=Tag.objects.all(),
|
|
|
+ required=False
|
|
|
+ )
|
|
|
+
|
|
|
+ # Optionally create a new IPAddress along with the NHRPGroup
|
|
|
+ ip_vrf = DynamicModelChoiceField(
|
|
|
+ queryset=VRF.objects.all(),
|
|
|
+ required=False,
|
|
|
+ label='VRF'
|
|
|
+ )
|
|
|
+ ip_address = IPNetworkFormField(
|
|
|
+ required=False,
|
|
|
+ label='Address'
|
|
|
+ )
|
|
|
+ ip_status = forms.ChoiceField(
|
|
|
+ choices=add_blank_choice(IPAddressStatusChoices),
|
|
|
+ required=False,
|
|
|
+ label='Status'
|
|
|
+ )
|
|
|
+
|
|
|
+ class Meta:
|
|
|
+ model = FHRPGroup
|
|
|
+ fields = (
|
|
|
+ 'protocol', 'group_id', 'auth_type', 'auth_key', 'description', 'ip_vrf', 'ip_address', 'ip_status', 'tags',
|
|
|
+ )
|
|
|
+ fieldsets = (
|
|
|
+ ('FHRP Group', ('protocol', 'group_id', 'description', 'tags')),
|
|
|
+ ('Authentication', ('auth_type', 'auth_key')),
|
|
|
+ ('Virtual IP Address', ('ip_vrf', 'ip_address', 'ip_status'))
|
|
|
+ )
|
|
|
+
|
|
|
+ def save(self, *args, **kwargs):
|
|
|
+ instance = super().save(*args, **kwargs)
|
|
|
+
|
|
|
+ # Check if we need to create a new IPAddress for the group
|
|
|
+ if self.cleaned_data.get('ip_address'):
|
|
|
+ ipaddress = IPAddress(
|
|
|
+ vrf=self.cleaned_data['ip_vrf'],
|
|
|
+ address=self.cleaned_data['ip_address'],
|
|
|
+ status=self.cleaned_data['ip_status'],
|
|
|
+ assigned_object=instance
|
|
|
+ )
|
|
|
+ ipaddress.role = {
|
|
|
+ FHRPGroupProtocolChoices.PROTOCOL_VRRP2: IPAddressRoleChoices.ROLE_VRRP,
|
|
|
+ FHRPGroupProtocolChoices.PROTOCOL_VRRP3: IPAddressRoleChoices.ROLE_VRRP,
|
|
|
+ FHRPGroupProtocolChoices.PROTOCOL_HSRP: IPAddressRoleChoices.ROLE_HSRP,
|
|
|
+ FHRPGroupProtocolChoices.PROTOCOL_GLBP: IPAddressRoleChoices.ROLE_GLBP,
|
|
|
+ FHRPGroupProtocolChoices.PROTOCOL_CARP: IPAddressRoleChoices.ROLE_CARP,
|
|
|
+ }[self.cleaned_data['protocol']]
|
|
|
+ ipaddress.save()
|
|
|
+
|
|
|
+ # Check that the new IPAddress conforms with any assigned object-level permissions
|
|
|
+ if not IPAddress.objects.filter(pk=ipaddress.pk).first():
|
|
|
+ raise PermissionsViolation()
|
|
|
+
|
|
|
+ return instance
|
|
|
+
|
|
|
+
|
|
|
+class FHRPGroupAssignmentForm(BootstrapMixin, forms.ModelForm):
|
|
|
+ group = DynamicModelChoiceField(
|
|
|
+ queryset=FHRPGroup.objects.all()
|
|
|
+ )
|
|
|
+
|
|
|
+ class Meta:
|
|
|
+ model = FHRPGroupAssignment
|
|
|
+ fields = ('group', 'priority')
|
|
|
+
|
|
|
+
|
|
|
class VLANGroupForm(BootstrapMixin, CustomFieldModelForm):
|
|
|
scope_type = ContentTypeChoiceField(
|
|
|
queryset=ContentType.objects.filter(model__in=VLANGROUP_SCOPE_TYPES),
|