« blib/lib/App/Netdisco/Web/Password.pm
↑ Summary
blib/lib/App/Netdisco/Worker/Plugin/Hook/Exec.pm »
| 1 | package App::Netdisco::Web::Plugin::AdminTask::NodeMonitor; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use Dancer::Plugin::Ajax; | ||
| 5 | use Dancer::Plugin::DBIC; | ||
| 6 | use Dancer::Plugin::Auth::Extensible; | ||
| 7 | |||
| 8 | use App::Netdisco::Web::Plugin; | ||
| 9 | use App::Netdisco::Util::Node 'check_mac'; | ||
| 10 | |||
| 11 | register_admin_task({ | ||
| 12 | tag => 'nodemonitor', | ||
| 13 | label => 'Node Monitor', | ||
| 14 | }); | ||
| 15 | |||
| 16 | sub _sanity_ok { | ||
| 17 | return 0 unless param('mac') | ||
| 18 | and check_mac(param('mac')); | ||
| 19 | |||
| 20 | params->{mac} = check_mac(param('mac')); | ||
| 21 | return 1; | ||
| 22 | } | ||
| 23 | |||
| 24 | ajax '/ajax/control/admin/nodemonitor/add' => require_role admin => sub { | ||
| 25 | send_error('Bad Request', 400) unless _sanity_ok(); | ||
| 26 | |||
| 27 | schema(vars->{'tenant'})->txn_do(sub { | ||
| 28 | my $monitor = schema(vars->{'tenant'})->resultset('NodeMonitor') | ||
| 29 | ->create({ | ||
| 30 | mac => param('mac'), | ||
| 31 | matchoui => (param('matchoui') ? \'true' : \'false'), | ||
| 32 | active => (param('active') ? \'true' : \'false'), | ||
| 33 | why => param('why'), | ||
| 34 | cc => param('cc'), | ||
| 35 | }); | ||
| 36 | }); | ||
| 37 | |||
| 38 | return ''; | ||
| 39 | }; | ||
| 40 | |||
| 41 | ajax '/ajax/control/admin/nodemonitor/del' => require_role admin => sub { | ||
| 42 | send_error('Bad Request', 400) unless _sanity_ok(); | ||
| 43 | |||
| 44 | schema(vars->{'tenant'})->txn_do(sub { | ||
| 45 | schema(vars->{'tenant'})->resultset('NodeMonitor') | ||
| 46 | ->find({mac => param('mac')})->delete; | ||
| 47 | }); | ||
| 48 | |||
| 49 | return ''; | ||
| 50 | }; | ||
| 51 | |||
| 52 | ajax '/ajax/control/admin/nodemonitor/update' => require_role admin => sub { | ||
| 53 | send_error('Bad Request', 400) unless _sanity_ok(); | ||
| 54 | |||
| 55 | schema(vars->{'tenant'})->txn_do(sub { | ||
| 56 | my $monitor = schema(vars->{'tenant'})->resultset('NodeMonitor') | ||
| 57 | ->find({mac => param('mac')}); | ||
| 58 | return unless $monitor; | ||
| 59 | |||
| 60 | $monitor->update({ | ||
| 61 | mac => param('mac'), | ||
| 62 | matchoui => (param('matchoui') ? \'true' : \'false'), | ||
| 63 | active => (param('active') ? \'true' : \'false'), | ||
| 64 | why => param('why'), | ||
| 65 | cc => param('cc'), | ||
| 66 | date => \'LOCALTIMESTAMP', | ||
| 67 | }); | ||
| 68 | }); | ||
| 69 | |||
| 70 | return ''; | ||
| 71 | }; | ||
| 72 | |||
| 73 | ajax '/ajax/content/admin/nodemonitor' => require_role admin => sub { | ||
| 74 | my $set = schema(vars->{'tenant'})->resultset('NodeMonitor') | ||
| 75 | ->search(undef, { order_by => [qw/active date mac/] }); | ||
| 76 | |||
| 77 | content_type('text/html'); | ||
| 78 | template 'ajax/admintask/nodemonitor.tt', { | ||
| 79 | results => $set, | ||
| 80 | }, { layout => undef }; | ||
| 81 | }; | ||
| 82 | |||
| 83 | true; |