App-Netdisco
view release on metacpan or search on metacpan
lib/App/Netdisco/Web/Plugin/Device/Ports.pm view on Meta::CPAN
package App::Netdisco::Web::Plugin::Device::Ports;
use Dancer ':syntax';
use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::Util::Permission 'acl_matches';
use App::Netdisco::Util::Port qw/port_acl_service port_acl_pvid port_acl_name/;
use App::Netdisco::Web::Plugin;
use List::MoreUtils 'singleton';
use NetAddr::MAC ();
use DBIx::Class::ResultClass::HashRefInflator;
use URI::Escape 'uri_escape';
register_device_tab({ tag => 'ports', label => 'Ports', provides_csv => 1 });
# $nodes_name names a has_many relation, but the source it reads from is a
# different name again.
my %node_result_class = (
nodes => 'Node',
active_nodes => 'Virtual::ActiveNode',
nodes_with_age => 'Virtual::NodeWithAge',
active_nodes_with_age => 'Virtual::ActiveNodeWithAge',
);
# Fetches nodes for one device and groups them by port name. Split out from
# the route so xt can call it without a session, the route being require_login.
#
# HashRefInflator returns plain hashrefs, and Template Toolkit's FOREACH over
# a bare hashref iterates its key/value pairs instead of treating it as one
# item, so manufacturer (belongs_to) and netbios (might_have) are wrapped in
# an arrayref to restore the one-row-or-none shape the template expects.
# wireless is has_many and arrives as an arrayref already.
sub _stitch_nodes {
my ($schema, $device_ip, $rows, $nodes_name, $ips_name, $node_order,
$extra_prefetch, $only_ports) = @_;
my %cond = (switch => $device_ip);
$cond{port} = { -in => $only_ports }
if $only_ports and scalar @$only_ports;
my $node_rs = $schema->resultset($node_result_class{$nodes_name})
->search(\%cond, {
order_by => $node_order,
prefetch => [ $ips_name, @{ $extra_prefetch || [] } ],
});
$node_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
my %nodes_by_port = ();
for my $node ($node_rs->all) {
$node->{stitched_ips} = delete $node->{$ips_name} || [];
$node->{net_mac} = NetAddr::MAC->new(mac => ($node->{mac} || ''));
$node->{manufacturer} = [ $node->{manufacturer} ] if $node->{manufacturer};
$node->{netbios} = [ $node->{netbios} ] if $node->{netbios};
push @{ $nodes_by_port{ $node->{port} } }, $node;
}
# attached here rather than by the caller, so a test exercises the same
# key the template reads.
$_->{stitched_nodes} = ($nodes_by_port{ $_->port } || []) for @$rows;
return;
}
# Decides whether to fetch nodes for this view, and how widely, as the
# $only_ports argument to _stitch_nodes. Kept free of param() so xt can pin
# the decision without a session.
#
# The neighbors-only branch is the shipped default, c_neighbors being checked
# in config.yml and c_nodes not. It reads node data only to name the MAC
# behind a port's remote_ip, so it fetches nothing when no port carries one,
# and only those ports when some do.
#
# Returns undef to fetch nothing, or an arrayref for $only_ports: empty means
lib/App/Netdisco/Web/Plugin/Device/Ports.pm view on Meta::CPAN
my $ips_name = ((param('n_ip4') and param('n_ip6')) ? 'ips'
: param('n_ip4') ? 'ip4s'
: 'ip6s');
my @node_order = (
\qq{regexp_replace(COALESCE(me.vlan, '0'), '[^0-9]*' ,'0') :: integer},
'me.mac',
"${ips_name}.ip",
);
my @extra_prefetch = ();
push @extra_prefetch, 'wireless' if param('n_ssid');
push @extra_prefetch, 'netbios' if param('n_netbios');
push @extra_prefetch, 'manufacturer' if param('n_vendor');
return ($nodes_name, $ips_name, \@node_order, \@extra_prefetch);
}
# One query, run once per c_nodes request rather than once per row: reads the
# same table _attach_search_shadow does, under the same active fragment, so a
# port cannot say "N nodes" here and expand to a different number there.
sub _count_nodes_by_port {
my ($schema, $device_ip, $rows, $active_fragment) = @_;
my $sql = sprintf(q{
SELECT n.port, count(*) AS n
FROM node n
WHERE n.switch = ? %s
GROUP BY n.port
}, $active_fragment);
my $counts = $schema->storage->dbh_do(sub {
my (undef, $dbh) = @_;
$dbh->selectall_arrayref($sql, undef, $device_ip);
});
my %count_by_port = map {; $_->[0] => $_->[1] } @$counts;
$_->{node_count} = ($count_by_port{ $_->port } || 0) for @$rows;
return \%count_by_port;
}
# Carries the display options that change how a node row renders, built once
# here rather than in the template, so the deferred fetch draws the same
# markup the eager path would have. mac_format feeds mac_format_call in
# Web.pm, which every node's MAC is rendered through. Kept free of param()
# so xt can pin the querystring without a session.
sub _deferred_node_params {
my ($n_ip4, $n_ip6, $n_dns, $n_age, $n_ssid, $n_vendor, $n_netbios,
$n_archived, $mac_format) = @_;
my $qs = '';
$qs .= '&n_ip4=1' if $n_ip4;
$qs .= '&n_ip6=1' if $n_ip6;
$qs .= '&n_dns=1' if $n_dns;
$qs .= '&n_age=1' if $n_age;
$qs .= '&n_ssid=1' if $n_ssid;
$qs .= '&n_vendor=1' if $n_vendor;
$qs .= '&n_netbios=1' if $n_netbios;
$qs .= '&n_archived=1' if $n_archived;
$qs .= '&mac_format=' . uri_escape($mac_format) if $mac_format;
return $qs;
}
# Ports at or under the threshold get their own row fetched; a port over it
# still gets one when it might carry a neighbor's own MAC, since the
# Neighbors column reads stitched_nodes for that regardless of the node
# list's own threshold, but only under c_neighbors: that carve-out is what
# makes neighbor_mac renderable at all, and stitching every node of an
# over-threshold trunk port just to throw the render away is expensive on
# exactly the ports a core switch has the most of. Kept free of param() and
# setting() so xt can pin the decision without a session.
sub _threshold_scope {
my ($threshold, $want_neighbors, $count_by_port, $rows) = @_;
return [ map { $_->port }
grep { ($count_by_port->{ $_->port } || 0) <= $threshold
or ($want_neighbors and $_->remote_ip) } @$rows ];
}
# device ports with a description (er, name) matching
get '/ajax/content/device/ports' => require_login sub {
my $q = param('q');
my $prefer = param('prefer');
$prefer = ''
unless defined $prefer and $prefer =~ m/^(?:port|name|vlan)$/;
my $device = schema(vars->{'tenant'})->resultset('Device')
->search_for_device($q) or send_error('Bad device', 400);
my $set = $device->ports->with_properties->with_custom_fields;
# refine by ports if requested
my $f = param('f');
if ($f) {
if (($prefer eq 'vlan') or (not $prefer and $f =~ m/^\d+$/)) {
return unless $f =~ m/^\d+$/;
}
else {
if (param('partial')) {
# change wildcard chars to SQL
$f =~ s/\*/%/g;
$f =~ s/\?/_/g;
# set wildcards at param boundaries
if ($f !~ m/[%_]/) {
$f =~ s/^\%*/%/;
$f =~ s/\%*$/%/;
}
# enable ILIKE op
$f = { (param('invert') ? '-not_ilike' : '-ilike') => $f };
}
elsif (param('invert')) {
$f = { '!=' => $f };
}
if (($prefer eq 'port') or not $prefer and
$set->search({-or => ['me.port' => $f, 'me.descr' => $f]})->count) {
$set = $set->search({
-or => [
'me.port' => $f,
'me.descr' => $f,
'me.slave_of' => $f,
( run in 1.073 second using v1.01-cache-2.11-cpan-54e63673c56 )