diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin/Device/Ports.pm b/Netdisco/lib/App/Netdisco/Web/Plugin/Device/Ports.pm index 0f0f04f1..1693e624 100644 --- a/Netdisco/lib/App/Netdisco/Web/Plugin/Device/Ports.pm +++ b/Netdisco/lib/App/Netdisco/Web/Plugin/Device/Ports.pm @@ -1,17 +1,16 @@ package App::Netdisco::Web::Plugin::Device::Ports; use Dancer ':syntax'; -use Dancer::Plugin::Ajax; use Dancer::Plugin::DBIC; use Dancer::Plugin::Auth::Extensible; use App::Netdisco::Util::Web (); # for sort_port use App::Netdisco::Web::Plugin; -register_device_tab({ tag => 'ports', label => 'Ports' }); +register_device_tab({ tag => 'ports', label => 'Ports', provides_csv => 1 }); # device ports with a description (er, name) matching -ajax '/ajax/content/device/ports' => require_login sub { +get '/ajax/content/device/ports' => require_login sub { my $q = param('q'); my $device = schema('netdisco')->resultset('Device') @@ -122,12 +121,21 @@ ajax '/ajax/content/device/ports' => require_login sub { my $results = [ sort { &App::Netdisco::Util::Web::sort_port($a->port, $b->port) } $set->all ]; return unless scalar @$results; - content_type('text/html'); - template 'ajax/device/ports.tt', { - results => $results, - nodes => $nodes_name, - device => $device, - }, { layout => undef }; + if (request->is_ajax) { + template 'ajax/device/ports.tt', { + results => $results, + nodes => $nodes_name, + device => $device, + }, { layout => undef }; + } + else { + header( 'Content-Type' => 'text/comma-separated-values' ); + template 'ajax/device/ports_csv.tt', { + results => $results, + nodes => $nodes_name, + device => $device, + }, { layout => undef }; + } }; true; diff --git a/Netdisco/share/views/ajax/device/ports_csv.tt b/Netdisco/share/views/ajax/device/ports_csv.tt new file mode 100644 index 00000000..0bc37995 --- /dev/null +++ b/Netdisco/share/views/ajax/device/ports_csv.tt @@ -0,0 +1,195 @@ +[% USE CSV -%] + +[% SET headers = [] %] +[% SET c_nodes_pos = -1 %] +[% FOREACH item IN vars.port_columns %] + [% NEXT IF item.name == 'c_admin' %] + [% NEXT UNLESS params.${item.name} %] + + [% headers.push(item.label) %] + + [% IF item.name == 'c_nodes' %] + [% SET c_nodes_pos = headers.max %] + [% headers.push('Archived Node') %] + [% headers.push('Age Stamp') IF params.n_age %] + [% headers.push('IP Address') IF params.n_ip %] + [% headers.push('Archived IP') IF params.n_ip %] + [% END %] +[% END %] +[% CSV.dump(headers) %] + +[% FOREACH row IN results %] + [% SET myport = [] %] + + [% FOREACH config IN settings._extra_device_port_cols %] + [% NEXT UNLESS config.position == 'left' AND params.${config.name} %] + [% TRY %] + [% PROCESS "plugin/${config.name}/device_port_column_csv.tt" %] + [% CATCH %] + [% myport.push('') %] + [% END %] + [% END %] + + [% IF params.c_port %] + [% myport.push(row.port) %] + [% END %] + + [% FOREACH config IN settings._extra_device_port_cols %] + [% NEXT UNLESS config.position == 'mid' AND params.${config.name} %] + [% TRY %] + [% PROCESS "plugin/${config.name}/device_port_column_csv.tt" %] + [% CATCH %] + [% myport.push('') %] + [% END %] + [% END %] + + [% IF params.c_descr %] + [% myport.push(row.descr) %] + [% END %] + + [% IF params.c_type %] + [% myport.push(row.type) %] + [% END %] + + [% IF params.c_duplex %] + [% IF row.up == 'up' AND row.duplex %] + [% myport.push( (row.duplex_admin.ucfirst || 'Auto') _ ' / ' _ row.duplex.ucfirst ) %] + [% ELSE %] + [% myport.push('') %] + [% END %] + [% END %] + + [% IF params.c_lastchange %] + [% myport.push(row.lastchange_stamp) %] + [% END %] + + [% IF params.c_name %] + [% myport.push(row.name) %] + [% END %] + + [% IF params.c_speed %] + [% myport.push(row.speed) %] + [% END %] + + [% IF params.c_mac %] + [% myport.push(row.mac) %] + [% END %] + + [% IF params.c_mtu %] + [% myport.push(row.mtu) %] + [% END %] + + [% IF params.c_vlan %] + [% myport.push(row.vlan) %] + [% END %] + + [% IF params.c_vmember %] + [% IF row.tagged_vlans_count %] + [% SET output = '' %] + [% SET vlanlist = [] %] + [% FOREACH vlan IN row.tagged_vlans %][% vlanlist.push(vlan.vlan) %][% END %] + [% FOREACH vlan IN vlanlist.nsort %] + [% SET output = output _ ',' IF NOT loop.first %] + [% SET output = output _ vlan %] + [% END %] + [% myport.push(output) %] + [% ELSE %] + [% myport.push('') %] + [% END %] + [% END %] + + [% IF params.c_power %] + [% IF row.power AND row.power.admin == 'true' %] + [% myport.push( (row.power.power || '0') _ 'mW (' _ row.power.status _ ')' ) %] + [% ELSE %] + [% myport.push('') %] + [% END %] + [% END %] + + [% IF params.c_nodes %] + [% myport.push('') %] + [% myport.push('') %] + [% myport.push('') IF params.n_age %] + [% myport.push('') IF params.n_ip %] + [% myport.push('') IF params.n_ip %] + [% END %] + + [% IF params.c_neighbors %] + [% IF params.c_neighbors AND (row.remote_ip OR row.is_uplink) %] + [% IF row.neighbor %] + [% SET output = row.neighbor.dns.remove(settings.domain_suffix) || row.neighbor.ip %] + [% SET output = output _ ' (port: ' _ row.remote_port %] + [% SET output = output _ ' id: '_ row.remote_id IF row.remote_id %] + [% SET output = output _ ' type: '_ row.remote_type IF row.remote_type %] + [% myport.push( output _ ')' ) %] + [% ELSIF row.remote_ip AND row.remote_port %] + [% SET output = row.remote_ip _ ' (port: ' _ row.remote_port %] + [% SET output = output _ ' id: '_ row.remote_id IF row.remote_id %] + [% SET output = output _ ' type: '_ row.remote_type IF row.remote_type %] + [% myport.push( output _ ')' ) %] + [% ELSE %] + [% myport.push('(possible uplink)') %] + [% END %] + [% ELSE %] + [% myport.push('') %] + [% END %] + [% END %] + + [% IF params.c_stp %] + [% myport.push(row.stp) %] + [% END %] + + [% IF params.c_up %] + [% myport.push( row.up_admin.ucfirst _ ' / ' _ row.up.ucfirst ) %] + [% END %] + + [% FOREACH config IN settings._extra_device_port_cols %] + [% NEXT UNLESS config.position == 'right' AND params.${config.name} %] + [% TRY %] + [% PROCESS "plugin/${config.name}/device_port_column_csv.tt" %] + [% CATCH %] + [% myport.push('') %] + [% END %] + [% END %] + + [% SET has_nodes = 0 %] + [% IF params.c_nodes %] + [% FOREACH node IN row.$nodes %] + [% SET has_nodes = 1 %] + [% SET mynode = [] %] + + [% CALL mynode.push(node.net_mac.$mac_format_call) %] + [% CALL mynode.push((node.active ? 'No' : 'Yes')) %] + + [% CALL mynode.push(node.time_last_age) IF params.n_age %] + + [% IF params.n_ip %] + [% mynode.push('') %] + [% mynode.push('') %] + [% SET has_ips = 0 %] + + [% FOREACH ip IN node.ips %] + [% SET has_ips = 1 %] + [% SET dns = ip.dns %] + [% IF dns %] + [% CALL mynode.splice(-2, 1, dns _ ' (' _ ip.ip _ ')') %] + [% ELSE %] + [% CALL mynode.splice(-2, 1, ip.ip) %] + [% END %] + + [% CALL mynode.splice(-1, 1, (ip.active ? 'No' : 'Yes')) %] + + [% CALL myport.splice(c_nodes_pos, mynode.size, mynode) %] + [% CSV.dump(myport) %] + [% END %] + + [% CSV.dump(myport) IF NOT has_ips %] + [% ELSE %] + [% CALL myport.splice(c_nodes_pos, mynode.size, mynode) %] + [% CSV.dump(myport) %] + [% END %] + [% END %] + [% END %] + + [% CSV.dump(myport) IF NOT has_nodes %] +[% END %]