remove Try::Tiny from web runtime

This commit is contained in:
Oliver Gorwits
2013-05-08 23:06:41 +01:00
parent c746e68b9b
commit e94e3cef3b
4 changed files with 107 additions and 143 deletions

View File

@@ -6,7 +6,6 @@ use Dancer::Plugin::DBIC;
use App::Netdisco::Web::Plugin;
use NetAddr::IP::Lite ':lower';
use Try::Tiny;
register_admin_task({
tag => 'pseudodevice',
@@ -14,47 +13,40 @@ register_admin_task({
});
sub _sanity_ok {
my $happy = 0;
return 0 unless var('user')->admin;
try {
return 0 unless var('user')->admin;
return 0 unless length param('dns')
and param('dns') =~ m/^[[:print:]]+$/
and param('dns') !~ m/[[:space:]]/;
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 unless ($ip and$ip->addr ne '0.0.0.0');
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:]]+$/;
return 0 unless length param('ports')
and param('ports') =~ m/^[[:digit:]]+$/;
$happy = 1;
};
return $happy;
return 1;
}
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')]},
]);
schema('netdisco')->txn_do(sub {
my $device = schema('netdisco')->resultset('Device')
->create({
ip => param('ip'),
dns => param('dns'),
vendor => 'netdisco',
last_discover => \'now()',
});
};
return unless $device;
$device->ports->populate([
['port'],
map {["Port$_"]} @{[1 .. param('ports')]},
]);
});
forward '/ajax/content/admin/pseudodevice';
};
@@ -63,15 +55,13 @@ ajax '/ajax/content/admin/pseudodevice/del' => sub {
forward '/ajax/content/admin/pseudodevice'
unless _sanity_ok();
try {
schema('netdisco')->txn_do(sub {
my $device = schema('netdisco')->resultset('Device')
->find({ip => param('ip')});
schema('netdisco')->txn_do(sub {
my $device = schema('netdisco')->resultset('Device')
->find({ip => param('ip')});
$device->ports->delete;
$device->delete;
});
};
$device->ports->delete;
$device->delete;
});
forward '/ajax/content/admin/pseudodevice';
};
@@ -80,35 +70,26 @@ ajax '/ajax/content/admin/pseudodevice/update' => sub {
forward '/ajax/content/admin/pseudodevice'
unless _sanity_ok();
try {
schema('netdisco')->txn_do(sub {
my $device = schema('netdisco')->resultset('Device')
->with_port_count->find({ip => param('ip')});
my $count = $device->port_count;
schema('netdisco')->txn_do(sub {
my $device = schema('netdisco')->resultset('Device')
->with_port_count->find({ip => param('ip')});
return unless $device;
my $count = $device->port_count;
if (param('ports') > $count) {
my $start = $count + 1;
try {
schema('netdisco')->txn_do(sub {
$device->ports->populate([
['port'],
map {["Port$_"]} @{[$start .. param('ports')]},
]);
});
};
}
elsif (param('ports') < $count) {
my $start = param('ports') + 1;
try {
schema('netdisco')->txn_do(sub {
$device->ports
->single({port => "Port$_"})->delete
for ($start .. $count);
});
};
}
});
};
if (param('ports') > $count) {
my $start = $count + 1;
$device->ports->populate([
['port'],
map {["Port$_"]} @{[$start .. param('ports')]},
]);
}
elsif (param('ports') < $count) {
my $start = param('ports') + 1;
$device->ports
->single({port => "Port$_"})->delete
for ($start .. $count);
}
});
forward '/ajax/content/admin/pseudodevice';
};

View File

@@ -6,7 +6,6 @@ use Dancer::Plugin::DBIC;
use App::Netdisco::Web::Plugin;
use NetAddr::IP::Lite ':lower';
use Try::Tiny;
register_admin_task({
tag => 'topology',
@@ -14,39 +13,31 @@ register_admin_task({
});
sub _sanity_ok {
my $happy = 0;
return 0 unless var('user')->admin;
try {
return 0 unless var('user')->admin;
my $dev1 = NetAddr::IP::Lite->new(param('dev1'));
return 0 unless ($dev1 and $dev1->addr ne '0.0.0.0');
my $dev1 = NetAddr::IP::Lite->new(param('dev1'));
return 0 if $dev1->addr eq '0.0.0.0';
my $dev2 = NetAddr::IP::Lite->new(param('dev2'));
return 0 unless ($dev2 and $dev2->addr ne '0.0.0.0');
my $dev2 = NetAddr::IP::Lite->new(param('dev2'));
return 0 if $dev2->addr eq '0.0.0.0';
return 0 unless length param('port1');
return 0 unless length param('port2');
return 0 unless length param('port1');
return 0 unless length param('port2');
$happy = 1;
};
return $happy;
return 1;
}
ajax '/ajax/content/admin/topology/add' => sub {
forward '/ajax/content/admin/topology'
unless _sanity_ok();
try {
my $device = schema('netdisco')->resultset('Topology')
->create({
dev1 => param('dev1'),
port1 => param('port1'),
dev2 => param('dev2'),
port2 => param('port2'),
});
};
my $device = schema('netdisco')->resultset('Topology')
->create({
dev1 => param('dev1'),
port1 => param('port1'),
dev2 => param('dev2'),
port2 => param('port2'),
});
forward '/ajax/content/admin/topology';
};
@@ -55,17 +46,15 @@ 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('Topology')
->search({
dev1 => param('dev1'),
port1 => param('port1'),
dev2 => param('dev2'),
port2 => param('port2'),
})->delete;
});
};
schema('netdisco')->txn_do(sub {
my $device = schema('netdisco')->resultset('Topology')
->search({
dev1 => param('dev1'),
port1 => param('port1'),
dev2 => param('dev2'),
port2 => param('port2'),
})->delete;
});
forward '/ajax/content/admin/topology';
};

View File

@@ -4,44 +4,40 @@ use Dancer ':syntax';
use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC;
use Try::Tiny;
ajax '/ajax/portcontrol' => sub {
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]',
param('device'), (param('port') || ''), param('field'),
(param('action') || ''), (param('value') || '');
my $log = sprintf 'd:[%s] p:[%s] f:[%s]. a:[%s] v[%s]',
param('device'), (param('port') || ''), param('field'),
(param('action') || ''), (param('value') || '');
my %action_map = (
'location' => 'location',
'contact' => 'contact',
'c_port' => 'portcontrol',
'c_name' => 'portname',
'c_vlan' => 'vlan',
'c_power' => 'power',
);
my %action_map = (
'location' => 'location',
'contact' => 'contact',
'c_port' => 'portcontrol',
'c_name' => 'portname',
'c_vlan' => 'vlan',
'c_power' => 'power',
);
my $action = $action_map{ param('field') };
my $subaction = ($action =~ m/^(?:power|portcontrol)/
? (param('action') ."-other")
: param('value'));
return unless (param('action') or param('value'));
schema('netdisco')->resultset('Admin')->create({
device => param('device'),
port => param('port'),
action => $action,
subaction => $subaction,
status => 'queued',
username => session('user'),
userip => request->remote_address,
log => $log,
});
}
catch {
send_error('Failed to parse params or add DB record');
};
my $action = $action_map{ param('field') };
my $subaction = ($action =~ m/^(?:power|portcontrol)/
? (param('action') ."-other")
: param('value'));
schema('netdisco')->resultset('Admin')->create({
device => param('device'),
port => param('port'),
action => $action,
subaction => $subaction,
status => 'queued',
username => session('user'),
userip => request->remote_address,
log => $log,
});
content_type('application/json');
to_json({});

View File

@@ -5,7 +5,6 @@ use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC;
use App::Netdisco::Util::Web (); # for sort_port
use Try::Tiny;
ajax '/ajax/data/devicename/typeahead' => sub {
my $q = param('query') || param('term');
@@ -38,14 +37,13 @@ ajax '/ajax/data/port/typeahead' => sub {
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 $device = schema('netdisco')->resultset('Device')
->find({ip => $dev});
return unless $device;
my $set = $device->ports({},{order_by => 'port'});
$set = $set->search({port => { -ilike => "\%$port\%" }})
if length $port;
my $results = [ sort { &App::Netdisco::Util::Web::sort_port($a->port, $b->port) } $set->all ];
return unless scalar @$results;