« blib/lib/App/Netdisco/Worker/Plugin/Hook.pm
↑ Summary
blib/lib/App/Netdisco/Worker/Plugin/Internal/BackendFQDN.pm »
| 1 | package App::Netdisco::Worker::Plugin::Hook::HTTP; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use App::Netdisco::Worker::Plugin; | ||
| 5 | use aliased 'App::Netdisco::Worker::Status'; | ||
| 6 | |||
| 7 | use Encode 'encode'; | ||
| 8 | use MIME::Base64 'decode_base64'; | ||
| 9 | use HTTP::Tiny; | ||
| 10 | use Template; | ||
| 11 | |||
| 12 | register_worker({ phase => 'main' }, sub { | ||
| 13 | my ($job, $workerconf) = @_; | ||
| 14 | my $extra = from_json( decode_base64( $job->extra || '' ) ); | ||
| 15 | $job->subaction(''); | ||
| 16 | |||
| 17 | my $event_data = $extra->{'event_data'}; | ||
| 18 | my $action_conf = $extra->{'action_conf'}; | ||
| 19 | $action_conf->{'body'} ||= to_json($event_data); | ||
| 20 | |||
| 21 | return Status->error('missing url parameter to http Hook') | ||
| 22 | if !defined $action_conf->{'url'}; | ||
| 23 | |||
| 24 | my $tt = Template->new({ ENCODING => 'utf8' }); | ||
| 25 | my $http = HTTP::Tiny->new( | ||
| 26 | timeout => (($action_conf->{'timeout'} || 5000) / 1000), | ||
| 27 | # explicit: HTTP::Tiny before 0.083 neither verifies by default | ||
| 28 | # nor reads this variable | ||
| 29 | verify_SSL => | ||
| 30 | (($ENV{PERL_HTTP_TINY_SSL_INSECURE_BY_DEFAULT} || '') ne '1'), | ||
| 31 | ); | ||
| 32 | |||
| 33 | $action_conf->{'custom_headers'} ||= {}; | ||
| 34 | $action_conf->{'custom_headers'}->{'Content-Type'} | ||
| 35 | ||= 'application/json; charset=UTF-8'; | ||
| 36 | $action_conf->{'custom_headers'}->{'Authorization'} | ||
| 37 | = ('Bearer '. $action_conf->{'bearer_token'}) | ||
| 38 | if $action_conf->{'bearer_token'}; | ||
| 39 | |||
| 40 | my ($orig_url, $url) = ($action_conf->{'url'}, undef); | ||
| 41 | $action_conf->{'url_is_template'} ||= 1 | ||
| 42 | if !exists $action_conf->{'url_is_template'}; | ||
| 43 | $tt->process(\$orig_url, $event_data, \$url) | ||
| 44 | if $action_conf->{'url_is_template'}; | ||
| 45 | $url ||= $orig_url; | ||
| 46 | |||
| 47 | my ($orig_body, $body) = ($action_conf->{'body'} , undef); | ||
| 48 | $action_conf->{'body_is_template'} ||= 1 | ||
| 49 | if !exists $action_conf->{'body_is_template'}; | ||
| 50 | $tt->process(\$orig_body, $event_data, \$body) | ||
| 51 | if $action_conf->{'body_is_template'}; | ||
| 52 | $body ||= $orig_body; | ||
| 53 | |||
| 54 | my $response = $http->request( | ||
| 55 | ($action_conf->{'method'} || 'POST'), $url, | ||
| 56 | { headers => $action_conf->{'custom_headers'}, | ||
| 57 | content => encode('UTF-8', $body ) }, | ||
| 58 | ); | ||
| 59 | |||
| 60 | if ($action_conf->{'ignore_failure'} or $response->{'success'}) { | ||
| 61 | return Status->done(sprintf 'HTTP Hook: %s %s', | ||
| 62 | $response->{'status'}, $response->{'reason'}); | ||
| 63 | } | ||
| 64 | else { | ||
| 65 | return Status->error(sprintf 'HTTP Hook: %s %s (%s)', | ||
| 66 | $response->{'status'}, $response->{'reason'}, ($response->{'content'} || 'no content')); | ||
| 67 | } | ||
| 68 | }); | ||
| 69 | |||
| 70 | true; |