From 7566e64886d836d73beea6c413a47d2ec992a0f6 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Tue, 26 Jul 2022 09:49:07 +0100 Subject: [PATCH] #882 option to display vlan names instead of numbers in device ports table --- lib/App/Netdisco/DB/Result/DevicePort.pm | 20 ++++++++++++ lib/App/Netdisco/DB/Result/DevicePortVlan.pm | 4 +-- lib/App/Netdisco/Web/Plugin/Device/Ports.pm | 16 +++++++-- share/config.yml | 13 ++++---- share/views/ajax/device/ports.tt | 34 +++++++++++--------- 5 files changed, 61 insertions(+), 26 deletions(-) diff --git a/lib/App/Netdisco/DB/Result/DevicePort.pm b/lib/App/Netdisco/DB/Result/DevicePort.pm index d23de02c..da1c24a2 100644 --- a/lib/App/Netdisco/DB/Result/DevicePort.pm +++ b/lib/App/Netdisco/DB/Result/DevicePort.pm @@ -203,6 +203,26 @@ __PACKAGE__->might_have( } ); +=head2 native_vlan + +Returns the row from the C table corresponding to the native +vlan of this port. + +=cut + +__PACKAGE__->belongs_to( + native_vlan => 'App::Netdisco::DB::Result::DeviceVlan', + sub { + my $args = shift; + return { + "$args->{foreign_alias}.ip" => + { '-ident' => "$args->{self_alias}.ip" }, + "$args->{self_alias}.vlan" => + { '=' => \"cast($args->{foreign_alias}.vlan as text)" } + }; + }, +); + =head2 agg_master Returns another row from the C table if this port is slave diff --git a/lib/App/Netdisco/DB/Result/DevicePortVlan.pm b/lib/App/Netdisco/DB/Result/DevicePortVlan.pm index a63e6d31..e03f5a33 100644 --- a/lib/App/Netdisco/DB/Result/DevicePortVlan.pm +++ b/lib/App/Netdisco/DB/Result/DevicePortVlan.pm @@ -60,14 +60,14 @@ __PACKAGE__->belongs_to( port => 'App::Netdisco::DB::Result::DevicePort', { 'foreign.ip' => 'self.ip', 'foreign.port' => 'self.port', }); -=head2 vlan +=head2 vlan_entry Returns the entry from the C table describing this VLAN in detail, typically in order that the C can be retrieved. =cut -__PACKAGE__->belongs_to( vlan => 'App::Netdisco::DB::Result::DeviceVlan', { +__PACKAGE__->belongs_to( vlan_entry => 'App::Netdisco::DB::Result::DeviceVlan', { 'foreign.ip' => 'self.ip', 'foreign.vlan' => 'self.vlan', }); diff --git a/lib/App/Netdisco/Web/Plugin/Device/Ports.pm b/lib/App/Netdisco/Web/Plugin/Device/Ports.pm index 3fa630d7..c493d826 100644 --- a/lib/App/Netdisco/Web/Plugin/Device/Ports.pm +++ b/lib/App/Netdisco/Web/Plugin/Device/Ports.pm @@ -108,10 +108,11 @@ get '/ajax/content/device/ports' => require_login sub { my $vlans = $set->search({}, { select => [ 'port', - { array_agg => 'port_vlans.vlan', -as => 'vlan_set' }, { count => 'port_vlans.vlan', -as => 'vlan_count' }, + { array_agg => 'port_vlans.vlan', -as => 'vlan_set' }, + { array_agg => 'vlan_entry.description', -as => 'vlan_name_set' }, ], - join => 'port_vlans', + join => {'port_vlans' => 'vlan_entry'}, group_by => 'me.port', }); @@ -119,12 +120,21 @@ get '/ajax/content/device/ports' => require_login sub { $vlans = { map {( $_->port => { # DBIC smart enough to work out this should be an arrayref :) - vlan_set => $_->get_column('vlan_set'), vlan_count => $_->get_column('vlan_count'), + vlan_set => $_->get_column('vlan_set'), + vlan_name_set => $_->get_column('vlan_name_set'), }, )} $vlans->all }; } + if (param('c_vlan_names')) { + $set = $set->search({}, { + 'join' => 'native_vlan', + '+select' => [qw/native_vlan.description/], + '+as' => [qw/native_vlan_name/], + }); + } + # get aggregate master status (self join) $set = $set->search({}, { 'join' => 'agg_master', diff --git a/share/config.yml b/share/config.yml index 2977ffd5..d3ca79b8 100644 --- a/share/config.yml +++ b/share/config.yml @@ -126,12 +126,13 @@ sidebar_defaults: c_mtu: { label: 'MTU', default: null, idx: 12 } c_pvid: { label: 'Native VLAN', default: checked, idx: 13 } c_vmember: { label: 'VLAN Membership', default: checked, idx: 14 } - c_power: { label: 'PoE', default: null, idx: 15 } - c_ssid: { label: 'SSID', default: null, idx: 16 } - c_nodes: { label: 'Connected Nodes', default: null, idx: 17 } - c_neighbors: { label: 'Connected Devices', default: checked, idx: 18 } - c_stp: { label: 'Spanning Tree', default: null, idx: 19 } - c_up: { label: 'Status', default: null, idx: 20 } + c_vlan_names: { label: 'use VLAN Names', default: null, idx: 15 } + c_power: { label: 'PoE', default: null, idx: 16 } + c_ssid: { label: 'SSID', default: null, idx: 17 } + c_nodes: { label: 'Connected Nodes', default: null, idx: 18 } + c_neighbors: { label: 'Connected Devices', default: checked, idx: 19 } + c_stp: { label: 'Spanning Tree', default: null, idx: 20 } + c_up: { label: 'Status', default: null, idx: 21 } mac_format: { default: IEEE } n_inventory: { label: 'Remote Inventory', default: checked, idx: 0 } n_detailed_inventory: { label: 'Remote Advertisement', default: null, idx: 1 } diff --git a/share/views/ajax/device/ports.tt b/share/views/ajax/device/ports.tt index 0bb10b1f..2947892b 100644 --- a/share/views/ajax/device/ports.tt +++ b/share/views/ajax/device/ports.tt @@ -4,7 +4,7 @@ [% FOREACH item IN settings.port_columns %] - [% NEXT IF item.name == 'c_admin' %] + [% NEXT IF item.name == 'c_admin' OR item.name == 'c_vlan_names' %] [% NEXT IF item.name == 'c_nodes' AND params.c_nodes AND params.c_neighbors %] [% NEXT UNLESS params.${item.name} %] [% SET th_class = '' %] @@ -204,8 +204,13 @@ [% IF row.vlan AND row.vlan > 0 %] - [% row.vlan | html_entity %] + [% IF params.c_vlan_names %] + href="[% uri_for('/search') | none %]?tab=vlan&q=[% row.get_column('native_vlan_name') | uri %]"> + [% row.get_column('native_vlan_name') | html_entity %] + [% ELSE %] + href="[% uri_for('/search') | none %]?tab=vlan&q=[% row.vlan | uri %]"> + [% row.vlan | html_entity %] + [% END %] [% END %] [% END %] @@ -215,21 +220,20 @@ [% IF vlans.$portname.vlan_count <= settings.devport_vlan_limit %] [% SET vlan_count = vlans.$portname.vlan_count %] - [% SET output = '' %] - [% FOREACH vlan IN vlans.$portname.vlan_set.nsort %] - [% IF vlan == 0 %][% SET vlan_count = (vlan_count - 1) %][% NEXT %][% END %] - [% SET output = output _ - '' _ vlan _ '' %] - [% IF NOT loop.last %][% SET output = output _ ', ' %][% END %] + [% IF params.c_vlan_names %][% SET vlanlist = vlans.$portname.vlan_name_set.sort %] + [% ELSE %][% SET vlanlist = vlans.$portname.vlan_set.nsort %][% END %] + [% IF vlan_count > 10 %] [%# TODO make this a settable variable %] +
([% vlan_count %])
+
Show VLANs
+
+ [% END %] + [% FOREACH vlan IN vlanlist %] + [% vlan | html_entity %] + [% ', ' IF NOT loop.last %] [% END %] [% IF vlan_count > 10 %] [%# TODO make this a settable variable %] - [% SET output = '
(' _ vlan_count - _ ')
-
Show VLANs
-
' _ output %] - [% SET output = output _ '
' %] +
[% END %] - [% output | none %] [% ELSE %] ([% vlans.$portname.vlan_count %] is too many to list) [% END %]