diff --git a/docs/core-functionality/ipam.md b/docs/core-functionality/ipam.md index c1e77069e..dd05d6a01 100644 --- a/docs/core-functionality/ipam.md +++ b/docs/core-functionality/ipam.md @@ -17,3 +17,7 @@ {!models/ipam/vrf.md!} {!models/ipam/routetarget.md!} + +__ + +{!models/ipam/fhrpgroup.md!} diff --git a/docs/models/ipam/fhrpgroup.md b/docs/models/ipam/fhrpgroup.md new file mode 100644 index 000000000..ec21ca37f --- /dev/null +++ b/docs/models/ipam/fhrpgroup.md @@ -0,0 +1,14 @@ +# FHRP Group + +A first-hop redundancy protocol (FHRP) enables multiple physical interfaces to present a virtual IP address in a redundant manner. Example of such protocols include: + +* Hot Standby Router Protocol (HSRP) +* Virtual Router Redundancy Protocol (VRRP) +* Common Address Redundancy Protocol (CARP) +* Gateway Load Balancing Protocol (GLBP) + +NetBox models these redundancy groups by protocol and group ID. Each group may optionally be assigned an authentication type and key. (Note that the authentication key is stored as a plaintext value in NetBox.) Each group may be assigned or more virtual IPv4 and/or IPv6 addresses. + +## FHRP Group Assignments + +Member device and VM interfaces can be assigned to FHRP groups, along with a numeric priority value. For instance, three interfaces, each belonging to a different router, may each be assigned to the same FHRP group to serve a common virtual IP address. Each of these assignments would typically receive a different priority. diff --git a/docs/release-notes/version-3.1.md b/docs/release-notes/version-3.1.md index ff615a92b..65d3627e5 100644 --- a/docs/release-notes/version-3.1.md +++ b/docs/release-notes/version-3.1.md @@ -37,6 +37,10 @@ Dynamic configuration parameters may also still be defined within `configuration For a complete list of supported parameters, please see the [dynamic configuration documentation](../configuration/dynamic-settings.md). +#### First Hop Redundancy Protocol (FHRP) Groups ([#6235](https://github.com/netbox-community/netbox/issues/6235)) + +A new FHRP group model has been introduced to aid in modeling the configurations of protocols such as HSRP, VRRP, and GLBP. Each FHRP group may be assigned one or more virtual IP addresses, as well as an authentication type and key. Member device and VM interfaces may be associated with one or more FHRP groups, with each assignment receiving a numeric priority designation. + #### Conditional Webhooks ([#6238](https://github.com/netbox-community/netbox/issues/6238)) Webhooks now include a `conditions` field, which may be used to specify conditions under which a webhook triggers. For example, you may wish to generate outgoing requests for a device webhook only when its status is "active" or "staged". This can be done by declaring conditional logic in JSON: diff --git a/netbox/dcim/models/device_components.py b/netbox/dcim/models/device_components.py index e166c44ab..a957aba41 100644 --- a/netbox/dcim/models/device_components.py +++ b/netbox/dcim/models/device_components.py @@ -599,6 +599,12 @@ class Interface(ComponentModel, BaseInterface, LinkTermination, PathEndpoint): object_id_field='assigned_object_id', related_query_name='interface' ) + fhrp_group_assignments = GenericRelation( + to='ipam.FHRPGroupAssignment', + content_type_field='interface_type', + object_id_field='interface_id', + related_query_name='+' + ) clone_fields = ['device', 'parent', 'bridge', 'lag', 'type', 'mgmt_only'] diff --git a/netbox/dcim/views.py b/netbox/dcim/views.py index 9b8ac3e45..7eef45f1b 100644 --- a/netbox/dcim/views.py +++ b/netbox/dcim/views.py @@ -1747,7 +1747,7 @@ class InterfaceView(generic.ObjectView): def get_extra_context(self, request, instance): # Get assigned IP addresses - ipaddress_table = InterfaceIPAddressTable( + ipaddress_table = AssignedIPAddressesTable( data=instance.ip_addresses.restrict(request.user, 'view').prefetch_related('vrf', 'tenant'), orderable=False ) diff --git a/netbox/ipam/api/nested_serializers.py b/netbox/ipam/api/nested_serializers.py index da679a01a..885982afb 100644 --- a/netbox/ipam/api/nested_serializers.py +++ b/netbox/ipam/api/nested_serializers.py @@ -6,6 +6,7 @@ from netbox.api import WritableNestedSerializer __all__ = [ 'NestedAggregateSerializer', 'NestedASNSerializer', + 'NestedFHRPGroupSerializer', 'NestedIPAddressSerializer', 'NestedIPRangeSerializer', 'NestedPrefixSerializer', @@ -78,6 +79,18 @@ class NestedAggregateSerializer(WritableNestedSerializer): fields = ['id', 'url', 'display', 'family', 'prefix'] +# +# FHRP groups +# + +class NestedFHRPGroupSerializer(WritableNestedSerializer): + url = serializers.HyperlinkedIdentityField(view_name='ipam-api:fhrpgroup-detail') + + class Meta: + model = models.FHRPGroup + fields = ['id', 'url', 'display', 'protocol', 'group_id'] + + # # VLANs # diff --git a/netbox/ipam/api/serializers.py b/netbox/ipam/api/serializers.py index 4b68c0c1b..eae653ad7 100644 --- a/netbox/ipam/api/serializers.py +++ b/netbox/ipam/api/serializers.py @@ -110,6 +110,45 @@ class AggregateSerializer(PrimaryModelSerializer): read_only_fields = ['family'] +# +# FHRP Groups +# + +class FHRPGroupSerializer(PrimaryModelSerializer): + url = serializers.HyperlinkedIdentityField(view_name='ipam-api:fhrpgroup-detail') + ip_addresses = NestedIPAddressSerializer(many=True, read_only=True) + + class Meta: + model = FHRPGroup + fields = [ + 'id', 'url', 'display', 'protocol', 'group_id', 'auth_type', 'auth_key', 'description', 'ip_addresses', + 'tags', 'custom_fields', 'created', 'last_updated', + ] + + +class FHRPGroupAssignmentSerializer(PrimaryModelSerializer): + url = serializers.HyperlinkedIdentityField(view_name='tenancy-api:contactassignment-detail') + interface_type = ContentTypeField( + queryset=ContentType.objects.all() + ) + interface = serializers.SerializerMethodField(read_only=True) + + class Meta: + model = FHRPGroupAssignment + fields = [ + 'id', 'url', 'display', 'interface_type', 'interface_id', 'interface', 'priority', 'created', + 'last_updated', + ] + + @swagger_serializer_method(serializer_or_field=serializers.DictField) + def get_interface(self, obj): + if obj.interface is None: + return None + serializer = get_serializer_for_model(obj.interface, prefix='Nested') + context = {'request': self.context['request']} + return serializer(obj.interface, context=context).data + + # # VLANs # diff --git a/netbox/ipam/api/urls.py b/netbox/ipam/api/urls.py index b05fcb303..e465fbd89 100644 --- a/netbox/ipam/api/urls.py +++ b/netbox/ipam/api/urls.py @@ -30,6 +30,10 @@ router.register('ip-ranges', views.IPRangeViewSet) # IP addresses router.register('ip-addresses', views.IPAddressViewSet) +# FHRP groups +router.register('fhrp-groups', views.FHRPGroupViewSet) +router.register('fhrp-group-assignments', views.FHRPGroupAssignmentViewSet) + # VLANs router.register('vlan-groups', views.VLANGroupViewSet) router.register('vlans', views.VLANViewSet) diff --git a/netbox/ipam/api/views.py b/netbox/ipam/api/views.py index 274ce29e8..5e40a2081 100644 --- a/netbox/ipam/api/views.py +++ b/netbox/ipam/api/views.py @@ -130,6 +130,22 @@ class IPAddressViewSet(CustomFieldModelViewSet): filterset_class = filtersets.IPAddressFilterSet +# +# FHRP groups +# + +class FHRPGroupViewSet(CustomFieldModelViewSet): + queryset = FHRPGroup.objects.prefetch_related('ip_addresses', 'tags') + serializer_class = serializers.FHRPGroupSerializer + filterset_class = filtersets.FHRPGroupFilterSet + + +class FHRPGroupAssignmentViewSet(CustomFieldModelViewSet): + queryset = FHRPGroupAssignment.objects.prefetch_related('group', 'interface') + serializer_class = serializers.FHRPGroupAssignmentSerializer + filterset_class = filtersets.FHRPGroupAssignmentFilterSet + + # # VLAN groups # diff --git a/netbox/ipam/choices.py b/netbox/ipam/choices.py index e3a45f577..638ef62f6 100644 --- a/netbox/ipam/choices.py +++ b/netbox/ipam/choices.py @@ -124,6 +124,38 @@ class IPAddressRoleChoices(ChoiceSet): } +# +# FHRP +# + +class FHRPGroupProtocolChoices(ChoiceSet): + + PROTOCOL_VRRP2 = 'vrrp2' + PROTOCOL_VRRP3 = 'vrrp3' + PROTOCOL_HSRP = 'hsrp' + PROTOCOL_GLBP = 'glbp' + PROTOCOL_CARP = 'carp' + + CHOICES = ( + (PROTOCOL_VRRP2, 'VRRPv2'), + (PROTOCOL_VRRP3, 'VRRPv3'), + (PROTOCOL_HSRP, 'HSRP'), + (PROTOCOL_GLBP, 'GLBP'), + (PROTOCOL_CARP, 'CARP'), + ) + + +class FHRPGroupAuthTypeChoices(ChoiceSet): + + AUTHENTICATION_PLAINTEXT = 'plaintext' + AUTHENTICATION_MD5 = 'md5' + + CHOICES = ( + (AUTHENTICATION_PLAINTEXT, 'Plaintext'), + (AUTHENTICATION_MD5, 'MD5'), + ) + + # # VLANs # diff --git a/netbox/ipam/constants.py b/netbox/ipam/constants.py index 9dd9328b8..b19d4061b 100644 --- a/netbox/ipam/constants.py +++ b/netbox/ipam/constants.py @@ -1,6 +1,6 @@ from django.db.models import Q -from .choices import IPAddressRoleChoices +from .choices import FHRPGroupProtocolChoices, IPAddressRoleChoices # BGP ASN bounds BGP_ASN_MIN = 1 @@ -34,6 +34,7 @@ PREFIX_LENGTH_MAX = 127 # IPv6 IPADDRESS_ASSIGNMENT_MODELS = Q( Q(app_label='dcim', model='interface') | + Q(app_label='ipam', model='fhrpgroup') | Q(app_label='virtualization', model='vminterface') ) @@ -51,6 +52,22 @@ IPADDRESS_ROLES_NONUNIQUE = ( ) +# +# FHRP groups +# + +FHRPGROUPASSIGNMENT_PRIORITY_MIN = 0 +FHRPGROUPASSIGNMENT_PRIORITY_MAX = 255 + +FHRP_PROTOCOL_ROLE_MAPPINGS = { + 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, +} + + # # VLANs # diff --git a/netbox/ipam/filtersets.py b/netbox/ipam/filtersets.py index 1dd8f97d6..6b6d14f5b 100644 --- a/netbox/ipam/filtersets.py +++ b/netbox/ipam/filtersets.py @@ -7,7 +7,7 @@ from netaddr.core import AddrFormatError from dcim.models import Device, Interface, Region, Site, SiteGroup from extras.filters import TagFilter -from netbox.filtersets import OrganizationalModelFilterSet, PrimaryModelFilterSet +from netbox.filtersets import ChangeLoggedModelFilterSet, OrganizationalModelFilterSet, PrimaryModelFilterSet from tenancy.filtersets import TenancyFilterSet from tenancy.models import Tenant from utilities.filters import ( @@ -21,6 +21,8 @@ from .models import * __all__ = ( 'AggregateFilterSet', 'ASNFilterSet', + 'FHRPGroupAssignmentFilterSet', + 'FHRPGroupFilterSet', 'IPAddressFilterSet', 'IPRangeFilterSet', 'PrefixFilterSet', @@ -648,6 +650,67 @@ class IPAddressFilterSet(PrimaryModelFilterSet, TenancyFilterSet): return queryset.exclude(assigned_object_id__isnull=value) +class FHRPGroupFilterSet(PrimaryModelFilterSet): + q = django_filters.CharFilter( + method='search', + label='Search', + ) + protocol = django_filters.MultipleChoiceFilter( + choices=FHRPGroupProtocolChoices + ) + auth_type = django_filters.MultipleChoiceFilter( + choices=FHRPGroupAuthTypeChoices + ) + related_ip = django_filters.ModelMultipleChoiceFilter( + queryset=IPAddress.objects.all(), + method='filter_related_ip' + ) + tag = TagFilter() + + class Meta: + model = FHRPGroup + fields = ['id', 'protocol', 'group_id', 'auth_type'] + + def search(self, queryset, name, value): + if not value.strip(): + return queryset + return queryset.filter( + Q(description__icontains=value) + ) + + def filter_related_ip(self, queryset, name, value): + """ + Filter by VRF & prefix of assigned IP addresses. + """ + ip_filter = Q() + for ipaddress in value: + if ipaddress.vrf: + q = Q( + ip_addresses__address__net_contained_or_equal=ipaddress.address, + ip_addresses__vrf=ipaddress.vrf + ) + else: + q = Q( + ip_addresses__address__net_contained_or_equal=ipaddress.address, + ip_addresses__vrf__isnull=True + ) + ip_filter |= q + + return queryset.filter(ip_filter) + + +class FHRPGroupAssignmentFilterSet(ChangeLoggedModelFilterSet): + interface_type = ContentTypeFilter() + group_id = django_filters.ModelMultipleChoiceFilter( + queryset=FHRPGroup.objects.all(), + label='Group (ID)', + ) + + class Meta: + model = FHRPGroupAssignment + fields = ['id', 'group_id', 'interface_type', 'interface_id', 'priority'] + + class VLANGroupFilterSet(OrganizationalModelFilterSet): q = django_filters.CharFilter( method='search', diff --git a/netbox/ipam/forms/bulk_edit.py b/netbox/ipam/forms/bulk_edit.py index 507d180ca..ab53dfb8c 100644 --- a/netbox/ipam/forms/bulk_edit.py +++ b/netbox/ipam/forms/bulk_edit.py @@ -15,6 +15,7 @@ from utilities.forms import ( __all__ = ( 'AggregateBulkEditForm', 'ASNBulkEditForm', + 'FHRPGroupBulkEditForm', 'IPAddressBulkEditForm', 'IPRangeBulkEditForm', 'PrefixBulkEditForm', @@ -314,6 +315,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(), diff --git a/netbox/ipam/forms/bulk_import.py b/netbox/ipam/forms/bulk_import.py index 1d18e94c7..ea2afff5f 100644 --- a/netbox/ipam/forms/bulk_import.py +++ b/netbox/ipam/forms/bulk_import.py @@ -13,6 +13,7 @@ from virtualization.models import VirtualMachine, VMInterface __all__ = ( 'AggregateCSVForm', 'ASNCSVForm', + 'FHRPGroupCSVForm', 'IPAddressCSVForm', 'IPRangeCSVForm', 'PrefixCSVForm', @@ -303,6 +304,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( diff --git a/netbox/ipam/forms/filtersets.py b/netbox/ipam/forms/filtersets.py index 5eeba47c1..b89fa919c 100644 --- a/netbox/ipam/forms/filtersets.py +++ b/netbox/ipam/forms/filtersets.py @@ -17,6 +17,7 @@ from utilities.forms import ( __all__ = ( 'AggregateFilterForm', 'ASNFilterForm', + 'FHRPGroupFilterForm', 'IPAddressFilterForm', 'IPRangeFilterForm', 'PrefixFilterForm', @@ -386,6 +387,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'], diff --git a/netbox/ipam/forms/models.py b/netbox/ipam/forms/models.py index ea00b6914..d69800aa5 100644 --- a/netbox/ipam/forms/models.py +++ b/netbox/ipam/forms/models.py @@ -4,19 +4,24 @@ 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 ipam.models import ASN 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', 'ASNForm', + 'FHRPGroupForm', + 'FHRPGroupAssignmentForm', 'IPAddressAssignForm', 'IPAddressBulkAddForm', 'IPAddressForm', @@ -518,6 +523,77 @@ 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 = FHRP_PROTOCOL_ROLE_MAPPINGS[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') + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + ipaddresses = self.instance.interface.ip_addresses.all() + for ipaddress in ipaddresses: + self.fields['group'].widget.add_query_param('related_ip', ipaddress.pk) + + class VLANGroupForm(BootstrapMixin, CustomFieldModelForm): scope_type = ContentTypeChoiceField( queryset=ContentType.objects.filter(model__in=VLANGROUP_SCOPE_TYPES), diff --git a/netbox/ipam/graphql/schema.py b/netbox/ipam/graphql/schema.py index aa9f89f2b..9609d1434 100644 --- a/netbox/ipam/graphql/schema.py +++ b/netbox/ipam/graphql/schema.py @@ -32,6 +32,12 @@ class IPAMQuery(graphene.ObjectType): service = ObjectField(ServiceType) service_list = ObjectListField(ServiceType) + fhrp_group = ObjectField(FHRPGroupType) + fhrp_group_list = ObjectListField(FHRPGroupType) + + fhrp_group_assignment = ObjectField(FHRPGroupAssignmentType) + fhrp_group_assignment_list = ObjectListField(FHRPGroupAssignmentType) + vlan = ObjectField(VLANType) vlan_list = ObjectListField(VLANType) diff --git a/netbox/ipam/graphql/types.py b/netbox/ipam/graphql/types.py index 3ba27fcf0..72526b3bd 100644 --- a/netbox/ipam/graphql/types.py +++ b/netbox/ipam/graphql/types.py @@ -7,6 +7,8 @@ from netbox.graphql.types import OrganizationalObjectType, PrimaryObjectType __all__ = ( 'ASNType', 'AggregateType', + 'FHRPGroupType', + 'FHRPGroupAssignmentType', 'IPAddressType', 'IPRangeType', 'PrefixType', @@ -37,6 +39,25 @@ class AggregateType(PrimaryObjectType): filterset_class = filtersets.AggregateFilterSet +class FHRPGroupType(PrimaryObjectType): + + class Meta: + model = models.FHRPGroup + fields = '__all__' + filterset_class = filtersets.FHRPGroupFilterSet + + def resolve_auth_type(self, info): + return self.auth_type or None + + +class FHRPGroupAssignmentType(PrimaryObjectType): + + class Meta: + model = models.FHRPGroupAssignment + fields = '__all__' + filterset_class = filtersets.FHRPGroupAssignmentFilterSet + + class IPAddressType(PrimaryObjectType): class Meta: diff --git a/netbox/ipam/migrations/0052_fhrpgroup.py b/netbox/ipam/migrations/0052_fhrpgroup.py new file mode 100644 index 000000000..976084b47 --- /dev/null +++ b/netbox/ipam/migrations/0052_fhrpgroup.py @@ -0,0 +1,58 @@ +import django.core.serializers.json +import django.core.validators +from django.db import migrations, models +import django.db.models.deletion +import taggit.managers + + +class Migration(migrations.Migration): + + dependencies = [ + ('contenttypes', '0002_remove_content_type_name'), + ('extras', '0064_configrevision'), + ('ipam', '0051_extend_tag_support'), + ] + + operations = [ + migrations.CreateModel( + name='FHRPGroup', + fields=[ + ('created', models.DateField(auto_now_add=True, null=True)), + ('last_updated', models.DateTimeField(auto_now=True, null=True)), + ('custom_field_data', models.JSONField(blank=True, default=dict, encoder=django.core.serializers.json.DjangoJSONEncoder)), + ('id', models.BigAutoField(primary_key=True, serialize=False)), + ('group_id', models.PositiveSmallIntegerField()), + ('protocol', models.CharField(max_length=50)), + ('auth_type', models.CharField(blank=True, max_length=50)), + ('auth_key', models.CharField(blank=True, max_length=255)), + ('description', models.CharField(blank=True, max_length=200)), + ('tags', taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag')), + ], + options={ + 'verbose_name': 'FHRP group', + 'ordering': ['protocol', 'group_id', 'pk'], + }, + ), + migrations.AlterField( + model_name='ipaddress', + name='assigned_object_type', + field=models.ForeignKey(blank=True, limit_choices_to=models.Q(models.Q(models.Q(('app_label', 'dcim'), ('model', 'interface')), models.Q(('app_label', 'ipam'), ('model', 'fhrpgroup')), models.Q(('app_label', 'virtualization'), ('model', 'vminterface')), _connector='OR')), null=True, on_delete=django.db.models.deletion.PROTECT, related_name='+', to='contenttypes.contenttype'), + ), + migrations.CreateModel( + name='FHRPGroupAssignment', + fields=[ + ('created', models.DateField(auto_now_add=True, null=True)), + ('last_updated', models.DateTimeField(auto_now=True, null=True)), + ('id', models.BigAutoField(primary_key=True, serialize=False)), + ('interface_id', models.PositiveIntegerField()), + ('priority', models.PositiveSmallIntegerField(validators=[django.core.validators.MinValueValidator(0), django.core.validators.MaxValueValidator(255)])), + ('group', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ipam.fhrpgroup')), + ('interface_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.contenttype')), + ], + options={ + 'verbose_name': 'FHRP group assignment', + 'ordering': ('priority', 'pk'), + 'unique_together': {('interface_type', 'interface_id', 'group')}, + }, + ), + ] diff --git a/netbox/ipam/models/__init__.py b/netbox/ipam/models/__init__.py index 0f65e6652..ab0e4b6ca 100644 --- a/netbox/ipam/models/__init__.py +++ b/netbox/ipam/models/__init__.py @@ -1,3 +1,4 @@ +from .fhrp import * from .ip import * from .services import * from .vlans import * @@ -8,6 +9,8 @@ __all__ = ( 'Aggregate', 'IPAddress', 'IPRange', + 'FHRPGroup', + 'FHRPGroupAssignment', 'Prefix', 'RIR', 'Role', diff --git a/netbox/ipam/models/fhrp.py b/netbox/ipam/models/fhrp.py new file mode 100644 index 000000000..95c907cfd --- /dev/null +++ b/netbox/ipam/models/fhrp.py @@ -0,0 +1,100 @@ +from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation +from django.contrib.contenttypes.models import ContentType +from django.core.validators import MaxValueValidator, MinValueValidator +from django.db import models +from django.urls import reverse + +from extras.utils import extras_features +from netbox.models import ChangeLoggedModel, PrimaryModel +from ipam.choices import * +from ipam.constants import * +from utilities.querysets import RestrictedQuerySet + +__all__ = ( + 'FHRPGroup', + 'FHRPGroupAssignment', +) + + +@extras_features('custom_fields', 'custom_links', 'export_templates', 'tags', 'webhooks') +class FHRPGroup(PrimaryModel): + """ + A grouping of next hope resolution protocol (FHRP) peers. (For instance, VRRP or HSRP.) + """ + group_id = models.PositiveSmallIntegerField( + verbose_name='Group ID' + ) + protocol = models.CharField( + max_length=50, + choices=FHRPGroupProtocolChoices + ) + auth_type = models.CharField( + max_length=50, + choices=FHRPGroupAuthTypeChoices, + blank=True, + verbose_name='Authentication type' + ) + auth_key = models.CharField( + max_length=255, + blank=True, + verbose_name='Authentication key' + ) + description = models.CharField( + max_length=200, + blank=True + ) + ip_addresses = GenericRelation( + to='ipam.IPAddress', + content_type_field='assigned_object_type', + object_id_field='assigned_object_id', + related_query_name='nhrp_group' + ) + + objects = RestrictedQuerySet.as_manager() + + clone_fields = [ + 'protocol', 'auth_type', 'auth_key' + ] + + class Meta: + ordering = ['protocol', 'group_id', 'pk'] + verbose_name = 'FHRP group' + + def __str__(self): + return f'{self.get_protocol_display()} group {self.group_id}' + + def get_absolute_url(self): + return reverse('ipam:fhrpgroup', args=[self.pk]) + + +@extras_features('webhooks') +class FHRPGroupAssignment(ChangeLoggedModel): + interface_type = models.ForeignKey( + to=ContentType, + on_delete=models.CASCADE + ) + interface_id = models.PositiveIntegerField() + interface = GenericForeignKey( + ct_field='interface_type', + fk_field='interface_id' + ) + group = models.ForeignKey( + to='ipam.FHRPGroup', + on_delete=models.CASCADE + ) + priority = models.PositiveSmallIntegerField( + validators=( + MinValueValidator(FHRPGROUPASSIGNMENT_PRIORITY_MIN), + MaxValueValidator(FHRPGROUPASSIGNMENT_PRIORITY_MAX) + ) + ) + + objects = RestrictedQuerySet.as_manager() + + class Meta: + ordering = ('priority', 'pk') + unique_together = ('interface_type', 'interface_id', 'group') + verbose_name = 'FHRP group assignment' + + def __str__(self): + return f'{self.interface}: {self.group} ({self.priority})' diff --git a/netbox/ipam/tables/__init__.py b/netbox/ipam/tables/__init__.py index a280eac1b..6f429e27d 100644 --- a/netbox/ipam/tables/__init__.py +++ b/netbox/ipam/tables/__init__.py @@ -1,3 +1,4 @@ +from .fhrp import * from .ip import * from .services import * from .vlans import * diff --git a/netbox/ipam/tables/fhrp.py b/netbox/ipam/tables/fhrp.py new file mode 100644 index 000000000..8a31694bf --- /dev/null +++ b/netbox/ipam/tables/fhrp.py @@ -0,0 +1,68 @@ +import django_tables2 as tables + +from utilities.tables import BaseTable, ButtonsColumn, MarkdownColumn, TagColumn, ToggleColumn +from ipam.models import * + +__all__ = ( + 'FHRPGroupTable', + 'FHRPGroupAssignmentTable', +) + + +IPADDRESSES = """ +{% for ip in record.ip_addresses.all %} + {{ ip }}
+{% endfor %} +""" + + +class FHRPGroupTable(BaseTable): + pk = ToggleColumn() + group_id = tables.Column( + linkify=True + ) + comments = MarkdownColumn() + ip_addresses = tables.TemplateColumn( + template_code=IPADDRESSES, + orderable=False, + verbose_name='IP Addresses' + ) + interface_count = tables.Column( + verbose_name='Interfaces' + ) + tags = TagColumn( + url_name='ipam:fhrpgroup_list' + ) + + class Meta(BaseTable.Meta): + model = FHRPGroup + fields = ( + 'pk', 'group_id', 'protocol', 'auth_type', 'auth_key', 'description', 'ip_addresses', 'interface_count', + 'tags', + ) + default_columns = ('pk', 'group_id', 'protocol', 'auth_type', 'description', 'ip_addresses', 'interface_count') + + +class FHRPGroupAssignmentTable(BaseTable): + pk = ToggleColumn() + object_parent = tables.Column( + accessor=tables.A('object.parent_object'), + linkify=True, + orderable=False, + verbose_name='Parent' + ) + interface = tables.Column( + linkify=True, + orderable=False + ) + group = tables.Column( + linkify=True + ) + actions = ButtonsColumn( + model=FHRPGroupAssignment, + buttons=('edit', 'delete', 'foo') + ) + + class Meta(BaseTable.Meta): + model = FHRPGroupAssignment + fields = ('pk', 'group', 'object_parent', 'interface', 'priority') diff --git a/netbox/ipam/tables/ip.py b/netbox/ipam/tables/ip.py index 32937d17e..5c41a3f0b 100644 --- a/netbox/ipam/tables/ip.py +++ b/netbox/ipam/tables/ip.py @@ -13,6 +13,7 @@ __all__ = ( 'AggregateTable', 'ASNTable', 'InterfaceIPAddressTable', + 'AssignedIPAddressesTable', 'IPAddressAssignTable', 'IPAddressTable', 'IPRangeTable', @@ -382,9 +383,9 @@ class IPAddressAssignTable(BaseTable): orderable = False -class InterfaceIPAddressTable(BaseTable): +class AssignedIPAddressesTable(BaseTable): """ - List IP addresses assigned to a specific Interface. + List IP addresses assigned to an object. """ address = tables.Column( linkify=True, diff --git a/netbox/ipam/tests/test_api.py b/netbox/ipam/tests/test_api.py index 77473e504..5ec0a0177 100644 --- a/netbox/ipam/tests/test_api.py +++ b/netbox/ipam/tests/test_api.py @@ -544,6 +544,47 @@ class IPAddressTest(APIViewTestCases.APIViewTestCase): IPAddress.objects.bulk_create(ip_addresses) +class FHRPGroupTest(APIViewTestCases.APIViewTestCase): + model = FHRPGroup + brief_fields = ['display', 'group_id', 'id', 'protocol', 'url'] + bulk_update_data = { + 'protocol': FHRPGroupProtocolChoices.PROTOCOL_GLBP, + 'group_id': 200, + 'auth_type': FHRPGroupAuthTypeChoices.AUTHENTICATION_MD5, + 'auth_key': 'foobarbaz999', + 'description': 'New description', + } + + @classmethod + def setUpTestData(cls): + + fhrp_groups = ( + FHRPGroup(protocol=FHRPGroupProtocolChoices.PROTOCOL_VRRP2, group_id=10, auth_type=FHRPGroupAuthTypeChoices.AUTHENTICATION_PLAINTEXT, auth_key='foobar123'), + FHRPGroup(protocol=FHRPGroupProtocolChoices.PROTOCOL_VRRP3, group_id=20, auth_type=FHRPGroupAuthTypeChoices.AUTHENTICATION_MD5, auth_key='foobar123'), + FHRPGroup(protocol=FHRPGroupProtocolChoices.PROTOCOL_HSRP, group_id=30), + ) + FHRPGroup.objects.bulk_create(fhrp_groups) + + cls.create_data = [ + { + 'protocol': FHRPGroupProtocolChoices.PROTOCOL_VRRP2, + 'group_id': 110, + 'auth_type': FHRPGroupAuthTypeChoices.AUTHENTICATION_PLAINTEXT, + 'auth_key': 'foobar123', + }, + { + 'protocol': FHRPGroupProtocolChoices.PROTOCOL_VRRP3, + 'group_id': 120, + 'auth_type': FHRPGroupAuthTypeChoices.AUTHENTICATION_MD5, + 'auth_key': 'barfoo456', + }, + { + 'protocol': FHRPGroupProtocolChoices.PROTOCOL_GLBP, + 'group_id': 130, + }, + ] + + class VLANGroupTest(APIViewTestCases.APIViewTestCase): model = VLANGroup brief_fields = ['display', 'id', 'name', 'slug', 'url', 'vlan_count'] diff --git a/netbox/ipam/tests/test_filtersets.py b/netbox/ipam/tests/test_filtersets.py index 523680767..19b6a8e8f 100644 --- a/netbox/ipam/tests/test_filtersets.py +++ b/netbox/ipam/tests/test_filtersets.py @@ -872,6 +872,33 @@ class IPAddressTestCase(TestCase, ChangeLoggedFilterSetTests): self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4) +class FHRPGroupTestCase(TestCase, ChangeLoggedFilterSetTests): + queryset = FHRPGroup.objects.all() + filterset = FHRPGroupFilterSet + + @classmethod + def setUpTestData(cls): + + fhrp_groups = ( + FHRPGroup(protocol=FHRPGroupProtocolChoices.PROTOCOL_VRRP2, group_id=10, auth_type=FHRPGroupAuthTypeChoices.AUTHENTICATION_PLAINTEXT, auth_key='foobar123'), + FHRPGroup(protocol=FHRPGroupProtocolChoices.PROTOCOL_VRRP3, group_id=20, auth_type=FHRPGroupAuthTypeChoices.AUTHENTICATION_MD5, auth_key='foobar123'), + FHRPGroup(protocol=FHRPGroupProtocolChoices.PROTOCOL_HSRP, group_id=30), + ) + FHRPGroup.objects.bulk_create(fhrp_groups) + + def test_protocol(self): + params = {'protocol': [FHRPGroupProtocolChoices.PROTOCOL_VRRP2, FHRPGroupProtocolChoices.PROTOCOL_VRRP3]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + def test_group_id(self): + params = {'group_id': [10, 20]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + def test_auth_type(self): + params = {'auth_type': [FHRPGroupAuthTypeChoices.AUTHENTICATION_PLAINTEXT, FHRPGroupAuthTypeChoices.AUTHENTICATION_MD5]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + class VLANGroupTestCase(TestCase, ChangeLoggedFilterSetTests): queryset = VLANGroup.objects.all() filterset = VLANGroupFilterSet diff --git a/netbox/ipam/tests/test_views.py b/netbox/ipam/tests/test_views.py index 23a03604b..83de73bde 100644 --- a/netbox/ipam/tests/test_views.py +++ b/netbox/ipam/tests/test_views.py @@ -427,6 +427,41 @@ class IPAddressTestCase(ViewTestCases.PrimaryObjectViewTestCase): } +class FHRPGroupTestCase(ViewTestCases.PrimaryObjectViewTestCase): + model = FHRPGroup + + @classmethod + def setUpTestData(cls): + + FHRPGroup.objects.bulk_create(( + FHRPGroup(protocol=FHRPGroupProtocolChoices.PROTOCOL_VRRP2, group_id=10, auth_type=FHRPGroupAuthTypeChoices.AUTHENTICATION_PLAINTEXT, auth_key='foobar123'), + FHRPGroup(protocol=FHRPGroupProtocolChoices.PROTOCOL_VRRP3, group_id=20, auth_type=FHRPGroupAuthTypeChoices.AUTHENTICATION_MD5, auth_key='foobar123'), + FHRPGroup(protocol=FHRPGroupProtocolChoices.PROTOCOL_HSRP, group_id=30), + )) + + tags = create_tags('Alpha', 'Bravo', 'Charlie') + + cls.form_data = { + 'protocol': FHRPGroupProtocolChoices.PROTOCOL_VRRP2, + 'group_id': 99, + 'auth_type': FHRPGroupAuthTypeChoices.AUTHENTICATION_MD5, + 'auth_key': 'abc123def456', + 'description': 'Blah blah blah', + 'tags': [t.pk for t in tags], + } + + cls.csv_data = ( + "protocol,group_id,auth_type,auth_key,description", + "vrrp2,40,plaintext,foobar123,Foo", + "vrrp3,50,md5,foobar123,Bar", + "hsrp,60,,,", + ) + + cls.bulk_edit_data = { + 'protocol': FHRPGroupProtocolChoices.PROTOCOL_CARP, + } + + class VLANGroupTestCase(ViewTestCases.OrganizationalObjectViewTestCase): model = VLANGroup diff --git a/netbox/ipam/urls.py b/netbox/ipam/urls.py index 88c5d7c9e..541acb3ac 100644 --- a/netbox/ipam/urls.py +++ b/netbox/ipam/urls.py @@ -119,6 +119,23 @@ urlpatterns = [ path('ip-addresses//edit/', views.IPAddressEditView.as_view(), name='ipaddress_edit'), path('ip-addresses//delete/', views.IPAddressDeleteView.as_view(), name='ipaddress_delete'), + # FHRP groups + path('fhrp-groups/', views.FHRPGroupListView.as_view(), name='fhrpgroup_list'), + path('fhrp-groups/add/', views.FHRPGroupEditView.as_view(), name='fhrpgroup_add'), + path('fhrp-groups/import/', views.FHRPGroupBulkImportView.as_view(), name='fhrpgroup_import'), + path('fhrp-groups/edit/', views.FHRPGroupBulkEditView.as_view(), name='fhrpgroup_bulk_edit'), + path('fhrp-groups/delete/', views.FHRPGroupBulkDeleteView.as_view(), name='fhrpgroup_bulk_delete'), + path('fhrp-groups//', views.FHRPGroupView.as_view(), name='fhrpgroup'), + path('fhrp-groups//edit/', views.FHRPGroupEditView.as_view(), name='fhrpgroup_edit'), + path('fhrp-groups//delete/', views.FHRPGroupDeleteView.as_view(), name='fhrpgroup_delete'), + path('fhrp-groups//changelog/', ObjectChangeLogView.as_view(), name='fhrpgroup_changelog', kwargs={'model': FHRPGroup}), + path('fhrp-groups//journal/', ObjectJournalView.as_view(), name='fhrpgroup_journal', kwargs={'model': FHRPGroup}), + + # FHRP group assignments + path('fhrp-group-assignments/add/', views.FHRPGroupAssignmentEditView.as_view(), name='fhrpgroupassignment_add'), + path('fhrp-group-assignments//edit/', views.FHRPGroupAssignmentEditView.as_view(), name='fhrpgroupassignment_edit'), + path('fhrp-group-assignments//delete/', views.FHRPGroupAssignmentDeleteView.as_view(), name='fhrpgroupassignment_delete'), + # VLAN groups path('vlan-groups/', views.VLANGroupListView.as_view(), name='vlangroup_list'), path('vlan-groups/add/', views.VLANGroupEditView.as_view(), name='vlangroup_add'), diff --git a/netbox/ipam/views.py b/netbox/ipam/views.py index 7801eec23..f869a75c1 100644 --- a/netbox/ipam/views.py +++ b/netbox/ipam/views.py @@ -1,11 +1,13 @@ +from django.contrib.contenttypes.models import ContentType from django.db.models import Prefetch from django.db.models.expressions import RawSQL +from django.http import Http404 from django.shortcuts import get_object_or_404, redirect, render +from django.urls import reverse from dcim.models import Device, Interface, Site from dcim.tables import SiteTable from netbox.views import generic -from utilities.forms import TableConfigForm from utilities.tables import paginate_table from utilities.utils import count_related from virtualization.models import VirtualMachine, VMInterface @@ -883,6 +885,113 @@ class VLANGroupBulkDeleteView(generic.BulkDeleteView): table = tables.VLANGroupTable +# +# FHRP groups +# + +class FHRPGroupListView(generic.ObjectListView): + queryset = FHRPGroup.objects.annotate( + member_count=count_related(FHRPGroupAssignment, 'group') + ) + filterset = filtersets.FHRPGroupFilterSet + filterset_form = forms.FHRPGroupFilterForm + table = tables.FHRPGroupTable + + +class FHRPGroupView(generic.ObjectView): + queryset = FHRPGroup.objects.all() + + def get_extra_context(self, request, instance): + # Get assigned IP addresses + ipaddress_table = tables.AssignedIPAddressesTable( + data=instance.ip_addresses.restrict(request.user, 'view').prefetch_related('vrf', 'tenant'), + orderable=False + ) + + # Get assigned interfaces + members_table = tables.FHRPGroupAssignmentTable( + data=FHRPGroupAssignment.objects.restrict(request.user, 'view').filter(group=instance), + orderable=False + ) + members_table.columns.hide('group') + + return { + 'ipaddress_table': ipaddress_table, + 'members_table': members_table, + 'member_count': FHRPGroupAssignment.objects.filter(group=instance).count(), + } + + +class FHRPGroupEditView(generic.ObjectEditView): + queryset = FHRPGroup.objects.all() + model_form = forms.FHRPGroupForm + template_name = 'ipam/fhrpgroup_edit.html' + + def get_return_url(self, request, obj=None): + return_url = super().get_return_url(request, obj) + + # If we're redirecting the user to the FHRPGroupAssignment creation form, + # initialize the group field with the FHRPGroup we just saved. + if return_url.startswith(reverse('ipam:fhrpgroupassignment_add')): + return_url += f'&group={obj.pk}' + + return return_url + + +class FHRPGroupDeleteView(generic.ObjectDeleteView): + queryset = FHRPGroup.objects.all() + + +class FHRPGroupBulkImportView(generic.BulkImportView): + queryset = FHRPGroup.objects.all() + model_form = forms.FHRPGroupCSVForm + table = tables.FHRPGroupTable + + +class FHRPGroupBulkEditView(generic.BulkEditView): + queryset = FHRPGroup.objects.all() + filterset = filtersets.FHRPGroupFilterSet + table = tables.FHRPGroupTable + form = forms.FHRPGroupBulkEditForm + + +class FHRPGroupBulkDeleteView(generic.BulkDeleteView): + queryset = FHRPGroup.objects.all() + filterset = filtersets.FHRPGroupFilterSet + table = tables.FHRPGroupTable + + +# +# FHRP group assignments +# + +class FHRPGroupAssignmentEditView(generic.ObjectEditView): + queryset = FHRPGroupAssignment.objects.all() + model_form = forms.FHRPGroupAssignmentForm + template_name = 'ipam/fhrpgroupassignment_edit.html' + + def alter_obj(self, instance, request, args, kwargs): + if not instance.pk: + # Assign the interface based on URL kwargs + try: + app_label, model = request.GET.get('interface_type').split('.') + except (AttributeError, ValueError): + raise Http404("Content type not specified") + content_type = get_object_or_404(ContentType, app_label=app_label, model=model) + instance.interface = get_object_or_404(content_type.model_class(), pk=request.GET.get('interface_id')) + return instance + + def get_return_url(self, request, obj=None): + return obj.interface.get_absolute_url() if obj else super().get_return_url(request) + + +class FHRPGroupAssignmentDeleteView(generic.ObjectDeleteView): + queryset = FHRPGroupAssignment.objects.all() + + def get_return_url(self, request, obj=None): + return obj.interface.get_absolute_url() if obj else super().get_return_url(request) + + # # VLANs # diff --git a/netbox/netbox/navigation_menu.py b/netbox/netbox/navigation_menu.py index 35cd8ece8..0bd29229f 100644 --- a/netbox/netbox/navigation_menu.py +++ b/netbox/netbox/navigation_menu.py @@ -257,8 +257,9 @@ IPAM_MENU = Menu( ), ), MenuGroup( - label='Services', + label='Other', items=( + get_model_item('ipam', 'fhrpgroup', 'FHRP Groups'), get_model_item('ipam', 'service', 'Services', actions=['import']), ), ), diff --git a/netbox/templates/dcim/interface.html b/netbox/templates/dcim/interface.html index 5851b3aeb..811bf6257 100644 --- a/netbox/templates/dcim/interface.html +++ b/netbox/templates/dcim/interface.html @@ -440,6 +440,7 @@ {% endif %} + {% include 'ipam/inc/panels/fhrp_groups.html' %} {% plugin_right_page object %} @@ -458,7 +459,7 @@ {% if perms.ipam.add_ipaddress %} diff --git a/netbox/templates/inc/panels/nhrp_groups.html b/netbox/templates/inc/panels/nhrp_groups.html new file mode 100644 index 000000000..223354441 --- /dev/null +++ b/netbox/templates/inc/panels/nhrp_groups.html @@ -0,0 +1,49 @@ +{% load helpers %} + +
+
Contacts
+
+ {% with fhrp_groups=object.fhrp_group_assignments.all %} + {% if contacts.exists %} + + + + + + + + {% for contact in contacts %} + + + + + + + {% endfor %} +
ProtocolGroup IDPriority
+ {{ contact.contact }} + {{ contact.role|placeholder }}{{ contact.get_priority_display|placeholder }} + {% if perms.tenancy.change_contactassignment %} + + + + {% endif %} + {% if perms.tenancy.delete_contactassignment %} + + + + {% endif %} +
+ {% else %} +
None
+ {% endif %} + {% endwith %} +
+ {% if perms.tenancy.add_contactassignment %} + + {% endif %} +
diff --git a/netbox/templates/ipam/fhrpgroup.html b/netbox/templates/ipam/fhrpgroup.html new file mode 100644 index 000000000..60d6a4bff --- /dev/null +++ b/netbox/templates/ipam/fhrpgroup.html @@ -0,0 +1,84 @@ +{% extends 'generic/object.html' %} +{% load helpers %} +{% load plugins %} +{% load render_table from django_tables2 %} + +{% block breadcrumbs %} + {{ block.super }} + +{% endblock breadcrumbs %} + +{% block content %} +
+
+
+
FHRP Group
+
+ + + + + + + + + + + + + + + + + +
Protocol{{ object.get_protocol_display }}
Group ID{{ object.group_id }}
Description{{ object.description|placeholder }}
Members{{ member_count }}
+
+
+ {% include 'inc/panels/tags.html' %} + {% plugin_left_page object %} +
+
+
+
Authentication
+
+ + + + + + + + + +
Authentication Type{{ object.get_auth_type_display|placeholder }}
Authentication Key{{ object.auth_key|placeholder }}
+
+
+ {% include 'inc/panels/custom_fields.html' %} + {% plugin_right_page object %} +
+
+
+
+
+
Virtual IP Addresses
+
+ {% if ipaddress_table.rows %} + {% render_table ipaddress_table 'inc/table.html' %} + {% else %} +
None
+ {% endif %} +
+
+
+
Members
+
+ {% if ipaddress_table.rows %} + {% render_table members_table 'inc/table.html' %} + {% else %} +
None
+ {% endif %} +
+ {% plugin_full_width_page object %} +
+
+{% endblock %} diff --git a/netbox/templates/ipam/fhrpgroup_edit.html b/netbox/templates/ipam/fhrpgroup_edit.html new file mode 100644 index 000000000..858d265ab --- /dev/null +++ b/netbox/templates/ipam/fhrpgroup_edit.html @@ -0,0 +1,40 @@ +{% extends 'generic/object_edit.html' %} +{% load form_helpers %} + +{% block form %} +
+
+
FHRP Group
+
+ {% render_field form.protocol %} + {% render_field form.group_id %} + {% render_field form.description %} + {% render_field form.tags %} +
+ +
+
+
Authentication
+
+ {% render_field form.auth_type %} + {% render_field form.auth_key %} +
+ + {% if not form.instance.pk %} +
+
+
Virtual IP Address
+
+ {% render_field form.ip_vrf %} + {% render_field form.ip_address %} + {% render_field form.ip_status %} +
+ {% endif %} + + {% if form.custom_fields %} +
+
Custom Fields
+
+ {% render_custom_fields form %} + {% endif %} +{% endblock %} diff --git a/netbox/templates/ipam/fhrpgroupassignment_edit.html b/netbox/templates/ipam/fhrpgroupassignment_edit.html new file mode 100644 index 000000000..5801febca --- /dev/null +++ b/netbox/templates/ipam/fhrpgroupassignment_edit.html @@ -0,0 +1,18 @@ +{% extends 'generic/object_edit.html' %} +{% load form_helpers %} + +{% block form %} +
+
+
FHRP Group Assignment
+
+
+ +
+ +
+
+ {% render_field form.group %} + {% render_field form.priority %} +
+{% endblock %} diff --git a/netbox/templates/ipam/inc/panels/fhrp_groups.html b/netbox/templates/ipam/inc/panels/fhrp_groups.html new file mode 100644 index 000000000..e5cb26104 --- /dev/null +++ b/netbox/templates/ipam/inc/panels/fhrp_groups.html @@ -0,0 +1,54 @@ +{% load helpers %} + +
+
NHRP Groups
+
+ + + + + + + + + + + {% for assignment in object.fhrp_group_assignments.all %} + + + + + + + {% empty %} + + + + {% endfor %} + +
GroupProtocolVirtual IPsPriority
+ {{ assignment.group.group_id }} + + {{ assignment.group.get_protocol_display }} + + {% for ipaddress in assignment.group.ip_addresses.all %} + {{ ipaddress }} + {% if not forloop.last %}
{% endif %} + {% endfor %} +
+ {{ assignment.priority }} +
None
+
+ +
diff --git a/netbox/templates/ipam/ipaddress.html b/netbox/templates/ipam/ipaddress.html index 31782bdd7..c39f4398a 100644 --- a/netbox/templates/ipam/ipaddress.html +++ b/netbox/templates/ipam/ipaddress.html @@ -73,12 +73,14 @@ Assignment - {% if object.assigned_object %} - {{ object.assigned_object.parent_object }} / - {{ object.assigned_object }} - {% else %} - + {% if object.assigned_object %} + {% if object.assigned_object.parent_object %} + {{ object.assigned_object.parent_object }} / {% endif %} + {{ object.assigned_object }} + {% else %} + + {% endif %} diff --git a/netbox/templates/virtualization/vminterface.html b/netbox/templates/virtualization/vminterface.html index 2646686e8..105adb6b7 100644 --- a/netbox/templates/virtualization/vminterface.html +++ b/netbox/templates/virtualization/vminterface.html @@ -76,11 +76,12 @@
+ {% include 'inc/panels/tags.html' %} {% plugin_left_page object %}
{% include 'inc/panels/custom_fields.html' %} - {% include 'inc/panels/tags.html' %} + {% include 'ipam/inc/panels/fhrp_groups.html' %} {% plugin_right_page object %}
@@ -99,7 +100,7 @@ {% if perms.ipam.add_ipaddress %}
diff --git a/netbox/virtualization/models.py b/netbox/virtualization/models.py index db2404546..08df36d4d 100644 --- a/netbox/virtualization/models.py +++ b/netbox/virtualization/models.py @@ -398,6 +398,12 @@ class VMInterface(PrimaryModel, BaseInterface): object_id_field='assigned_object_id', related_query_name='vminterface' ) + fhrp_group_assignments = GenericRelation( + to='ipam.FHRPGroupAssignment', + content_type_field='interface_type', + object_id_field='interface_id', + related_query_name='+' + ) objects = RestrictedQuerySet.as_manager() diff --git a/netbox/virtualization/views.py b/netbox/virtualization/views.py index 2294d2c38..5cb4f133a 100644 --- a/netbox/virtualization/views.py +++ b/netbox/virtualization/views.py @@ -8,7 +8,7 @@ from dcim.models import Device from dcim.tables import DeviceTable from extras.views import ObjectConfigContextView from ipam.models import IPAddress, Service -from ipam.tables import InterfaceIPAddressTable, InterfaceVLANTable +from ipam.tables import AssignedIPAddressesTable, InterfaceVLANTable from netbox.views import generic from utilities.tables import paginate_table from utilities.utils import count_related @@ -421,7 +421,7 @@ class VMInterfaceView(generic.ObjectView): def get_extra_context(self, request, instance): # Get assigned IP addresses - ipaddress_table = InterfaceIPAddressTable( + ipaddress_table = AssignedIPAddressesTable( data=instance.ip_addresses.restrict(request.user, 'view').prefetch_related('vrf', 'tenant'), orderable=False )