1package App::Netdisco::Web::Plugin::AdminTask::NodeMonitor;
2
3use Dancer ':syntax';
4use Dancer::Plugin::Ajax;
5use Dancer::Plugin::DBIC;
6use Dancer::Plugin::Auth::Extensible;
7
8use App::Netdisco::Web::Plugin;
9use App::Netdisco::Util::Node 'check_mac';
10
11register_admin_task({
12  tag => 'nodemonitor',
13  label => 'Node Monitor',
14});
15
16sub _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
24ajax '/ajax/control/admin/nodemonitor/add' => require_role admin => sub {
25    send_error('Bad Request', 400) unless _sanity_ok();
26
27    schema('netdisco')->txn_do(sub {
28      my $monitor = schema('netdisco')->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
39ajax '/ajax/control/admin/nodemonitor/del' => require_role admin => sub {
40    send_error('Bad Request', 400) unless _sanity_ok();
41
42    schema('netdisco')->txn_do(sub {
43      schema('netdisco')->resultset('NodeMonitor')
44        ->find({mac => param('mac')})->delete;
45    });
46};
47
48ajax '/ajax/control/admin/nodemonitor/update' => require_role admin => sub {
49    send_error('Bad Request', 400) unless _sanity_ok();
50
51    schema('netdisco')->txn_do(sub {
52      my $monitor = schema('netdisco')->resultset('NodeMonitor')
53        ->find({mac => param('mac')});
54      return unless $monitor;
55
56      $monitor->update({
57        mac => param('mac'),
58        matchoui => (param('matchoui') ? \'true' : \'false'),
59        active => (param('active') ? \'true' : \'false'),
60        why => param('why'),
61        cc => param('cc'),
62        date => \'now()',
63      });
64    });
65};
66
67ajax '/ajax/content/admin/nodemonitor' => require_role admin => sub {
68    my $set = schema('netdisco')->resultset('NodeMonitor')
69      ->search(undef, { order_by => [qw/active date mac/] });
70
71    content_type('text/html');
72    template 'ajax/admintask/nodemonitor.tt', {
73      results => $set,
74    }, { layout => undef };
75};
76
77true;
78