Implement Hooks per #726
This commit is contained in:
@@ -46,7 +46,7 @@ register 'register_worker' => sub {
|
||||
# support part-actions via action::namespace
|
||||
if ($job->only_namespace and $workerconf->{phase} ne 'check') {
|
||||
return unless $workerconf->{namespace} eq lc( $job->only_namespace )
|
||||
or (($workerconf->{phase} eq 'early')
|
||||
or (($job->only_namespace ne 'hooks') and ($workerconf->{phase} eq 'early')
|
||||
and ($job->device and not $job->device->in_storage));
|
||||
}
|
||||
|
||||
|
||||
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',
|
||||
|
||||
16
lib/App/Netdisco/Worker/Plugin/Hook.pm
Normal file
16
lib/App/Netdisco/Worker/Plugin/Hook.pm
Normal file
@@ -0,0 +1,16 @@
|
||||
package App::Netdisco::Worker::Plugin::Hook;
|
||||
|
||||
use Dancer ':syntax';
|
||||
use App::Netdisco::Worker::Plugin;
|
||||
use aliased 'App::Netdisco::Worker::Status';
|
||||
|
||||
register_worker({ phase => 'check' }, sub {
|
||||
my ($job, $workerconf) = @_;
|
||||
|
||||
return Status->error('can only run a specific hook')
|
||||
unless $job->action eq 'hook' and defined $job->only_namespace;
|
||||
|
||||
return Status->done('Hook is able to run.');
|
||||
});
|
||||
|
||||
true;
|
||||
63
lib/App/Netdisco/Worker/Plugin/Hook/HTTP.pm
Normal file
63
lib/App/Netdisco/Worker/Plugin/Hook/HTTP.pm
Normal file
@@ -0,0 +1,63 @@
|
||||
package App::Netdisco::Worker::Plugin::Hook::HTTP;
|
||||
|
||||
use Dancer ':syntax';
|
||||
use App::Netdisco::Worker::Plugin;
|
||||
use aliased 'App::Netdisco::Worker::Status';
|
||||
|
||||
use MIME::Base64 'decode_base64';
|
||||
use HTTP::Tiny;
|
||||
use Template;
|
||||
|
||||
register_worker({ phase => 'main' }, sub {
|
||||
my ($job, $workerconf) = @_;
|
||||
my $extra = from_json( decode_base64( $job->extra || '' ) );
|
||||
|
||||
my $event_data = $extra->{'event_data'};
|
||||
my $action_conf = $extra->{'action_conf'};
|
||||
$action_conf->{'body'} ||= to_json($event_data);
|
||||
|
||||
return Status->error('missing url parameter to http Hook')
|
||||
if !defined $action_conf->{'url'};
|
||||
|
||||
my $tt = Template->new({ ENCODING => 'utf8' });
|
||||
my $http = HTTP::Tiny
|
||||
->new( timeout => (($action_conf->{'timeout'} || 5000) / 1000) );
|
||||
|
||||
$action_conf->{'custom_headers'} ||= {};
|
||||
$action_conf->{'custom_headers'}->{'Content-Type'}
|
||||
||= 'application/json; charset=UTF-8';
|
||||
$action_conf->{'custom_headers'}->{'Authorization'}
|
||||
= ('Bearer '. $action_conf->{'bearer_token'})
|
||||
if $action_conf->{'bearer_token'};
|
||||
|
||||
my ($orig_url, $url) = ($action_conf->{'url'}, undef);
|
||||
$action_conf->{'url_is_template'} ||= 1
|
||||
if !exists $action_conf->{'url_is_template'};
|
||||
$tt->process(\$orig_url, $event_data, \$url)
|
||||
if $action_conf->{'url_is_template'};
|
||||
$url ||= $orig_url;
|
||||
|
||||
my ($orig_body, $body) = ($action_conf->{'body'} , undef);
|
||||
$action_conf->{'body_is_template'} ||= 1
|
||||
if !exists $action_conf->{'body_is_template'};
|
||||
$tt->process(\$orig_body, $event_data, \$body)
|
||||
if $action_conf->{'body_is_template'};
|
||||
$body ||= $orig_body;
|
||||
|
||||
my $response = $http->request(
|
||||
($action_conf->{'method'} || 'POST'), $url,
|
||||
{ headers => $action_conf->{'custom_headers'},
|
||||
content => $body },
|
||||
);
|
||||
|
||||
if ($action_conf->{'ignore_failure'} or $response->{'success'}) {
|
||||
return Status->done(sprintf 'HTTP Hook: %s %s',
|
||||
$response->{'status'}, $response->{'reason'});
|
||||
}
|
||||
else {
|
||||
return Status->error(sprintf 'HTTP Hook: %s %s',
|
||||
$response->{'status'}, $response->{'reason'});
|
||||
}
|
||||
});
|
||||
|
||||
true;
|
||||
Reference in New Issue
Block a user