Skip to content

Commit

Permalink
Closes #10710: Add status field to WirelessLAN
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Nov 4, 2022
1 parent cdeb65e commit ad40d42
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 28 deletions.
7 changes: 7 additions & 0 deletions docs/models/wireless/wirelesslan.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ The service set identifier (SSID) for the wireless network.

The [wireless LAN group](./wirelesslangroup.md) to which this wireless LAN is assigned (if any).

### Status

The operational status of the wireless network.

!!! tip
Additional statuses may be defined by setting `WirelessLAN.status` under the [`FIELD_CHOICES`](../../configuration/data-validation.md#field_choices) configuration parameter.

### VLAN

Each wireless LAN can optionally be mapped to a [VLAN](../ipam/vlan.md), to model a bridge between wired and wireless segments.
Expand Down
4 changes: 4 additions & 0 deletions netbox/templates/wireless/wirelesslan.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ <h5 class="card-header">Wireless LAN</h5>
<td>Group</td>
<td>{{ object.group|linkify|placeholder }}</td>
</tr>
<tr>
<td>Status</td>
<td>{% badge object.get_status_display bg_color=object.get_status_color %}</td>
</tr>
<tr>
<th scope="row">Description</th>
<td>{{ object.description|placeholder }}</td>
Expand Down
5 changes: 3 additions & 2 deletions netbox/wireless/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Meta:
class WirelessLANSerializer(NetBoxModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name='wireless-api:wirelesslan-detail')
group = NestedWirelessLANGroupSerializer(required=False, allow_null=True)
status = ChoiceField(choices=WirelessLANStatusChoices, required=False, allow_blank=True)
vlan = NestedVLANSerializer(required=False, allow_null=True)
tenant = NestedTenantSerializer(required=False, allow_null=True)
auth_type = ChoiceField(choices=WirelessAuthTypeChoices, required=False, allow_blank=True)
Expand All @@ -41,8 +42,8 @@ class WirelessLANSerializer(NetBoxModelSerializer):
class Meta:
model = WirelessLAN
fields = [
'id', 'url', 'display', 'ssid', 'description', 'group', 'vlan', 'tenant', 'auth_type', 'auth_cipher',
'auth_psk', 'description', 'comments', 'tags', 'custom_fields', 'created', 'last_updated',
'id', 'url', 'display', 'ssid', 'description', 'group', 'status', 'vlan', 'tenant', 'auth_type',
'auth_cipher', 'auth_psk', 'description', 'comments', 'tags', 'custom_fields', 'created', 'last_updated',
]


Expand Down
18 changes: 18 additions & 0 deletions netbox/wireless/choices.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.utils.translation import gettext as _

from utilities.choices import ChoiceSet


Expand All @@ -11,6 +13,22 @@ class WirelessRoleChoices(ChoiceSet):
)


class WirelessLANStatusChoices(ChoiceSet):
key = 'WirelessLANS.status'

STATUS_ACTIVE = 'active'
STATUS_RESERVED = 'reserved'
STATUS_DISABLED = 'disabled'
STATUS_DEPRECATED = 'deprecated'

CHOICES = [
(STATUS_ACTIVE, _('Active'), 'green'),
(STATUS_RESERVED, _('Reserved'), 'cyan'),
(STATUS_DISABLED, _('Disabled'), 'orange'),
(STATUS_DEPRECATED, _('Deprecated'), 'red'),
]


class WirelessChannelChoices(ChoiceSet):

# 2.4 GHz
Expand Down
3 changes: 3 additions & 0 deletions netbox/wireless/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class WirelessLANFilterSet(NetBoxModelFilterSet, TenancyFilterSet):
lookup_expr='in',
to_field_name='slug'
)
status = django_filters.MultipleChoiceFilter(
choices=WirelessLANStatusChoices
)
vlan_id = django_filters.ModelMultipleChoiceFilter(
queryset=VLAN.objects.all()
)
Expand Down
6 changes: 5 additions & 1 deletion netbox/wireless/forms/bulk_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class WirelessLANGroupBulkEditForm(NetBoxModelBulkEditForm):


class WirelessLANBulkEditForm(NetBoxModelBulkEditForm):
status = forms.ChoiceField(
choices=add_blank_choice(WirelessLANStatusChoices),
required=False
)
group = DynamicModelChoiceField(
queryset=WirelessLANGroup.objects.all(),
required=False
Expand Down Expand Up @@ -75,7 +79,7 @@ class WirelessLANBulkEditForm(NetBoxModelBulkEditForm):

model = WirelessLAN
fieldsets = (
(None, ('group', 'ssid', 'vlan', 'tenant', 'description')),
(None, ('group', 'ssid', 'status', 'vlan', 'tenant', 'description')),
('Authentication', ('auth_type', 'auth_cipher', 'auth_psk')),
)
nullable_fields = (
Expand Down
8 changes: 6 additions & 2 deletions netbox/wireless/forms/bulk_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class WirelessLANCSVForm(NetBoxModelCSVForm):
to_field_name='name',
help_text='Assigned group'
)
status = CSVChoiceField(
choices=WirelessLANStatusChoices,
help_text='Operational status'
)
vlan = CSVModelChoiceField(
queryset=VLAN.objects.all(),
required=False,
Expand All @@ -61,8 +65,8 @@ class WirelessLANCSVForm(NetBoxModelCSVForm):
class Meta:
model = WirelessLAN
fields = (
'ssid', 'group', 'vlan', 'tenant', 'auth_type', 'auth_cipher', 'auth_psk', 'description', 'comments',
'tags',
'ssid', 'group', 'status', 'vlan', 'tenant', 'auth_type', 'auth_cipher', 'auth_psk', 'description',
'comments', 'tags',
)


Expand Down
7 changes: 6 additions & 1 deletion netbox/wireless/forms/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class WirelessLANFilterForm(TenancyFilterForm, NetBoxModelFilterSetForm):
model = WirelessLAN
fieldsets = (
(None, ('q', 'filter', 'tag')),
('Attributes', ('ssid', 'group_id',)),
('Attributes', ('ssid', 'group_id', 'status')),
('Tenant', ('tenant_group_id', 'tenant_id')),
('Authentication', ('auth_type', 'auth_cipher', 'auth_psk')),
)
Expand All @@ -43,6 +43,11 @@ class WirelessLANFilterForm(TenancyFilterForm, NetBoxModelFilterSetForm):
null_option='None',
label=_('Group')
)
status = forms.ChoiceField(
required=False,
choices=add_blank_choice(WirelessLANStatusChoices),
widget=StaticSelect()
)
auth_type = forms.ChoiceField(
required=False,
choices=add_blank_choice(WirelessAuthTypeChoices),
Expand Down
6 changes: 3 additions & 3 deletions netbox/wireless/forms/model_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class WirelessLANForm(TenancyForm, NetBoxModelForm):
queryset=WirelessLANGroup.objects.all(),
required=False
)

region = DynamicModelChoiceField(
queryset=Region.objects.all(),
required=False,
Expand Down Expand Up @@ -85,7 +84,7 @@ class WirelessLANForm(TenancyForm, NetBoxModelForm):
comments = CommentField()

fieldsets = (
('Wireless LAN', ('ssid', 'group', 'description', 'tags')),
('Wireless LAN', ('ssid', 'group', 'status', 'description', 'tags')),
('VLAN', ('region', 'site_group', 'site', 'vlan_group', 'vlan',)),
('Tenancy', ('tenant_group', 'tenant')),
('Authentication', ('auth_type', 'auth_cipher', 'auth_psk')),
Expand All @@ -94,10 +93,11 @@ class WirelessLANForm(TenancyForm, NetBoxModelForm):
class Meta:
model = WirelessLAN
fields = [
'ssid', 'group', 'region', 'site_group', 'site', 'vlan_group', 'vlan', 'tenant_group', 'tenant',
'ssid', 'group', 'region', 'site_group', 'site', 'status', 'vlan_group', 'vlan', 'tenant_group', 'tenant',
'auth_type', 'auth_cipher', 'auth_psk', 'description', 'comments', 'tags',
]
widgets = {
'status': StaticSelect,
'auth_type': StaticSelect,
'auth_cipher': StaticSelect,
}
Expand Down
18 changes: 18 additions & 0 deletions netbox/wireless/migrations/0008_wirelesslan_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.2 on 2022-11-04 17:07

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('wireless', '0007_standardize_description_comments'),
]

operations = [
migrations.AddField(
model_name='wirelesslan',
name='status',
field=models.CharField(default='active', max_length=50),
),
]
8 changes: 8 additions & 0 deletions netbox/wireless/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ class WirelessLAN(WirelessAuthenticationBase, PrimaryModel):
blank=True,
null=True
)
status = models.CharField(
max_length=50,
choices=WirelessLANStatusChoices,
default=WirelessLANStatusChoices.STATUS_ACTIVE
)
vlan = models.ForeignKey(
to='ipam.VLAN',
on_delete=models.PROTECT,
Expand Down Expand Up @@ -111,6 +116,9 @@ def __str__(self):
def get_absolute_url(self):
return reverse('wireless:wirelesslan', args=[self.pk])

def get_status_color(self):
return WirelessLANStatusChoices.colors.get(self.status)


def get_wireless_interface_types():
# Wrap choices in a callable to avoid generating dummy migrations
Expand Down
7 changes: 4 additions & 3 deletions netbox/wireless/tables/wirelesslan.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class WirelessLANTable(TenancyColumnsMixin, NetBoxTable):
group = tables.Column(
linkify=True
)
status = columns.ChoiceFieldColumn()
interface_count = tables.Column(
verbose_name='Interfaces'
)
Expand All @@ -53,10 +54,10 @@ class WirelessLANTable(TenancyColumnsMixin, NetBoxTable):
class Meta(NetBoxTable.Meta):
model = WirelessLAN
fields = (
'pk', 'ssid', 'group', 'tenant', 'tenant_group', 'vlan', 'interface_count', 'auth_type', 'auth_cipher',
'auth_psk', 'description', 'comments', 'tags', 'created', 'last_updated',
'pk', 'ssid', 'group', 'status', 'tenant', 'tenant_group', 'vlan', 'interface_count', 'auth_type',
'auth_cipher', 'auth_psk', 'description', 'comments', 'tags', 'created', 'last_updated',
)
default_columns = ('pk', 'ssid', 'group', 'description', 'vlan', 'auth_type', 'interface_count')
default_columns = ('pk', 'ssid', 'group', 'status', 'description', 'vlan', 'auth_type', 'interface_count')


class WirelessLANInterfacesTable(NetBoxTable):
Expand Down
10 changes: 7 additions & 3 deletions netbox/wireless/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,33 +68,37 @@ def setUpTestData(cls):
group.save()

wireless_lans = (
WirelessLAN(ssid='WLAN1'),
WirelessLAN(ssid='WLAN2'),
WirelessLAN(ssid='WLAN3'),
WirelessLAN(ssid='WLAN1', status=WirelessLANStatusChoices.STATUS_ACTIVE),
WirelessLAN(ssid='WLAN2', status=WirelessLANStatusChoices.STATUS_ACTIVE),
WirelessLAN(ssid='WLAN3', status=WirelessLANStatusChoices.STATUS_ACTIVE),
)
WirelessLAN.objects.bulk_create(wireless_lans)

cls.create_data = [
{
'ssid': 'WLAN4',
'group': groups[0].pk,
'status': WirelessLANStatusChoices.STATUS_DISABLED,
'tenant': tenants[0].pk,
'auth_type': WirelessAuthTypeChoices.TYPE_OPEN,
},
{
'ssid': 'WLAN5',
'group': groups[1].pk,
'status': WirelessLANStatusChoices.STATUS_DISABLED,
'tenant': tenants[0].pk,
'auth_type': WirelessAuthTypeChoices.TYPE_WPA_PERSONAL,
},
{
'ssid': 'WLAN6',
'status': WirelessLANStatusChoices.STATUS_DISABLED,
'tenant': tenants[0].pk,
'auth_type': WirelessAuthTypeChoices.TYPE_WPA_ENTERPRISE,
},
]

cls.bulk_update_data = {
'status': WirelessLANStatusChoices.STATUS_DEPRECATED,
'group': groups[2].pk,
'tenant': tenants[1].pk,
'description': 'New description',
Expand Down
52 changes: 46 additions & 6 deletions netbox/wireless/tests/test_filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,18 @@ class WirelessLANTestCase(TestCase, ChangeLoggedFilterSetTests):
def setUpTestData(cls):

groups = (
WirelessLANGroup(name='Wireless LAN Group 1', slug='wireless-lan-group-1'),
WirelessLANGroup(name='Wireless LAN Group 2', slug='wireless-lan-group-2'),
WirelessLANGroup(name='Wireless LAN Group 3', slug='wireless-lan-group-3'),
WirelessLANGroup(
name='Wireless LAN Group 1',
slug='wireless-lan-group-1'
),
WirelessLANGroup(
name='Wireless LAN Group 2',
slug='wireless-lan-group-2'
),
WirelessLANGroup(
name='Wireless LAN Group 3',
slug='wireless-lan-group-3'
),
)
for group in groups:
group.save()
Expand All @@ -86,9 +95,36 @@ def setUpTestData(cls):
Tenant.objects.bulk_create(tenants)

wireless_lans = (
WirelessLAN(ssid='WLAN1', group=groups[0], vlan=vlans[0], tenant=tenants[0], auth_type=WirelessAuthTypeChoices.TYPE_OPEN, auth_cipher=WirelessAuthCipherChoices.CIPHER_AUTO, auth_psk='PSK1'),
WirelessLAN(ssid='WLAN2', group=groups[1], vlan=vlans[1], tenant=tenants[1], auth_type=WirelessAuthTypeChoices.TYPE_WEP, auth_cipher=WirelessAuthCipherChoices.CIPHER_TKIP, auth_psk='PSK2'),
WirelessLAN(ssid='WLAN3', group=groups[2], vlan=vlans[2], tenant=tenants[2], auth_type=WirelessAuthTypeChoices.TYPE_WPA_PERSONAL, auth_cipher=WirelessAuthCipherChoices.CIPHER_AES, auth_psk='PSK3'),
WirelessLAN(
ssid='WLAN1',
group=groups[0],
status=WirelessLANStatusChoices.STATUS_ACTIVE,
vlan=vlans[0],
tenant=tenants[0],
auth_type=WirelessAuthTypeChoices.TYPE_OPEN,
auth_cipher=WirelessAuthCipherChoices.CIPHER_AUTO,
auth_psk='PSK1'
),
WirelessLAN(
ssid='WLAN2',
group=groups[1],
status=WirelessLANStatusChoices.STATUS_DISABLED,
vlan=vlans[1],
tenant=tenants[1],
auth_type=WirelessAuthTypeChoices.TYPE_WEP,
auth_cipher=WirelessAuthCipherChoices.CIPHER_TKIP,
auth_psk='PSK2'
),
WirelessLAN(
ssid='WLAN3',
group=groups[2],
status=WirelessLANStatusChoices.STATUS_RESERVED,
vlan=vlans[2],
tenant=tenants[2],
auth_type=WirelessAuthTypeChoices.TYPE_WPA_PERSONAL,
auth_cipher=WirelessAuthCipherChoices.CIPHER_AES,
auth_psk='PSK3'
),
)
WirelessLAN.objects.bulk_create(wireless_lans)

Expand All @@ -103,6 +139,10 @@ def test_group(self):
params = {'group': [groups[0].slug, groups[1].slug]}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)

def test_status(self):
params = {'status': [WirelessLANStatusChoices.STATUS_ACTIVE, WirelessLANStatusChoices.STATUS_DISABLED]}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)

def test_vlan(self):
vlans = VLAN.objects.all()[:2]
params = {'vlan_id': [vlans[0].pk, vlans[1].pk]}
Expand Down
Loading

0 comments on commit ad40d42

Please sign in to comment.