Files
netdisco/lib/App/Netdisco/Web/PortControl.pm
Oliver Gorwits 1c7c749f0e custom fields on devices and ports in the web from config (#945)
* custom device field web display and edit

* make display work; relies on T::T calling dict slot or method with same syntax

* add storing port custom fields

* use resultset method instead, use cf_ prefix

* update Pg min ver for jsonb

* allow override of position and default for port custom fields

* support hidden for custom fields

* update description of Objects API class

* allow left and mid position for custom fields

* add custom fields in csv

* change port control sidebar label

* fix default missing bug on backend jobs
2022-12-09 10:20:26 +00:00

90 lines
2.4 KiB
Perl

package App::Netdisco::Web::PortControl;
use Dancer ':syntax';
use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::JobQueue qw/jq_insert jq_userlog/;
ajax '/ajax/portcontrol' => require_any_role [qw(admin port_control)] => sub {
send_error('No device/port/field', 400)
unless param('device') and (param('port') or param('field'));
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_pvid' => 'vlan',
'c_power' => 'power',
);
my $action = ($action_map{ param('field') } || param('field') || '');
my $subaction = ($action =~ m/^(?:power|portcontrol)/
? (param('action') ."-other")
: param('value'));
schema(vars->{'tenant'})->txn_do(sub {
if (param('port')) {
my $act = "$action $subaction";
$act =~ s/-other$//;
$act =~ s/^portcontrol/port/;
$act =~ s/^device_port_custom_field_/custom_field: /;
schema(vars->{'tenant'})->resultset('DevicePortLog')->create({
ip => param('device'),
port => param('port'),
action => $act,
username => session('logged_in_user'),
userip => request->remote_address,
reason => (param('reason') || 'other'),
log => param('log'),
});
}
jq_insert({
device => param('device'),
port => param('port'),
action => $action,
subaction => $subaction,
username => session('logged_in_user'),
userip => request->remote_address,
log => $log,
});
});
content_type('application/json');
to_json({});
};
ajax '/ajax/userlog' => require_login sub {
my @jobs = jq_userlog( session('logged_in_user') );
my %status = (
'done' => [
map {s/\[\]/<empty>/; $_}
map { $_->log }
grep { $_->status eq 'done' }
grep { defined }
@jobs
],
'error' => [
map {s/\[\]/<empty>/; $_}
map { $_->log }
grep { $_->status eq 'error' }
grep { defined }
@jobs
],
);
content_type('application/json');
to_json(\%status);
};
true;