remove Try::Tiny from web runtime
This commit is contained in:
@@ -6,7 +6,6 @@ use Dancer::Plugin::DBIC;
|
|||||||
|
|
||||||
use App::Netdisco::Web::Plugin;
|
use App::Netdisco::Web::Plugin;
|
||||||
use NetAddr::IP::Lite ':lower';
|
use NetAddr::IP::Lite ':lower';
|
||||||
use Try::Tiny;
|
|
||||||
|
|
||||||
register_admin_task({
|
register_admin_task({
|
||||||
tag => 'pseudodevice',
|
tag => 'pseudodevice',
|
||||||
@@ -14,47 +13,40 @@ register_admin_task({
|
|||||||
});
|
});
|
||||||
|
|
||||||
sub _sanity_ok {
|
sub _sanity_ok {
|
||||||
my $happy = 0;
|
return 0 unless var('user')->admin;
|
||||||
|
|
||||||
try {
|
return 0 unless length param('dns')
|
||||||
return 0 unless var('user')->admin;
|
and param('dns') =~ m/^[[:print:]]+$/
|
||||||
|
and param('dns') !~ m/[[:space:]]/;
|
||||||
|
|
||||||
return 0 unless length param('dns')
|
my $ip = NetAddr::IP::Lite->new(param('ip'));
|
||||||
and param('dns') =~ m/^[[:print:]]+$/
|
return 0 unless ($ip and$ip->addr ne '0.0.0.0');
|
||||||
and param('dns') !~ m/[[:space:]]/;
|
|
||||||
|
|
||||||
my $ip = NetAddr::IP::Lite->new(param('ip'));
|
return 0 unless length param('ports')
|
||||||
return 0 if $ip->addr eq '0.0.0.0';
|
and param('ports') =~ m/^[[:digit:]]+$/;
|
||||||
|
|
||||||
return 0 unless length param('ports')
|
return 1;
|
||||||
and param('ports') =~ m/^[[:digit:]]+$/;
|
|
||||||
|
|
||||||
$happy = 1;
|
|
||||||
};
|
|
||||||
|
|
||||||
return $happy;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ajax '/ajax/content/admin/pseudodevice/add' => sub {
|
ajax '/ajax/content/admin/pseudodevice/add' => sub {
|
||||||
forward '/ajax/content/admin/pseudodevice'
|
forward '/ajax/content/admin/pseudodevice'
|
||||||
unless _sanity_ok();
|
unless _sanity_ok();
|
||||||
|
|
||||||
try {
|
schema('netdisco')->txn_do(sub {
|
||||||
schema('netdisco')->txn_do(sub {
|
my $device = schema('netdisco')->resultset('Device')
|
||||||
my $device = schema('netdisco')->resultset('Device')
|
->create({
|
||||||
->create({
|
ip => param('ip'),
|
||||||
ip => param('ip'),
|
dns => param('dns'),
|
||||||
dns => param('dns'),
|
vendor => 'netdisco',
|
||||||
vendor => 'netdisco',
|
last_discover => \'now()',
|
||||||
last_discover => \'now()',
|
|
||||||
});
|
|
||||||
|
|
||||||
$device->ports->populate([
|
|
||||||
['port'],
|
|
||||||
map {["Port$_"]} @{[1 .. param('ports')]},
|
|
||||||
]);
|
|
||||||
});
|
});
|
||||||
};
|
return unless $device;
|
||||||
|
|
||||||
|
$device->ports->populate([
|
||||||
|
['port'],
|
||||||
|
map {["Port$_"]} @{[1 .. param('ports')]},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
forward '/ajax/content/admin/pseudodevice';
|
forward '/ajax/content/admin/pseudodevice';
|
||||||
};
|
};
|
||||||
@@ -63,15 +55,13 @@ ajax '/ajax/content/admin/pseudodevice/del' => sub {
|
|||||||
forward '/ajax/content/admin/pseudodevice'
|
forward '/ajax/content/admin/pseudodevice'
|
||||||
unless _sanity_ok();
|
unless _sanity_ok();
|
||||||
|
|
||||||
try {
|
schema('netdisco')->txn_do(sub {
|
||||||
schema('netdisco')->txn_do(sub {
|
my $device = schema('netdisco')->resultset('Device')
|
||||||
my $device = schema('netdisco')->resultset('Device')
|
->find({ip => param('ip')});
|
||||||
->find({ip => param('ip')});
|
|
||||||
|
|
||||||
$device->ports->delete;
|
$device->ports->delete;
|
||||||
$device->delete;
|
$device->delete;
|
||||||
});
|
});
|
||||||
};
|
|
||||||
|
|
||||||
forward '/ajax/content/admin/pseudodevice';
|
forward '/ajax/content/admin/pseudodevice';
|
||||||
};
|
};
|
||||||
@@ -80,35 +70,26 @@ ajax '/ajax/content/admin/pseudodevice/update' => sub {
|
|||||||
forward '/ajax/content/admin/pseudodevice'
|
forward '/ajax/content/admin/pseudodevice'
|
||||||
unless _sanity_ok();
|
unless _sanity_ok();
|
||||||
|
|
||||||
try {
|
schema('netdisco')->txn_do(sub {
|
||||||
schema('netdisco')->txn_do(sub {
|
my $device = schema('netdisco')->resultset('Device')
|
||||||
my $device = schema('netdisco')->resultset('Device')
|
->with_port_count->find({ip => param('ip')});
|
||||||
->with_port_count->find({ip => param('ip')});
|
return unless $device;
|
||||||
my $count = $device->port_count;
|
my $count = $device->port_count;
|
||||||
|
|
||||||
if (param('ports') > $count) {
|
if (param('ports') > $count) {
|
||||||
my $start = $count + 1;
|
my $start = $count + 1;
|
||||||
try {
|
$device->ports->populate([
|
||||||
schema('netdisco')->txn_do(sub {
|
['port'],
|
||||||
$device->ports->populate([
|
map {["Port$_"]} @{[$start .. param('ports')]},
|
||||||
['port'],
|
]);
|
||||||
map {["Port$_"]} @{[$start .. param('ports')]},
|
}
|
||||||
]);
|
elsif (param('ports') < $count) {
|
||||||
});
|
my $start = param('ports') + 1;
|
||||||
};
|
$device->ports
|
||||||
}
|
->single({port => "Port$_"})->delete
|
||||||
elsif (param('ports') < $count) {
|
for ($start .. $count);
|
||||||
my $start = param('ports') + 1;
|
}
|
||||||
try {
|
});
|
||||||
schema('netdisco')->txn_do(sub {
|
|
||||||
$device->ports
|
|
||||||
->single({port => "Port$_"})->delete
|
|
||||||
for ($start .. $count);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
forward '/ajax/content/admin/pseudodevice';
|
forward '/ajax/content/admin/pseudodevice';
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ use Dancer::Plugin::DBIC;
|
|||||||
|
|
||||||
use App::Netdisco::Web::Plugin;
|
use App::Netdisco::Web::Plugin;
|
||||||
use NetAddr::IP::Lite ':lower';
|
use NetAddr::IP::Lite ':lower';
|
||||||
use Try::Tiny;
|
|
||||||
|
|
||||||
register_admin_task({
|
register_admin_task({
|
||||||
tag => 'topology',
|
tag => 'topology',
|
||||||
@@ -14,39 +13,31 @@ register_admin_task({
|
|||||||
});
|
});
|
||||||
|
|
||||||
sub _sanity_ok {
|
sub _sanity_ok {
|
||||||
my $happy = 0;
|
return 0 unless var('user')->admin;
|
||||||
|
|
||||||
try {
|
my $dev1 = NetAddr::IP::Lite->new(param('dev1'));
|
||||||
return 0 unless var('user')->admin;
|
return 0 unless ($dev1 and $dev1->addr ne '0.0.0.0');
|
||||||
|
|
||||||
my $dev1 = NetAddr::IP::Lite->new(param('dev1'));
|
my $dev2 = NetAddr::IP::Lite->new(param('dev2'));
|
||||||
return 0 if $dev1->addr eq '0.0.0.0';
|
return 0 unless ($dev2 and $dev2->addr ne '0.0.0.0');
|
||||||
|
|
||||||
my $dev2 = NetAddr::IP::Lite->new(param('dev2'));
|
return 0 unless length param('port1');
|
||||||
return 0 if $dev2->addr eq '0.0.0.0';
|
return 0 unless length param('port2');
|
||||||
|
|
||||||
return 0 unless length param('port1');
|
return 1;
|
||||||
return 0 unless length param('port2');
|
|
||||||
|
|
||||||
$happy = 1;
|
|
||||||
};
|
|
||||||
|
|
||||||
return $happy;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ajax '/ajax/content/admin/topology/add' => sub {
|
ajax '/ajax/content/admin/topology/add' => sub {
|
||||||
forward '/ajax/content/admin/topology'
|
forward '/ajax/content/admin/topology'
|
||||||
unless _sanity_ok();
|
unless _sanity_ok();
|
||||||
|
|
||||||
try {
|
my $device = schema('netdisco')->resultset('Topology')
|
||||||
my $device = schema('netdisco')->resultset('Topology')
|
->create({
|
||||||
->create({
|
dev1 => param('dev1'),
|
||||||
dev1 => param('dev1'),
|
port1 => param('port1'),
|
||||||
port1 => param('port1'),
|
dev2 => param('dev2'),
|
||||||
dev2 => param('dev2'),
|
port2 => param('port2'),
|
||||||
port2 => param('port2'),
|
});
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
forward '/ajax/content/admin/topology';
|
forward '/ajax/content/admin/topology';
|
||||||
};
|
};
|
||||||
@@ -55,17 +46,15 @@ ajax '/ajax/content/admin/topology/del' => sub {
|
|||||||
forward '/ajax/content/admin/topology'
|
forward '/ajax/content/admin/topology'
|
||||||
unless _sanity_ok();
|
unless _sanity_ok();
|
||||||
|
|
||||||
try {
|
schema('netdisco')->txn_do(sub {
|
||||||
schema('netdisco')->txn_do(sub {
|
my $device = schema('netdisco')->resultset('Topology')
|
||||||
my $device = schema('netdisco')->resultset('Topology')
|
->search({
|
||||||
->search({
|
dev1 => param('dev1'),
|
||||||
dev1 => param('dev1'),
|
port1 => param('port1'),
|
||||||
port1 => param('port1'),
|
dev2 => param('dev2'),
|
||||||
dev2 => param('dev2'),
|
port2 => param('port2'),
|
||||||
port2 => param('port2'),
|
})->delete;
|
||||||
})->delete;
|
});
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
forward '/ajax/content/admin/topology';
|
forward '/ajax/content/admin/topology';
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,44 +4,40 @@ use Dancer ':syntax';
|
|||||||
use Dancer::Plugin::Ajax;
|
use Dancer::Plugin::Ajax;
|
||||||
use Dancer::Plugin::DBIC;
|
use Dancer::Plugin::DBIC;
|
||||||
|
|
||||||
use Try::Tiny;
|
|
||||||
|
|
||||||
ajax '/ajax/portcontrol' => sub {
|
ajax '/ajax/portcontrol' => sub {
|
||||||
return unless var('user')->port_control;
|
return unless var('user')->port_control;
|
||||||
|
return unless param('device') and param('port') and param('field');
|
||||||
|
|
||||||
try {
|
my $log = sprintf 'd:[%s] p:[%s] f:[%s]. a:[%s] v[%s]',
|
||||||
my $log = sprintf 'd:[%s] p:[%s] f:[%s]. a:[%s] v[%s]',
|
param('device'), (param('port') || ''), param('field'),
|
||||||
param('device'), (param('port') || ''), param('field'),
|
(param('action') || ''), (param('value') || '');
|
||||||
(param('action') || ''), (param('value') || '');
|
|
||||||
|
|
||||||
my %action_map = (
|
my %action_map = (
|
||||||
'location' => 'location',
|
'location' => 'location',
|
||||||
'contact' => 'contact',
|
'contact' => 'contact',
|
||||||
'c_port' => 'portcontrol',
|
'c_port' => 'portcontrol',
|
||||||
'c_name' => 'portname',
|
'c_name' => 'portname',
|
||||||
'c_vlan' => 'vlan',
|
'c_vlan' => 'vlan',
|
||||||
'c_power' => 'power',
|
'c_power' => 'power',
|
||||||
);
|
);
|
||||||
|
|
||||||
my $action = $action_map{ param('field') };
|
return unless (param('action') or param('value'));
|
||||||
my $subaction = ($action =~ m/^(?:power|portcontrol)/
|
|
||||||
? (param('action') ."-other")
|
|
||||||
: param('value'));
|
|
||||||
|
|
||||||
schema('netdisco')->resultset('Admin')->create({
|
my $action = $action_map{ param('field') };
|
||||||
device => param('device'),
|
my $subaction = ($action =~ m/^(?:power|portcontrol)/
|
||||||
port => param('port'),
|
? (param('action') ."-other")
|
||||||
action => $action,
|
: param('value'));
|
||||||
subaction => $subaction,
|
|
||||||
status => 'queued',
|
schema('netdisco')->resultset('Admin')->create({
|
||||||
username => session('user'),
|
device => param('device'),
|
||||||
userip => request->remote_address,
|
port => param('port'),
|
||||||
log => $log,
|
action => $action,
|
||||||
});
|
subaction => $subaction,
|
||||||
}
|
status => 'queued',
|
||||||
catch {
|
username => session('user'),
|
||||||
send_error('Failed to parse params or add DB record');
|
userip => request->remote_address,
|
||||||
};
|
log => $log,
|
||||||
|
});
|
||||||
|
|
||||||
content_type('application/json');
|
content_type('application/json');
|
||||||
to_json({});
|
to_json({});
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ use Dancer::Plugin::Ajax;
|
|||||||
use Dancer::Plugin::DBIC;
|
use Dancer::Plugin::DBIC;
|
||||||
|
|
||||||
use App::Netdisco::Util::Web (); # for sort_port
|
use App::Netdisco::Util::Web (); # for sort_port
|
||||||
use Try::Tiny;
|
|
||||||
|
|
||||||
ajax '/ajax/data/devicename/typeahead' => sub {
|
ajax '/ajax/data/devicename/typeahead' => sub {
|
||||||
my $q = param('query') || param('term');
|
my $q = param('query') || param('term');
|
||||||
@@ -38,14 +37,13 @@ ajax '/ajax/data/port/typeahead' => sub {
|
|||||||
my $port = param('port1') || param('port2');
|
my $port = param('port1') || param('port2');
|
||||||
return unless length $dev;
|
return unless length $dev;
|
||||||
|
|
||||||
my $set = undef;
|
my $device = schema('netdisco')->resultset('Device')
|
||||||
try {
|
->find({ip => $dev});
|
||||||
$set = schema('netdisco')->resultset('Device')
|
return unless $device;
|
||||||
->find({ip => $dev})->ports({},{order_by => 'port'});
|
|
||||||
$set = $set->search({port => { -ilike => "\%$port\%" }})
|
my $set = $device->ports({},{order_by => 'port'});
|
||||||
if length $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 ];
|
my $results = [ sort { &App::Netdisco::Util::Web::sort_port($a->port, $b->port) } $set->all ];
|
||||||
return unless scalar @$results;
|
return unless scalar @$results;
|
||||||
|
|||||||
Reference in New Issue
Block a user