Manual Device Topology

Needed to add the 'autocomplete' jQuery UI component because
it can do minLength=0 properly. Used the smoothness UI theme.

Added typeahead AJAX calls to support the topology searching.

Added new plugin and template for the topology editing page.
This commit is contained in:
Oliver Gorwits
2013-05-07 00:56:19 +01:00
parent bf7a419d08
commit 11d55e0129
25 changed files with 231 additions and 9 deletions

View File

@@ -10,7 +10,7 @@ use Try::Tiny;
register_admin_task({
tag => 'pseudodevice',
label => 'Manage Pseudo Devices',
label => 'Pseudo Devices',
});
sub _sanity_ok {

View File

@@ -0,0 +1,90 @@
package App::Netdisco::Web::Plugin::AdminTask::Topology;
use Dancer ':syntax';
use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC;
use App::Netdisco::Web::Plugin;
use Try::Tiny;
register_admin_task({
tag => 'topology',
label => 'Manual Device Topology',
});
sub _sanity_ok {
my $happy = 0;
try {
return 0 unless var('user')->admin;
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/topology/add' => sub {
forward '/ajax/content/admin/topology'
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/topology';
};
ajax '/ajax/content/admin/topology/del' => sub {
forward '/ajax/content/admin/topology'
unless _sanity_ok();
try {
schema('netdisco')->txn_do(sub {
my $device = schema('netdisco')->resultset('Device')
->find({ip => param('ip')});
$device->ports->delete;
$device->delete;
});
};
forward '/ajax/content/admin/topology';
};
ajax '/ajax/content/admin/topology' => sub {
return unless var('user')->admin;
my $set = schema('netdisco')->resultset('Topology')
->search({},{order_by => [qw/dev1 dev2 port1/]});
content_type('text/html');
template 'ajax/admintask/topology.tt', {
results => $set,
}, { layout => undef };
};
true;

View File

@@ -4,13 +4,46 @@ use Dancer ':syntax';
use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC;
# support typeahead with simple AJAX query for device names
ajax '/ajax/data/device/typeahead' => sub {
my $q = param('query');
use App::Netdisco::Util::Web (); # for sort_port
use Try::Tiny;
ajax '/ajax/data/devicename/typeahead' => sub {
my $q = param('query') || param('term');
my $set = schema('netdisco')->resultset('Device')->search_fuzzy($q);
content_type 'application/json';
return to_json [map {$_->dns || $_->name || $_->ip} $set->all];
};
ajax '/ajax/data/deviceip/typeahead' => sub {
my $q = param('query') || param('term');
my $set = schema('netdisco')->resultset('Device')->search_fuzzy($q);
content_type 'application/json';
return to_json [map {
{label => ($_->dns || $_->name || $_->ip), value => $_->ip}
} $set->all];
};
ajax '/ajax/data/port/typeahead' => sub {
my $dev = param('dev1') || param('dev2');
my $port = param('port1') || param('port2');
return unless length $dev;
my $set = undef;
try {
$set = schema('netdisco')->resultset('Device')
->find({ip => $dev})->ports({},{order_by => 'port'});
$set = $set->search({port => { -ilike => "\%$port\%" }})
if length $port;
};
return unless defined $set;
my $results = [ sort { &App::Netdisco::Util::Web::sort_port($a->port, $b->port) } $set->all ];
return unless scalar @$results;
content_type 'application/json';
return to_json [map {$_->port} @$results];
};
true;