1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Initial work on #6235

This commit is contained in:
jeremystretch
2021-11-01 16:14:44 -04:00
parent e0230ed104
commit bb4f3e1789
33 changed files with 959 additions and 17 deletions

View File

@ -13,6 +13,7 @@ from utilities.forms import (
__all__ = (
'AggregateBulkEditForm',
'FHRPGroupBulkEditForm',
'IPAddressBulkEditForm',
'IPRangeBulkEditForm',
'PrefixBulkEditForm',
@ -280,6 +281,41 @@ class IPAddressBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldModelB
]
class FHRPGroupBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldModelBulkEditForm):
pk = forms.ModelMultipleChoiceField(
queryset=FHRPGroup.objects.all(),
widget=forms.MultipleHiddenInput()
)
protocol = forms.ChoiceField(
choices=add_blank_choice(FHRPGroupProtocolChoices),
required=False,
widget=StaticSelect()
)
group_id = forms.IntegerField(
min_value=0,
required=False,
label='Group ID'
)
auth_type = forms.ChoiceField(
choices=add_blank_choice(FHRPGroupAuthTypeChoices),
required=False,
widget=StaticSelect(),
label='Authentication type'
)
auth_key = forms.CharField(
max_length=255,
required=False,
label='Authentication key'
)
description = forms.CharField(
max_length=200,
required=False
)
class Meta:
nullable_fields = ['auth_type', 'auth_key', 'description']
class VLANGroupBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldModelBulkEditForm):
pk = forms.ModelMultipleChoiceField(
queryset=VLANGroup.objects.all(),

View File

@ -12,6 +12,7 @@ from virtualization.models import VirtualMachine, VMInterface
__all__ = (
'AggregateCSVForm',
'FHRPGroupCSVForm',
'IPAddressCSVForm',
'IPRangeCSVForm',
'PrefixCSVForm',
@ -283,6 +284,20 @@ class IPAddressCSVForm(CustomFieldModelCSVForm):
return ipaddress
class FHRPGroupCSVForm(CustomFieldModelCSVForm):
protocol = CSVChoiceField(
choices=FHRPGroupProtocolChoices
)
auth_type = CSVChoiceField(
choices=FHRPGroupAuthTypeChoices,
required=False
)
class Meta:
model = FHRPGroup
fields = ('protocol', 'group_id', 'auth_type', 'auth_key', 'description')
class VLANGroupCSVForm(CustomFieldModelCSVForm):
slug = SlugField()
scope_type = CSVContentTypeField(

View File

@ -14,6 +14,7 @@ from utilities.forms import (
__all__ = (
'AggregateFilterForm',
'FHRPGroupFilterForm',
'IPAddressFilterForm',
'IPRangeFilterForm',
'PrefixFilterForm',
@ -356,6 +357,41 @@ class IPAddressFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldModelFil
tag = TagFilterField(model)
class FHRPGroupFilterForm(BootstrapMixin, CustomFieldModelFilterForm):
model = FHRPGroup
field_groups = (
('q', 'tag'),
('protocol', 'group_id'),
('auth_type', 'auth_key'),
)
q = forms.CharField(
required=False,
widget=forms.TextInput(attrs={'placeholder': _('All Fields')}),
label=_('Search')
)
protocol = forms.MultipleChoiceField(
choices=FHRPGroupProtocolChoices,
required=False,
widget=StaticSelectMultiple()
)
group_id = forms.IntegerField(
min_value=0,
required=False,
label='Group ID'
)
auth_type = forms.MultipleChoiceField(
choices=FHRPGroupAuthTypeChoices,
required=False,
widget=StaticSelectMultiple(),
label='Authentication type'
)
auth_key = forms.CharField(
required=False,
label='Authentication key'
)
tag = TagFilterField(model)
class VLANGroupFilterForm(BootstrapMixin, CustomFieldModelFilterForm):
field_groups = [
['q', 'tag'],

View File

@ -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),