Implement Hooks per #726
This commit is contained in:
32
lib/App/Netdisco/Worker/Plugin/Discover/Hooks.pm
Normal file
32
lib/App/Netdisco/Worker/Plugin/Discover/Hooks.pm
Normal file
@@ -0,0 +1,32 @@
|
||||
package App::Netdisco::Worker::Plugin::Discover::Hooks;
|
||||
|
||||
use Dancer ':syntax';
|
||||
use App::Netdisco::Worker::Plugin;
|
||||
use aliased 'App::Netdisco::Worker::Status';
|
||||
|
||||
use App::Netdisco::Util::Worker;
|
||||
use App::Netdisco::Util::Permission qw/check_acl_no check_acl_only/;
|
||||
|
||||
register_worker({ phase => 'late' }, sub {
|
||||
my ($job, $workerconf) = @_;
|
||||
my $count = 0;
|
||||
|
||||
foreach my $conf (@{ setting('hooks') }) {
|
||||
my $no = ($conf->{'filter'}->{'no'} || []);
|
||||
my $only = ($conf->{'filter'}->{'only'} || []);
|
||||
|
||||
next if check_acl_no( $job->device, $no );
|
||||
next unless check_acl_only( $job->device, $only);
|
||||
|
||||
$count += queue_hook('new_device', $conf)
|
||||
if vars->{'new_device'} and $conf->{'event'} eq 'new_device';
|
||||
|
||||
$count += queue_hook('discover', $conf)
|
||||
if $conf->{'event'} eq 'discover';
|
||||
}
|
||||
|
||||
return Status
|
||||
->info(sprintf ' [%s] hooks - %d queued', $job->device, $count);
|
||||
});
|
||||
|
||||
true;
|
||||
@@ -71,6 +71,13 @@ register_worker({ phase => 'early', driver => 'snmp' }, sub {
|
||||
}
|
||||
}
|
||||
|
||||
# support for Hooks
|
||||
vars->{'hook_data'} = { $device->get_columns };
|
||||
delete vars->{'hook_data'}->{'snmp_comm'}; # for privacy
|
||||
|
||||
# support for new_device Hook
|
||||
vars->{'new_device'} = 1 if not $device->in_storage;
|
||||
|
||||
schema('netdisco')->txn_do(sub {
|
||||
$device->update_or_insert(undef, {for => 'update'});
|
||||
return Status->done("Ended discover for $device");
|
||||
@@ -149,6 +156,9 @@ register_worker({ phase => 'early', driver => 'snmp' }, sub {
|
||||
push @$resolved_aliases, { alias => $device->ip, dns => $device->dns }
|
||||
if 0 == scalar grep {$_->{alias} eq $device->ip} @aliases;
|
||||
|
||||
# support for Hooks
|
||||
vars->{'hook_data'}->{'device_ips'} = $resolved_aliases;
|
||||
|
||||
schema('netdisco')->txn_do(sub {
|
||||
my $gone = $device->device_ips->delete;
|
||||
debug sprintf ' [%s] device - removed %d aliases',
|
||||
@@ -228,7 +238,7 @@ register_worker({ phase => 'early', driver => 'snmp' }, sub {
|
||||
|
||||
if (exists $i_ignore->{$entry}) {
|
||||
debug sprintf ' [%s] interfaces - ignoring %s (%s) (%s)',
|
||||
$device->ip, $entry, $port, $i_type->{$entry};
|
||||
$device->ip, $entry, $port, ($i_type->{$entry} || '');
|
||||
next;
|
||||
}
|
||||
|
||||
@@ -298,6 +308,9 @@ register_worker({ phase => 'early', driver => 'snmp' }, sub {
|
||||
$interfaces{$master}->{is_master} = 'true';
|
||||
}
|
||||
|
||||
# support for Hooks
|
||||
vars->{'hook_data'}->{'ports'} = [values %interfaces];
|
||||
|
||||
schema('netdisco')->resultset('DevicePort')->txn_do_locked(sub {
|
||||
my $gone = $device->ports->delete({keep_nodes => 1});
|
||||
debug sprintf ' [%s] interfaces - removed %d interfaces',
|
||||
|
||||
@@ -124,6 +124,9 @@ register_worker({ phase => 'main', driver => 'snmp' }, sub {
|
||||
};
|
||||
}
|
||||
|
||||
# support for Hooks
|
||||
vars->{'hook_data'}->{'vlans'} = \@devicevlans;
|
||||
|
||||
schema('netdisco')->txn_do(sub {
|
||||
my $gone = $device->vlans->delete;
|
||||
debug sprintf ' [%s] vlans - removed %d device VLANs',
|
||||
|
||||
Reference in New Issue
Block a user