From f75f1e5cbf51d024b0588ca9b410b0d0abad517f Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Mon, 6 May 2013 00:45:18 +0100 Subject: [PATCH] add frontend update/del forms, and display port count --- Netdisco/lib/App/Netdisco/DB/Result/Device.pm | 10 ++++ .../lib/App/Netdisco/DB/ResultSet/Device.pm | 30 ++++++++++++ .../Web/Plugin/AdminTask/PseudoDevice.pm | 48 ++++++++++++++++++- Netdisco/share/public/css/netdisco.css | 4 ++ .../views/ajax/admintask/pseudodevice.tt | 11 ++++- 5 files changed, 100 insertions(+), 3 deletions(-) diff --git a/Netdisco/lib/App/Netdisco/DB/Result/Device.pm b/Netdisco/lib/App/Netdisco/DB/Result/Device.pm index 19774496..d0b81e24 100644 --- a/Netdisco/lib/App/Netdisco/DB/Result/Device.pm +++ b/Netdisco/lib/App/Netdisco/DB/Result/Device.pm @@ -180,6 +180,16 @@ __PACKAGE__->has_many( =head1 ADDITIONAL COLUMNS +=head2 port_count + +Returns the number of ports on this device. Enable this +column by applying the C modifier to C. + +=cut + +sub port_count { return (shift)->get_column('port_count') } + + =head2 uptime_age Formatted version of the C field. diff --git a/Netdisco/lib/App/Netdisco/DB/ResultSet/Device.pm b/Netdisco/lib/App/Netdisco/DB/ResultSet/Device.pm index f5e6f0f8..097fd902 100644 --- a/Netdisco/lib/App/Netdisco/DB/ResultSet/Device.pm +++ b/Netdisco/lib/App/Netdisco/DB/ResultSet/Device.pm @@ -491,4 +491,34 @@ sub get_distinct_col { )->get_column($col)->all; } +=head2 with_port_count + +This is a modifier for any C which +will add the following additional synthesized column to the result set: + +=over 4 + +=item port_count + +=back + +=cut + +sub with_port_count { + my ($rs, $cond, $attrs) = @_; + + return $rs + ->search_rs($cond, $attrs) + ->search({}, + { + '+columns' => { port_count => + $rs->result_source->schema->resultset('DevicePort') + ->search( + { 'dp.ip' => { -ident => 'me.ip' } }, + { alias => 'dp' } + )->count_rs->as_query + }, + }); +} + 1; diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin/AdminTask/PseudoDevice.pm b/Netdisco/lib/App/Netdisco/Web/Plugin/AdminTask/PseudoDevice.pm index 6ac3a208..5a41bca7 100644 --- a/Netdisco/lib/App/Netdisco/Web/Plugin/AdminTask/PseudoDevice.pm +++ b/Netdisco/lib/App/Netdisco/Web/Plugin/AdminTask/PseudoDevice.pm @@ -5,18 +5,64 @@ use Dancer::Plugin::Ajax; use Dancer::Plugin::DBIC; use App::Netdisco::Web::Plugin; +use NetAddr::IP::Lite ':lower'; +use Try::Tiny; register_admin_task({ tag => 'pseudodevice', label => 'Manage Pseudo Devices', }); +sub _sanity_ok { + my $happy = 0; + + try { + return 0 unless length param('dns') + and param('dns') =~ m/^[[:print:]]+$/ + and param('dns') !~ m/[[:space:]]/; + + my $ip = NetAddr::IP::Lite->new(param('ip')); + return 0 if $ip->addr eq '0.0.0.0'; + + return 0 unless length param('ports') + and param('ports') =~ m/^[[:digit:]]+$/; + + $happy = 1; + }; + + return $happy; +} + +ajax '/ajax/content/admin/pseudodevice/add' => sub { + forward '/ajax/content/admin/pseudodevice' + unless _sanity_ok(); + + try { + schema('netdisco')->txn_do(sub { + my $device = schema('netdisco')->resultset('Device') + ->create({ + ip => param('ip'), + dns => param('dns'), + vendor => 'netdisco', + last_discover => \'now()', + }); + + $device->ports->populate([ + ['port'], + map {["Port$_"]} @{[1 .. param('ports')]}, + ]); + }); + }; + + forward '/ajax/content/admin/pseudodevice'; +}; + ajax '/ajax/content/admin/pseudodevice' => sub { my $set = schema('netdisco')->resultset('Device') ->search( {vendor => 'netdisco'}, {order_by => { -desc => 'last_discover' }}, - ); + )->with_port_count; content_type('text/html'); template 'ajax/admintask/pseudodevice.tt', { diff --git a/Netdisco/share/public/css/netdisco.css b/Netdisco/share/public/css/netdisco.css index d380136f..f16caa6e 100644 --- a/Netdisco/share/public/css/netdisco.css +++ b/Netdisco/share/public/css/netdisco.css @@ -196,6 +196,10 @@ td { margin-bottom: 2px; } +.nd_inline_form { + display: inline; +} + /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /* tabs */ diff --git a/Netdisco/share/views/ajax/admintask/pseudodevice.tt b/Netdisco/share/views/ajax/admintask/pseudodevice.tt index 98e73c98..a0be74e2 100644 --- a/Netdisco/share/views/ajax/admintask/pseudodevice.tt +++ b/Netdisco/share/views/ajax/admintask/pseudodevice.tt @@ -20,11 +20,18 @@ [% WHILE (row = results.next) %] +
[% row.dns | html_entity %] [% row.ip | html_entity %] - [% row.ports.count | html_entity %] - Add + + + +
+
+ +
+ [% END %]