From 856a02c68f47819ed1bc43de66c5759679d5fc2b Mon Sep 17 00:00:00 2001 From: Gerhard Jungwirth Date: Thu, 5 Oct 2017 11:46:43 +0200 Subject: [PATCH] TT#22420 properly reload trusted sources via xmlrpc kamailio cannot see our changes while we're inside the transaction * also: fix kamailio xmlrpc return code Change-Id: I4949ee16dd12ce8a501c42106aaabca0c69f388f --- .../Panel/Controller/API/TrustedSources.pm | 13 +++++- .../Controller/API/TrustedSourcesItem.pm | 41 +++++++++++++++++-- lib/NGCP/Panel/Role/API/TrustedSources.pm | 1 - lib/NGCP/Panel/Utils/Kamailio.pm | 4 +- 4 files changed, 52 insertions(+), 7 deletions(-) diff --git a/lib/NGCP/Panel/Controller/API/TrustedSources.pm b/lib/NGCP/Panel/Controller/API/TrustedSources.pm index 03f9918f8a..0c3fa1b592 100644 --- a/lib/NGCP/Panel/Controller/API/TrustedSources.pm +++ b/lib/NGCP/Panel/Controller/API/TrustedSources.pm @@ -189,7 +189,6 @@ sub POST :Allow { my $item; try { $item = $c->model('DB')->resultset('voip_trusted_sources')->create($resource); - NGCP::Panel::Utils::Kamailio::trusted_reload($c); } catch($e) { $c->log->error("failed to create trusted source: $e"); # TODO: user, message, trace, ... $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to create trusted source."); @@ -204,6 +203,18 @@ sub POST :Allow { $guard->commit; + try { + my (undef, $xmlrpc_res) = NGCP::Panel::Utils::Kamailio::trusted_reload($c); + if (!defined $xmlrpc_res || $xmlrpc_res < 1) { + die "XMLRPC failed"; + } + } catch($e) { + $c->log->error("failed to reload kamailio: $e. Trusted source created"); + $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to reload kamailio. Trusted source was created"); + $c->response->header(Location => sprintf('/%s%d', $c->request->path, $item->id)); + last; + } + $c->response->status(HTTP_CREATED); $c->response->header(Location => sprintf('/%s%d', $c->request->path, $item->id)); $c->response->body(q()); diff --git a/lib/NGCP/Panel/Controller/API/TrustedSourcesItem.pm b/lib/NGCP/Panel/Controller/API/TrustedSourcesItem.pm index 26535616d2..3739990793 100644 --- a/lib/NGCP/Panel/Controller/API/TrustedSourcesItem.pm +++ b/lib/NGCP/Panel/Controller/API/TrustedSourcesItem.pm @@ -74,9 +74,8 @@ sub GET :Allow { my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new( (map { # XXX Data::HAL must be able to generate links with multiple relations - s|rel="(http://purl.org/sipwise/ngcp-api/#rel-resellers)"|rel="item $1"|; - s/rel=self/rel="item self"/; - $_ + s|rel="(http://purl.org/sipwise/ngcp-api/#rel-\w+)"|rel="item $1"|r =~ + s/rel=self/rel="item self"/r; } $hal->http_headers), ), $hal->as_json); $c->response->headers($response->headers); @@ -134,6 +133,17 @@ sub PATCH :Allow { $guard->commit; + try { + my (undef, $xmlrpc_res) = NGCP::Panel::Utils::Kamailio::trusted_reload($c); + if (!defined $xmlrpc_res || $xmlrpc_res < 1) { + die "XMLRPC failed"; + } + } catch($e) { + $c->log->error("failed to reload kamailio: $e. Trusted source modified."); + $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to reload kamailio. Trusted source was modified."); + last; + } + if ('minimal' eq $preference) { $c->response->status(HTTP_NO_CONTENT); $c->response->header(Preference_Applied => 'return=minimal'); @@ -175,7 +185,19 @@ sub PUT :Allow { my $hal = $self->hal_from_item($c, $item, $form); last unless $self->add_update_journal_item_hal($c,$hal); - $guard->commit; + $guard->commit; + + try { + my (undef, $xmlrpc_res) = NGCP::Panel::Utils::Kamailio::trusted_reload($c); + if (!defined $xmlrpc_res || $xmlrpc_res < 1) { + die "XMLRPC failed"; + } + } catch($e) { + $c->log->error("failed to reload kamailio: $e. Trusted source modified."); + $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to reload kamailio. Trusted source was modified."); + # TODO: do we still want to return the modified item here? + last; + } if ('minimal' eq $preference) { $c->response->status(HTTP_NO_CONTENT); @@ -212,6 +234,17 @@ sub DELETE :Allow { $guard->commit; + try { + my (undef, $xmlrpc_res) = NGCP::Panel::Utils::Kamailio::trusted_reload($c); + if (!defined $xmlrpc_res || $xmlrpc_res < 1) { + die "XMLRPC failed"; + } + } catch($e) { + $c->log->error("failed to reload kamailio: $e. Trusted source deleted."); + $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to reload kamailio. Trusted source was deleted."); + last; + } + $c->response->status(HTTP_NO_CONTENT); $c->response->body(q()); } diff --git a/lib/NGCP/Panel/Role/API/TrustedSources.pm b/lib/NGCP/Panel/Role/API/TrustedSources.pm index 00762486d4..0836c1fc88 100644 --- a/lib/NGCP/Panel/Role/API/TrustedSources.pm +++ b/lib/NGCP/Panel/Role/API/TrustedSources.pm @@ -115,7 +115,6 @@ sub update_item { $resource->{uuid} = $sub->uuid; $item->update($resource); - NGCP::Panel::Utils::Kamailio::trusted_reload($c); return $item; } diff --git a/lib/NGCP/Panel/Utils/Kamailio.pm b/lib/NGCP/Panel/Utils/Kamailio.pm index 25f4577434..209e08fe81 100644 --- a/lib/NGCP/Panel/Utils/Kamailio.pm +++ b/lib/NGCP/Panel/Utils/Kamailio.pm @@ -86,16 +86,18 @@ sub flush { EOF } +# returns: () or (ID, STATUS, [XML]) sub trusted_reload { my ($c) = @_; my $dispatcher = NGCP::Panel::Utils::XMLDispatcher->new; - my $ret = $dispatcher->dispatch($c, "proxy-ng", 1, 1, <dispatch($c, "proxy-ng", 1, 1, < permissions.trustedReload EOF + return ref $ret ? @{ $ret } : (); } sub get_aor{