|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
#!/usr/bin/perl
|
|
|
|
|
#
|
|
|
|
|
# Copyright: 2013 Sipwise Development Team <support@sipwise.com>
|
|
|
|
|
# Copyright: 2013-2016 Sipwise Development Team <support@sipwise.com>
|
|
|
|
|
#
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
@ -21,169 +21,192 @@
|
|
|
|
|
use strict;
|
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
|
|
die("Not implemented in REST");
|
|
|
|
|
|
|
|
|
|
use English;
|
|
|
|
|
use Getopt::Std;
|
|
|
|
|
use Getopt::Long;
|
|
|
|
|
use Cwd 'abs_path';
|
|
|
|
|
use Config::Tiny;
|
|
|
|
|
use Sipwise::API qw(all);
|
|
|
|
|
use YAML;
|
|
|
|
|
use Getopt::Long;
|
|
|
|
|
use Sipwise::Provisioning::Voip;
|
|
|
|
|
use Sipwise::Provisioning::Billing;
|
|
|
|
|
use Sipwise::Provisioning::Config;
|
|
|
|
|
|
|
|
|
|
our %CONFIG = ( admin => 'cmd' );
|
|
|
|
|
|
|
|
|
|
my $config = Sipwise::Provisioning::Config->new()->get_config();
|
|
|
|
|
|
|
|
|
|
unless ($CONFIG{password} = $config->{acl}->{$CONFIG{admin}}->{password}) {
|
|
|
|
|
die "Error: No provisioning password found for user $CONFIG{admin}\n";
|
|
|
|
|
my $config = Config::Tiny->read('/etc/default/ngcp-api');
|
|
|
|
|
my $opts;
|
|
|
|
|
if ($config) {
|
|
|
|
|
$opts = {};
|
|
|
|
|
$opts->{host} = $config->{_}->{NGCP_API_IP};
|
|
|
|
|
$opts->{port} = $config->{_}->{NGCP_API_PORT};
|
|
|
|
|
$opts->{sslverify} = $config->{_}->{NGCP_API_SSLVERIFY};
|
|
|
|
|
}
|
|
|
|
|
my $api = Sipwise::API->new($opts);
|
|
|
|
|
$opts = $api->opts;
|
|
|
|
|
my $del;
|
|
|
|
|
|
|
|
|
|
sub usage;
|
|
|
|
|
sub call_prov;
|
|
|
|
|
|
|
|
|
|
sub usage {
|
|
|
|
|
return "Usage:\n$PROGRAM_NAME peer.yml\n".
|
|
|
|
|
"Options:\n".
|
|
|
|
|
" -delete\n".
|
|
|
|
|
" -d debug\n".
|
|
|
|
|
" -h this help\n";
|
|
|
|
|
}
|
|
|
|
|
my $help = 0;
|
|
|
|
|
my $del = 0;
|
|
|
|
|
GetOptions ("h|help" => \$help,
|
|
|
|
|
"d|delete" => \$del)
|
|
|
|
|
"d|debug" => \$opts->{verbose},
|
|
|
|
|
"delete" => \$del)
|
|
|
|
|
or die("Error in command line arguments\n".usage());
|
|
|
|
|
|
|
|
|
|
die(usage()) unless (!$help);
|
|
|
|
|
die("Wrong number of arguments\n".usage()) unless ($#ARGV == 0);
|
|
|
|
|
die("Error: wrong number of arguments\n".usage()) unless ($#ARGV == 0);
|
|
|
|
|
|
|
|
|
|
our $bprov = Sipwise::Provisioning::Billing->new();
|
|
|
|
|
our $vprov = Sipwise::Provisioning::Voip->new();
|
|
|
|
|
|
|
|
|
|
if ($del)
|
|
|
|
|
sub manage_contact
|
|
|
|
|
{
|
|
|
|
|
do_delete();
|
|
|
|
|
my $contact = shift;
|
|
|
|
|
my $type = shift;
|
|
|
|
|
|
|
|
|
|
$contact->{id} = $api->check_contact_exists($contact, $type);
|
|
|
|
|
if(defined $contact->{id}) {
|
|
|
|
|
print "contact_$type [$contact->{email}] already there [$contact->{id}]\n";
|
|
|
|
|
} else {
|
|
|
|
|
$contact->{id} = $api->create_contact($contact, $type);
|
|
|
|
|
print "contact_$type [$contact->{email}]: created [$contact->{id}]\n";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
do_create();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub do_delete
|
|
|
|
|
sub manage_contract
|
|
|
|
|
{
|
|
|
|
|
my $filename = abs_path($ARGV[0]);
|
|
|
|
|
my $r = YAML::LoadFile($filename);
|
|
|
|
|
my $contract = shift;
|
|
|
|
|
|
|
|
|
|
for (keys $r)
|
|
|
|
|
{
|
|
|
|
|
my $peer_name = $_;
|
|
|
|
|
my $peer = $r->{$peer_name};
|
|
|
|
|
# groups
|
|
|
|
|
my $group = {};
|
|
|
|
|
my $result = call_prov( $vprov, 'get_peer_groups');
|
|
|
|
|
foreach (@{$result})
|
|
|
|
|
if(defined $contract->{contact_id})
|
|
|
|
|
{
|
|
|
|
|
$group->{$_->{name}} = $_->{id};
|
|
|
|
|
$contract->{id} = $api->check_contract_exists($contract);
|
|
|
|
|
if(defined $contract->{id}) {
|
|
|
|
|
print "contract: already there [$contract->{id}]\n";
|
|
|
|
|
} else {
|
|
|
|
|
$contract->{id} = $api->create_contract($contract);
|
|
|
|
|
print "contract : created [$contract->{id}]\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
die("Error: contract: No contact_id");
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
foreach (@{$peer->{groups}})
|
|
|
|
|
|
|
|
|
|
sub manage_groups
|
|
|
|
|
{
|
|
|
|
|
if (exists($group->{$_->{name}}))
|
|
|
|
|
my $data = shift;
|
|
|
|
|
|
|
|
|
|
foreach my $group (@{$data})
|
|
|
|
|
{
|
|
|
|
|
call_prov($vprov, 'delete_peer_group', {id => $group->{$_->{name}}});
|
|
|
|
|
$group->{id} = $api->check_peeringgroup_exists($group);
|
|
|
|
|
if(defined $group->{id}) {
|
|
|
|
|
print "peer_group: already there [$group->{id}]\n";
|
|
|
|
|
} else {
|
|
|
|
|
$group->{id} = $api->create_peeringgroup($group);
|
|
|
|
|
print "peer_group: created [$group->{id}]\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
# contracts
|
|
|
|
|
$result = call_prov( $bprov, 'get_sip_peering_contracts');
|
|
|
|
|
foreach (@{$result})
|
|
|
|
|
{
|
|
|
|
|
my $contact = $_->{contact};
|
|
|
|
|
if(defined($contact->{company}))
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub manage_rules
|
|
|
|
|
{
|
|
|
|
|
if ($contact->{company} eq $peer_name)
|
|
|
|
|
my $data = shift;
|
|
|
|
|
|
|
|
|
|
foreach my $rule (@{$data})
|
|
|
|
|
{
|
|
|
|
|
call_prov($bprov, 'delete_sip_peering_contract', { id => $_->{id}});
|
|
|
|
|
$rule->{id} = $api->check_peeringrule_exists($rule);
|
|
|
|
|
if(defined $rule->{id}) {
|
|
|
|
|
print "rule: already there [$rule->{id}]\n";
|
|
|
|
|
} else {
|
|
|
|
|
$rule->{id} = $api->create_peeringrule($rule);
|
|
|
|
|
print "rule: created [$rule->{id}]\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub manage_hosts
|
|
|
|
|
{
|
|
|
|
|
my $data = shift;
|
|
|
|
|
|
|
|
|
|
foreach my $host (@{$data})
|
|
|
|
|
{
|
|
|
|
|
my $param = {name => $host->{name}, contract_id => $host->{contract_id}};
|
|
|
|
|
$host->{id} = $api->check_peeringserver_exists($param);
|
|
|
|
|
if(defined $host->{id}) {
|
|
|
|
|
print "peer: $host->{name} already there [$host->{id}]\n";
|
|
|
|
|
} else {
|
|
|
|
|
$host->{id} = $api->create_peeringserver($host);
|
|
|
|
|
print "peer: $host->{name} created [$host->{id}]\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
exit;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub do_create {
|
|
|
|
|
my $filename = abs_path($ARGV[0]);
|
|
|
|
|
my $r = YAML::LoadFile($filename);
|
|
|
|
|
sub do_delete
|
|
|
|
|
{
|
|
|
|
|
my $r = shift;
|
|
|
|
|
|
|
|
|
|
for (keys $r)
|
|
|
|
|
foreach (keys %{$r})
|
|
|
|
|
{
|
|
|
|
|
my $peer = $r->{$_};
|
|
|
|
|
# contract
|
|
|
|
|
my $param = { data => $peer->{contract} };
|
|
|
|
|
my $contract_id = call_prov( $bprov, 'create_sip_peering_contract', $param );
|
|
|
|
|
# groups
|
|
|
|
|
my $group = {}; # name = id
|
|
|
|
|
foreach (@{$peer->{groups}})
|
|
|
|
|
{
|
|
|
|
|
$_->{peering_contract_id} = $contract_id;
|
|
|
|
|
call_prov( $vprov, 'create_peer_group', $_ );
|
|
|
|
|
$peer->{contact}->{id} = $api->check_contact_exists($peer->{contact},'system');
|
|
|
|
|
my $contract_id = $api->check_contract_exists($peer->{contract});
|
|
|
|
|
foreach my $group (@{$peer->{groups}}) {
|
|
|
|
|
$group->{contract_id} = $contract_id;
|
|
|
|
|
$group->{id} = $api->check_peeringgroup_exists($group);
|
|
|
|
|
if(defined $group->{id}) {
|
|
|
|
|
if($api->delete_peeringgroup($group->{id})) {
|
|
|
|
|
print "peer_group: deleted [$group->{name}]\n";
|
|
|
|
|
} else {
|
|
|
|
|
die("Error: peer_group: can't delete [$group->{name}]");
|
|
|
|
|
}
|
|
|
|
|
my $result = call_prov( $vprov, 'get_peer_groups');
|
|
|
|
|
foreach (@{$result})
|
|
|
|
|
{
|
|
|
|
|
$group->{$_->{name}}->{id} = $_->{id};
|
|
|
|
|
} else {
|
|
|
|
|
print "peer_group: already gone [$group->{name}]\n";
|
|
|
|
|
}
|
|
|
|
|
# rules
|
|
|
|
|
foreach (@{$peer->{rules}})
|
|
|
|
|
{
|
|
|
|
|
$_->{group_id} = $group->{$_->{group_id}}->{id};
|
|
|
|
|
call_prov($vprov, 'create_peer_rule', $_);
|
|
|
|
|
}
|
|
|
|
|
# hosts
|
|
|
|
|
my $host = {};
|
|
|
|
|
foreach (@{$peer->{hosts}})
|
|
|
|
|
{
|
|
|
|
|
$host->{$_->{data}->{name}} = { preferences => delete $_->{preferences}};
|
|
|
|
|
$_->{group_id} = $group->{$_->{group_id}}->{id};
|
|
|
|
|
call_prov($vprov, 'create_peer_host', $_);
|
|
|
|
|
}
|
|
|
|
|
foreach (keys $group)
|
|
|
|
|
{
|
|
|
|
|
my $result = call_prov( $vprov, 'get_peer_group_details', {id=>$group->{$_}->{id}});
|
|
|
|
|
foreach (@{$result->{peers}})
|
|
|
|
|
{
|
|
|
|
|
if(exists($host->{$_->{name}}))
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub do_create {
|
|
|
|
|
my $r = shift;
|
|
|
|
|
|
|
|
|
|
foreach (keys %{$r})
|
|
|
|
|
{
|
|
|
|
|
$param = { id => $_->{id}, preferences => $host->{$_->{name}}->{preferences}};
|
|
|
|
|
call_prov($vprov, 'set_peer_preferences', $param);
|
|
|
|
|
print "processing $_\n";
|
|
|
|
|
my $peer = $r->{$_};
|
|
|
|
|
manage_contact($peer->{contact}, 'system');
|
|
|
|
|
$peer->{contract}->{contact_id} = $peer->{contact}->{id};
|
|
|
|
|
manage_contract($peer->{contract});
|
|
|
|
|
my $group = {};
|
|
|
|
|
foreach (@{$peer->{groups}}) {
|
|
|
|
|
$_->{contract_id} = $peer->{contract}->{id};
|
|
|
|
|
$group->{$_->{name}} = $_;
|
|
|
|
|
}
|
|
|
|
|
manage_groups($peer->{groups});
|
|
|
|
|
foreach (@{$peer->{rules}}) {
|
|
|
|
|
$_->{group_id} = $group->{$_->{group_id}}->{id};
|
|
|
|
|
}
|
|
|
|
|
manage_rules($peer->{rules});
|
|
|
|
|
foreach (@{$peer->{hosts}}) {
|
|
|
|
|
$_->{group_id} = $group->{$_->{group_id}}->{id};
|
|
|
|
|
}
|
|
|
|
|
manage_hosts($peer->{hosts});
|
|
|
|
|
}
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub call_prov {
|
|
|
|
|
# scalar, scalar, hash-ref
|
|
|
|
|
my ($prov, $function, $parameter) = @_;
|
|
|
|
|
my $result;
|
|
|
|
|
sub main {
|
|
|
|
|
my $r = shift;
|
|
|
|
|
|
|
|
|
|
eval {
|
|
|
|
|
$result = $prov->handle_request( $function,
|
|
|
|
|
{
|
|
|
|
|
authentication => {
|
|
|
|
|
type => 'system',
|
|
|
|
|
username => $CONFIG{admin},
|
|
|
|
|
password => $CONFIG{password},
|
|
|
|
|
},
|
|
|
|
|
parameters => $parameter,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if($EVAL_ERROR) {
|
|
|
|
|
if(ref $EVAL_ERROR eq 'SOAP::Fault') {
|
|
|
|
|
die "Voip\::$function failed: ". $EVAL_ERROR->faultstring;
|
|
|
|
|
if ($del) {
|
|
|
|
|
print "delete peers\n" unless $opts->{debug};
|
|
|
|
|
return do_delete($r);
|
|
|
|
|
} else {
|
|
|
|
|
die "Voip\::$function failed: $EVAL_ERROR";
|
|
|
|
|
print "create peers\n" unless $opts->{debug};
|
|
|
|
|
return do_create($r);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub usage {
|
|
|
|
|
return "Usage:\n$PROGRAM_NAME peer.yml\n";
|
|
|
|
|
}
|
|
|
|
|
main(YAML::LoadFile(abs_path($ARGV[0])));
|
|
|
|
|