Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions nautobot_bgp_models/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from nautobot.extras.filters.mixins import RoleModelFilterSetMixin
from nautobot.extras.models import Role
from nautobot.ipam.models import VRF
from nautobot.tenancy.models import Tenant

from . import choices, models

Expand All @@ -27,6 +28,10 @@ class AutonomousSystemFilterSet(NautobotFilterSet, StatusModelFilterSetMixin):
"description": "icontains",
},
)
tenant = django_filters.ModelMultipleChoiceFilter(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible to use nautobot.tenancy.filters.mixins.TenancyModelFilterSetMixin instead of this explicit declaration?

queryset=Tenant.objects.all(),
label="Tenant",
)

class Meta:
model = models.AutonomousSystem
Expand Down
17 changes: 16 additions & 1 deletion nautobot_bgp_models/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ class AutonomousSystemForm(NautobotModelForm):

tags = DynamicModelMultipleChoiceField(queryset=Tag.objects.all(), required=False)
provider = DynamicModelChoiceField(queryset=Provider.objects.all(), required=False)
tenant = DynamicModelChoiceField(
queryset=Tenant.objects.all(),
required=False,
label="Tenant",
)

class Meta:
model = models.AutonomousSystem
fields = ("asn", "description", "provider", "status", "tags")
fields = ("asn", "description", "tenant", "provider", "status", "tags")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest using TenancyForm as a mixin on the form class, which will automatically add the relevant form fields.



class AutonomousSystemFilterForm(NautobotFilterForm):
Copy link
Contributor

@glennmatthews glennmatthews Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, please add TenancyFilterForm to this form class. You'll also need to update the AutonomousSystemFilterSet with the TenancyModelFilterSetMixin mixin class to support filtering by tenant.

Expand All @@ -37,6 +42,11 @@ class AutonomousSystemFilterForm(NautobotFilterForm):
model = models.AutonomousSystem
field_order = ["status", "tag"]
tag = TagFilterField(model)
tenant = DynamicModelMultipleChoiceField(
queryset=Tenant.objects.all(),
required=False,
label="Tenant",
)


class AutonomousSystemBulkEditForm(TagsBulkEditFormMixin, NautobotBulkEditForm):
Expand All @@ -46,6 +56,11 @@ class AutonomousSystemBulkEditForm(TagsBulkEditFormMixin, NautobotBulkEditForm):
queryset=models.AutonomousSystem.objects.all(), widget=forms.MultipleHiddenInput()
)
description = forms.CharField(max_length=200, required=False)
tenant = DynamicModelChoiceField(
queryset=Tenant.objects.all(),
required=False,
label="Tenant",
)

class Meta:
nullable_fields = [
Expand Down
20 changes: 20 additions & 0 deletions nautobot_bgp_models/migrations/0011_autonomoussystem_tenant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2.17 on 2025-02-26 19:23

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('tenancy', '0009_update_all_charfields_max_length_to_255'),
('nautobot_bgp_models', '0010_alter_autonomoussystem_status_and_more'),
]

operations = [
migrations.AddField(
model_name='autonomoussystem',
name='tenant',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='tenancy.tenant'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2.19 on 2025-02-28 21:54

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('tenancy', '0009_update_all_charfields_max_length_to_255'),
('nautobot_bgp_models', '0011_autonomoussystem_tenant'),
]

operations = [
migrations.AlterField(
model_name='autonomoussystem',
name='tenant',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='autonomous_systems', to='tenancy.tenant'),
),
]
8 changes: 8 additions & 0 deletions nautobot_bgp_models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ class AutonomousSystem(PrimaryModel):
asn = ASNField(unique=True, verbose_name="ASN", help_text="32-bit autonomous system number")
description = models.CharField(max_length=200, blank=True)
provider = models.ForeignKey(to=Provider, on_delete=models.PROTECT, blank=True, null=True)
tenant = models.ForeignKey(
Tenant,
on_delete=models.PROTECT,
related_name="autonomous_systems",
blank=True,
null=True,
help_text="The tenant this Autonomous System belongs to",
)
status = StatusField(null=True)

class Meta:
Expand Down
6 changes: 6 additions & 0 deletions nautobot_bgp_models/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ class AutonomousSystemTable(StatusTableMixin, BaseTable):
pk = ToggleColumn()
asn = tables.TemplateColumn(template_code=ASN_LINK, verbose_name="ASN")
provider = tables.LinkColumn()
tenant = tables.LinkColumn(
viewname="tenancy:tenant",
args=[A("tenant__pk")],
verbose_name="Tenant",
text=lambda record: record.tenant.name if record.tenant else "",
)
Comment on lines +37 to +42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is all this configuration needed? I would have thought tenant = tables.Column(linkify=True) would work just as well.

Copy link
Contributor

@smk4664 smk4664 Mar 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have TenantColumn as well.

from nautobot.tenancy.tables import TenantColumn


...

tenant = TenantColumn()

tags = TagColumn(url_name="plugins:nautobot_bgp_models:autonomoussystem_list")
actions = ButtonsColumn(model=models.AutonomousSystem)
asn_asdot = tables.Column(accessor=A("asn_asdot"), linkify=True, order_by=A("asn"), verbose_name="ASN ASDOT")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
<td>Description</td>
<td>{{ object.description }}</td>
</tr>
<tr>
<td>Tenant</td>
<td>{{ object.tenant | hyperlinked_object }}</td>
</tr>
<tr>
<td>Status</td>
<td>
Expand Down
Loading