| 1 | package App::Netdisco::Util::Worker; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use App::Netdisco::JobQueue 'jq_insert'; | ||
| 5 | |||
| 6 | use Encode 'encode'; | ||
| 7 | use MIME::Base64 'encode_base64'; | ||
| 8 | |||
| 9 | use Storable 'dclone'; | ||
| 10 | use Data::Visitor::Tiny; | ||
| 11 | |||
| 12 | use base 'Exporter'; | ||
| 13 | our @EXPORT = ('queue_hook'); | ||
| 14 | |||
| 15 | sub queue_hook { | ||
| 16 | my ($hook, $conf) = @_; | ||
| 17 | |||
| 18 | unless ($conf->{'type'}) { | ||
| 19 | warning sprintf ' hooks - missing type in hook config for event "%s", skipping', ($hook || '?'); | ||
| 20 | return 0; | ||
| 21 | } | ||
| 22 | |||
| 23 | unless (lc($conf->{'type'}) =~ m/^(?:http|exec)$/) { | ||
| 24 | warning sprintf ' hooks - unknown hook type "%s" for event "%s", skipping', $conf->{'type'}, ($hook || '?'); | ||
| 25 | return 0; | ||
| 26 | } | ||
| 27 | |||
| 28 | my $hook_data = dclone (vars->{'hook_data'} || {}); | ||
| 29 | my $extra = { action_conf => dclone ($conf->{'with'} || {}), | ||
| 30 | event_data => dclone ($hook_data) }; | ||
| 31 | |||
| 32 | # remove scalar references which to_json cannot handle | ||
| 33 | visit( $extra->{'event_data'}, sub { | ||
| 34 | my ($key, $valueref) = @_; | ||
| 35 | $$valueref = '' if ref $$valueref eq 'SCALAR'; | ||
| 36 | }); | ||
| 37 | |||
| 38 | # Extract device IP from hook_data for backend routing | ||
| 39 | my $device_ip = $hook_data->{'ip'}; | ||
| 40 | |||
| 41 | my $job_spec = { | ||
| 42 | action => ('hook::'. lc($conf->{'type'})), | ||
| 43 | extra => encode_base64( encode('UTF-8', to_json( $extra )), '' ), | ||
| 44 | }; | ||
| 45 | |||
| 46 | # Include device parameter if available for proper backend routing | ||
| 47 | $job_spec->{device} = $device_ip if defined $device_ip; | ||
| 48 | |||
| 49 | jq_insert($job_spec); | ||
| 50 | |||
| 51 | return 1; | ||
| 52 | } | ||
| 53 | |||
| 54 | true; |