On X1 requests, properly update the configured agents on the cluster. Currently we only throw an error if no agents are configured at all, but let it slip through if the actual requests are failing, as they might be down or failing. Change-Id: I0f4e021a5cc4ba6a30e30bb197ed20d4504797d8changes/22/3622/2
parent
7e7e148292
commit
520b66201e
@ -0,0 +1,84 @@
|
||||
package NGCP::Panel::Utils::Interception;
|
||||
|
||||
use Data::Dumper;
|
||||
use LWP::UserAgent;
|
||||
use TryCatch;
|
||||
|
||||
sub request {
|
||||
my ($c, $method, $uuid, $data) = @_;
|
||||
|
||||
my $a = _init($c);
|
||||
return unless($a);
|
||||
|
||||
return unless($method eq 'POST' || $method eq 'PUT' || $method eq 'DELETE');
|
||||
|
||||
my @agents = @{ $a->{ua} };
|
||||
my @urls = @{ $a->{url} };
|
||||
for(my $i = 0; $i < scalar(@agents); ++$i) {
|
||||
my $ua = $agents[$i];
|
||||
my $url = $urls[$i];
|
||||
if($method eq 'PUT' or $method eq 'DELETE') {
|
||||
return unless($uuid);
|
||||
$url .= '/'.$uuid;
|
||||
}
|
||||
$c->log->debug("performing $method for interception at $url");
|
||||
try {
|
||||
_request($c, $ua, $url, $method, $data);
|
||||
} catch($e) {
|
||||
# skip errors
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub _request {
|
||||
my ($c, $ua, $url, $method, $data) = @_;
|
||||
|
||||
my $req = HTTP::Request->new($method => $url);
|
||||
if($data) {
|
||||
$req->content_type('application/json');
|
||||
$req->content($jdata);
|
||||
}
|
||||
my $res = $ua->request($req);
|
||||
if($res->is_success) {
|
||||
return 1;
|
||||
} else {
|
||||
$c->log->error("Failed to do $method on $url: " . $res->status_line);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
sub _init {
|
||||
my ($c) = @_;
|
||||
|
||||
my @ua = ();
|
||||
my @url = ();
|
||||
my @cfgs = ();
|
||||
if(ref $c->config->{intercept}->{agent} eq 'HASH') {
|
||||
push @cfgs, $c->config->{intercept}->{agent};
|
||||
} elsif(ref $c->config->{intercept}->{agent} eq 'ARRAY') {
|
||||
@cfgs = @{ $c->config->{intercept}->{agent} };
|
||||
}
|
||||
unless(@cfgs) {
|
||||
$c->log->error("No intercept agents configured in ngcp_panel.conf, rejecting request");
|
||||
return;
|
||||
}
|
||||
foreach my $cfg(@cfgs) {
|
||||
my $agent = LWP::UserAgent->new();
|
||||
$agent->agent("Sipwise NGCP X1/0.2");
|
||||
$agent->timeout(2);
|
||||
if($cfg->{user} && $cfg->{pass}) {
|
||||
$agent->credentials(
|
||||
$cfg->{host}.":".$cfg->{port},
|
||||
$cfg->{realm},
|
||||
$cfg->{user},
|
||||
$cfg->{pass}
|
||||
);
|
||||
}
|
||||
push @ua, $agent;
|
||||
push @url, $cfg->{schema}.'://'.$cfg->{host}.':'.$cfg->{port}.$cfg->{url};
|
||||
}
|
||||
return { ua => \@ua, url => \@url };
|
||||
}
|
||||
|
||||
1;
|
||||
Loading…
Reference in new issue