From 1c66df8bdd39c4194cb07ec6fe912dfc2c9edeee Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Wed, 3 Jun 2015 14:20:04 +0200 Subject: [PATCH] MT#13015 migrate scripts to REST API use /etc/default/ngcp-api as default values Change-Id: I93a5a5d30948afa16150a2696dc370fc912e139a --- bin/ngcp-create_customer | 188 +++++++++++++++++++ bin/ngcp-create_domain | 207 ++++++++++++++++----- bin/ngcp-create_subscriber | 340 ++++++++++++++++++++++++---------- bin/ngcp-delete_domain | 257 +++++++++++++++++-------- bin/ngcp-get_customer | 225 ++++++++++++++++++++++ bin/ngcp-terminate_customer | 233 +++++++++++++++++++++++ bin/ngcp-terminate_subscriber | 291 ++++++++++++++++++++++------- 7 files changed, 1446 insertions(+), 295 deletions(-) create mode 100755 bin/ngcp-create_customer create mode 100755 bin/ngcp-get_customer create mode 100755 bin/ngcp-terminate_customer diff --git a/bin/ngcp-create_customer b/bin/ngcp-create_customer new file mode 100755 index 0000000..002afea --- /dev/null +++ b/bin/ngcp-create_customer @@ -0,0 +1,188 @@ +#!/usr/bin/perl +use strict; +use warnings; +use Config::Tiny; +use English; +use Getopt::Long; +use JSON qw(); +use LWP::UserAgent; +use Pod::Usage; + +my $config = Config::Tiny->read('/etc/default/ngcp-api'); +my $opts = { + billing_id => 1, + contact_id => 1, + type => 'sipaccount', + host => '127.0.0.1', + port => 1443, + auth_user => 'administrator', + auth_pwd => 'administrator', + verbose => 0 +}; + +if ($config) { + $opts->{host} = $config->{_}->{NGCP_API_IP}; + $opts->{port} = $config->{_}->{NGCP_API_PORT}; +} + +GetOptions( $opts, + "help|h" => sub { pod2usage(-exitval =>0); }, + "billing_id=i", + "contact_id=i", + "host=s", + "port=i", + "auth_user=s", + "auth_pwd=s", + "verbose", + "type=s", + "man" => sub { pod2usage(-exitval => 0, -verbose => 2); } +) or pod2usage(2); + +sub main { + my $urlbase = 'https://'.$opts->{host}.':'.$opts->{port}; + my $data = { + billing_profile_id => $opts->{billing_id}, + contact_id => $opts->{contact_id}, + type => $opts->{type}, + status => 'active', + }; + my $ua = LWP::UserAgent->new(); + # set to 0 if using a self-signed certificate + $ua->ssl_opts(verify_hostname => 0); + $ua->credentials($opts->{host}.':'.$opts->{port}, 'api_admin_http', + $opts->{auth_user}, $opts->{auth_pwd}); + # debug!! + if($opts->{verbose}) { + $ua->show_progress(1); + $ua->add_handler("request_send", sub { shift->dump; return }); + $ua->add_handler("response_done", sub { shift->dump; return }); + } + my $res = do_request($ua, $urlbase, $data); + if($res->is_success) { + print $res->status_line . ' ' . $res->header('Location'). "\n"; + } else { + die $res->as_string; + } + return; +} + +sub do_request { + my $ua = shift; + my $urlbase = shift; + my $data = shift; + + my $req = HTTP::Request->new('POST', $urlbase.'/api/customers/'); + $req->header('Content-Type' => 'application/json'); + $req->header('Prefer' => 'return=representation'); + $req->content(JSON::to_json($data)); + return $ua->request($req); +} + +main(); + +__END__ +=head1 NAME + +ngcp-create_customer - create a customer on NGCP + +=head1 SYNOPSIS + +ngcp-create_customer [options] + +=head1 OPTIONS + +=over 8 + +=item B<-help> + +Print a brief help message and exits. + +=item B<-auth_user> + +Authentication username . Defaults to 'administrator'. + +=item B<-auth_pwd> + +Authentication password . Defaults to 'administrator'. + +=item B<-host> + +Host where the send queries. Defaults to '127.0.0.1'. + +=item B<-port> + +Port where the send queries. Defaults to 1443. + +=item B<-billing_id> + +The billing profile id. Defaults to 1. + +=item B<-contact_id> + +The contact id. Defaults to 1. + +=item B<-type> + +Customer can be one of the "sipaccount" or "pbxaccount" type. Defaults to +"sipaccount". + +=item B<-verbose> + +See debug information. Default false. + +=back + +=head1 DESCRIPTION + +B will create a subscriber at NGCP. + +=head1 USAGE + +ngcp-create_customer -host 1.2.3.4 -billing_id 1 -profile_id 4 -type sipaccount + +=head1 REQUIRED ARGUMENTS + +TODO + +=head1 EXIT STATUS + +Exit code 0 means that everything should have went fine otherwise error. + +=head1 DIAGNOSTICS + +=head1 CONFIGURATION + +/etc/default/ngcp-api for default values + +=head1 DEPENDENCIES + +ngcp-create_customer relies on a bunch of Perl modules, all of them specified as +dependencies through the ngcp-ossbss-clients-perl Debian package. + +=head1 INCOMPATIBILITIES + +No known at this time. + +=head1 BUGS AND LIMITATIONS + +Please report problems you notice to the Sipwise +Development Team . + +=head1 AUTHOR + +Victor Seva + +=head1 LICENSE + +Copyright (c) 2015 Sipwise GmbH, Austria. +All rights reserved. You may not copy, distribute +or modify without prior written permission from +Sipwise GmbH, Austria. + +=head1 LICENSE AND COPYRIGHT + +Copyright (c) 2015 Sipwise GmbH, Austria. +You should have received a copy of the licence terms together with the +software. + +=cut diff --git a/bin/ngcp-create_domain b/bin/ngcp-create_domain index 8173486..93d8bab 100755 --- a/bin/ngcp-create_domain +++ b/bin/ngcp-create_domain @@ -1,65 +1,178 @@ #!/usr/bin/perl use strict; use warnings; +use Config::Tiny; +use English; +use Getopt::Long; +use JSON qw(); +use LWP::UserAgent; +use Pod::Usage; -use Getopt::Std; -use Sipwise::Provisioning::Billing; -use Sipwise::Provisioning::Config; +my $config = Config::Tiny->read('/etc/default/ngcp-api'); +my $opts = { + reseller_id => 1, + host => '127.0.0.1', + port => 1443, + auth_user => 'administrator', + auth_pwd => 'administrator', + verbose => 0 +}; -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"; +if ($config) { + $opts->{host} = $config->{_}->{NGCP_API_IP}; + $opts->{port} = $config->{_}->{NGCP_API_PORT}; } -die usage() unless ($#ARGV == 0); +GetOptions( $opts, + "help|h" => sub { pod2usage(-exitval =>0); }, + "reseller_id=i", + "host=s", + "port=i", + "auth_user=s", + "auth_pwd=s", + "verbose", + "man" => sub { pod2usage(-exitval => 0, -verbose => 2); } +) or pod2usage(2); -sub main { - my ($bprov) = @_; - call_prov( $bprov, 'create_domain', - { - domain => $ARGV[0] - } - ); - exit; -} +die pod2usage(-exitval => 1, -message => "No domain") unless ($#ARGV == 0); -sub call_prov { - # scalar, scalar, hash-ref - my ($bprov, $function, $parameter) = @_; - my $result; - - eval { - $result = $bprov->handle_request( $function, - { - authentication => { - type => 'system', - username => $CONFIG{admin}, - password => $CONFIG{password}, - }, - parameters => $parameter, - }); +sub main { + my $domain = shift; + my $urlbase = 'https://'.$opts->{host}.':'.$opts->{port}; + my $data = { + domain => $domain, + reseller_id => $opts->{reseller_id} }; - - if($@) { - if(ref $@ eq 'SOAP::Fault') { - die "Billing\::$function failed: ". $@->faultstring; - } else { - die "Billing\::$function failed: $@"; - } + my $ua = LWP::UserAgent->new(); + # set to 0 if using a self-signed certificate + $ua->ssl_opts(verify_hostname => 0); + $ua->credentials($opts->{host}.':'.$opts->{port}, 'api_admin_http', + $opts->{auth_user}, $opts->{auth_pwd}); + # debug!! + if($opts->{verbose}) { + $ua->show_progress(1); + $ua->add_handler("request_send", sub { shift->dump; return }); + $ua->add_handler("response_done", sub { shift->dump; return }); } - return $result; + my $res = do_request($ua, $urlbase, $data); + if($res->is_success) { + print $res->status_line . ' ' . $res->header('Location') . "\n"; + } else { + die $res->as_string; + } + return; } -sub usage { - die "Usage:\n$0 \n". - "\ne.g.: $0 sip.sipwise.com\n"; +sub do_request { + my $ua = shift; + my $urlbase = shift; + my $data = shift; + + my $req = HTTP::Request->new('POST', $urlbase."/api/domains/"); + $req->header('Content-Type' => 'application/json'); + $req->header('Prefer' => 'return=representation'); + $req->content(JSON::to_json($data)); + + return $ua->request($req); } -my $bprov = Sipwise::Provisioning::Billing->new(); +main($ARGV[0]); + +__END__ +=head1 NAME + +ngcp-create_domain - create a domain on NGCP + +=head1 SYNOPSIS + +ngcp-create_domain [options] domain + +=head1 OPTIONS + +=over 8 + +=item B<-help> + +Print a brief help message and exits. + +=item B<-reseller_id> + +The reseller id to assign this domain to. Defaults to 1. + +=item B<-auth_user> + +Authentication username . Defaults to 'administrator'. + +=item B<-auth_pwd> + +Authentication password . Defaults to 'administrator'. + +=item B<-host> + +Host where the send queries. Defaults to '127.0.0.1'. + +=item B<-port> + +Port where the send queries. Defaults to 1443. + +=item B<-verbose> + +See debug information. Default false. + +=back + +=head1 DESCRIPTION + +B will create a domain at NGCP. + +=head1 USAGE + +ngcp-create_domain -host 1.2.3.4 -reseller_id 4 test.example.org + +=head1 REQUIRED ARGUMENTS + +B to be created + +=head1 EXIT STATUS + +Exit code 0 means that everything should have went fine otherwise error. + +=head1 DIAGNOSTICS + +=head1 CONFIGURATION + +/etc/default/ngcp-api for default values + +=head1 DEPENDENCIES + +ngcp-create_domain relies on a bunch of Perl modules, all of them specified as +dependencies through the ngcp-ossbss-clients-perl Debian package. + +=head1 INCOMPATIBILITIES + +No known at this time. + +=head1 BUGS AND LIMITATIONS + +Please report problems you notice to the Sipwise +Development Team . + +=head1 AUTHOR + +Victor Seva + +=head1 LICENSE + +Copyright (c) 2015 Sipwise GmbH, Austria. +All rights reserved. You may not copy, distribute +or modify without prior written permission from +Sipwise GmbH, Austria. + +=head1 LICENSE AND COPYRIGHT -main($bprov); +Copyright (c) 2015 Sipwise GmbH, Austria. +You should have received a copy of the licence terms together with the +software. +=cut diff --git a/bin/ngcp-create_subscriber b/bin/ngcp-create_subscriber index 9facca1..ccbd256 100755 --- a/bin/ngcp-create_subscriber +++ b/bin/ngcp-create_subscriber @@ -1,115 +1,265 @@ #!/usr/bin/perl use strict; +use warnings; +use Config::Tiny; +use English; +use Getopt::Long; +use JSON qw(); +use LWP::UserAgent; +use Pod::Usage; -use Getopt::Std; -use Sipwise::Provisioning::Billing; -use Sipwise::Provisioning::Config; +my $config = Config::Tiny->read('/etc/default/ngcp-api'); +my $opts = { + host => '127.0.0.1', + port => 1443, + auth_user => 'administrator', + auth_pwd => 'administrator', + verbose => 0, + admin => 0 +}; -my %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"; +if ($config) { + $opts->{host} = $config->{_}->{NGCP_API_IP}; + $opts->{port} = $config->{_}->{NGCP_API_PORT}; } -my %BILLING = ( - billing_profile => 'default', -# not needed, but may be set if desired -# product => 'handle', - ); - -sub main; -sub usage; -sub call_prov; - -my %opts; -getopts('v:u:d:p:c:a:n:s:', \%opts); - -die usage() unless defined $opts{u} and defined $opts{d} and defined $opts{p}; -die usage() unless (defined $opts{c} and defined $opts{a} and defined $opts{n}) - or (!defined $opts{c} and !defined $opts{a} and !defined $opts{n}); +GetOptions( $opts, + "help|h" => sub { pod2usage(-exitval =>0); }, + "customer_id=i", + "host=s", + "port=i", + "auth_user=s", + "auth_pwd=s", + "verbose", + "man" => sub { pod2usage(-exitval => 0, -verbose => 2); }, + "username|u=s", + "domain|d=s", + "password|p=s", + "admin|s=i", + "cc|c=i", + "ac|a=i", + "sn|n=i", + "account_id|v=i" +) or pod2usage(2); -my $bprov = Sipwise::Provisioning::Billing->new(); - -main; +die pod2usage(-exitval => 1, -message => "Missing parameters: customer_id") + unless defined $opts->{customer_id}; +die pod2usage(-exitval => 1, -message => "Missing parameters") unless + defined $opts->{username} and defined $opts->{domain} + and defined $opts->{password}; +die pod2usage(-exitval => 1, -message => "Missing parameters") unless + (defined $opts->{cc} and defined $opts->{ac} and defined $opts->{sn}) + or ( not defined $opts->{cc} and not defined $opts->{ac} + and not defined $opts->{sn}); sub main { - my $subscriber = { username => $opts{u}, - domain => $opts{d}, - password => $opts{p}, - admin => $opts{s} ? 1 : 0, - }; - $$subscriber{cc} = $opts{c} if defined $opts{c}; - $$subscriber{ac} = $opts{a} if defined $opts{a}; - $$subscriber{sn} = $opts{n} if defined $opts{n}; - - if(defined $opts{v}) { - my $account = call_prov( 'get_voip_account_by_id', { id => $opts{v} }); - call_prov( 'add_voip_account_subscriber', - { - id => $$account{id}, - subscriber => $subscriber, - } - ); - print "Added subscriber to VoIP account $$account{id}.\n"; + my $urlbase = 'https://'.$opts->{host}.':'.$opts->{port}; + + my $ua = LWP::UserAgent->new(); + # set to 0 if using a self-signed certificate + $ua->ssl_opts(verify_hostname => 0); + $ua->credentials($opts->{host}.':'.$opts->{port}, 'api_admin_http', + $opts->{auth_user}, $opts->{auth_pwd}); + # debug!! + if($opts->{verbose}) { + $ua->show_progress(1); + $ua->add_handler("request_send", sub { shift->dump; return }); + $ua->add_handler("response_done", sub { shift->dump; return }); + } + my $domain_id = get_domain_id($ua, $urlbase, $opts->{domain}); + my $res = do_request($ua, $urlbase, get_data($domain_id)); + if($res->is_success) { + print $res->status_line . ' ' . $res->header('Location') . "\n"; } else { - my $id = call_prov( 'create_voip_account', - { - data => { - %BILLING, - subscribers => [ $subscriber ], - }, - } - ); - print "Created VoIP account $id with one subscriber.\n"; + die $res->as_string; } + return; +} - exit; +sub get_data { + my $domain_id = shift; + my $data = { + administrative => $opts->{admin} || 0, + domain_id => $domain_id, + customer_id => $opts->{customer_id}, + username => $opts->{username}, + password => $opts->{password}, + primary_number => { + cc => $opts->{cc}, + ac => $opts->{ac}, + sn => $opts->{sn} + }, + }; + return $data; } +sub get_domain_id { + my $ua = shift; + my $urlbase = shift; + my $domain = shift; + my $url = $urlbase."/api/domains/?domain=".$domain; + my $req = HTTP::Request->new('GET', $url); + my $domain_id; -sub call_prov { - # scalar, hash-ref - my ($function, $parameter) = @_; - my $result; - - eval { - $result = $bprov->handle_request( $function, - { - authentication => { - type => 'system', - username => $CONFIG{admin}, - password => $CONFIG{password}, - }, - parameters => $parameter, - }); - }; - - if($@) { - if(ref $@ eq 'SOAP::Fault') { - die "Billing\::$function failed: ". $@->faultstring ."\n"; - } else { - die "Billing\::$function failed: $@\n"; + my $res = $ua->request($req); + if($res->is_success) { + my $collection = JSON::from_json($res->decoded_content); + if ($collection->{total_count} == 1) { + $domain_id = $collection->{_embedded}->{'ngcp:domains'}->{id}; + } + else { + pod2usage(-exitval => 3, -message => "Domain not found"); } } - - return $result; + else { + die $res->status_line, "\n"; + } + return $domain_id; } -sub usage { - die "Usage:\n$0 [-v account_id] -u -d -p [-c -a -n ] [-s {1|0}]\n\n". - "e.g.: $0 -u test -d sip.sipwise.com -p secret -c 43 -a 720 -n 555000\n\n". - "Options:\n". - " -v the unique ID of an existing account, the\n". - " script will create a new one if unspecified\n". - " -u new SIP username\n". - " -d existing domain for subscriber\n". - " -p unencrypted password for subscriber\n". - " -c country code of subscriber number\n". - " -a area code of subscriber number\n". - " -n local part of subscriber number\n". - " -s {1|0} whether or not to set the administrative flag\n". - " for the subscriber; defaults to 0 (no)\n". - ""; +sub do_request { + my $ua = shift; + my $urlbase = shift; + my $data = shift; + + my $req = HTTP::Request->new('POST', $urlbase.'/api/subscribers/'); + $req->header('Content-Type' => 'application/json'); + $req->header('Prefer' => 'return=representation'); + $req->content(JSON::to_json($data)); + return $ua->request($req); } + +main(); + +__END__ +=head1 NAME + +ngcp-create_subscriber - create a subscriber on NGCP + +=head1 SYNOPSIS + +ngcp-create_subscriber [options] + +=head1 OPTIONS + +=over 8 + +=item B<-help> + +Print a brief help message and exits. + +=item B<-customer_id> + +The customer id to assign this subscriber to. + +=item B<-auth_user> + +Authentication username . Defaults to 'administrator'. + +=item B<-auth_pwd> + +Authentication password . Defaults to 'administrator'. + +=item B<-host> + +Host where the send queries. Defaults to '127.0.0.1'. + +=item B<-port> + +Port where the send queries. Defaults to 1443. + +=item B<-account_id|v> + +the unique ID of an existing account, the script will create a new one +if unspecified. + +=item B<-username|u> + +new SIP username. + +=item B<-domain|d> + +existing domain for subscriber. + +=item B<-password|p> + +unencrypted password for subscriber. + +=item B<-cc|c> + +country code of subscriber number. + +=item B<-ac|a> + +area code of subscriber number. + +=item B<-number|n> + +local part of subscriber number. + +=item B<-admin|s> + +whether or not to set the administrative flag for the subscriber. +defaults to 0 (no). + +=item B<-verbose> + +See debug information. Default false. + +=back + +=head1 DESCRIPTION + +B will create a subscriber at NGCP. + +=head1 USAGE + +ngcp-create_subscriber -host 1.2.3.4 -customer_id 4 -d test.example.org -u test + -p passw12_ -c 34 -a 11 -n 12345 -s 0 + +=head1 REQUIRED ARGUMENTS + +TODO + +=head1 EXIT STATUS + +Exit code 0 means that everything should have went fine otherwise error. + +=head1 DIAGNOSTICS + +=head1 CONFIGURATION + +/etc/default/ngcp-api for default values + +=head1 DEPENDENCIES + +ngcp-create_subscriber relies on a bunch of Perl modules, all of them specified as +dependencies through the ngcp-ossbss-clients-perl Debian package. + +=head1 INCOMPATIBILITIES + +No known at this time. + +=head1 BUGS AND LIMITATIONS + +Please report problems you notice to the Sipwise +Development Team . + +=head1 AUTHOR + +Victor Seva + +=head1 LICENSE + +Copyright (c) 2015 Sipwise GmbH, Austria. +All rights reserved. You may not copy, distribute +or modify without prior written permission from +Sipwise GmbH, Austria. + +=head1 LICENSE AND COPYRIGHT + +Copyright (c) 2015 Sipwise GmbH, Austria. +You should have received a copy of the licence terms together with the +software. + +=cut diff --git a/bin/ngcp-delete_domain b/bin/ngcp-delete_domain index 7ff1299..239b14a 100755 --- a/bin/ngcp-delete_domain +++ b/bin/ngcp-delete_domain @@ -1,100 +1,195 @@ #!/usr/bin/perl use strict; use warnings; - -use Getopt::Std; -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"; +use Config::Tiny; +use English; +use Getopt::Long; +use JSON qw(); +use LWP::UserAgent; +use Pod::Usage; + +my $config = Config::Tiny->read('/etc/default/ngcp-api'); +my $opts = { + reseller_id => 1, + host => '127.0.0.1', + port => 1443, + auth_user => 'administrator', + auth_pwd => 'administrator', + verbose => 0 +}; + +if ($config) { + $opts->{host} = $config->{_}->{NGCP_API_IP}; + $opts->{port} = $config->{_}->{NGCP_API_PORT}; } -die usage() unless ($#ARGV == 0); +GetOptions( $opts, + "help|h", + "reseller_id=i", + "host=s", + "port=i", + "auth_user=s", + "auth_pwd=s", + "verbose", + "man" => sub { pod2usage(-exitval => 0, -verbose => 2); } +) or pod2usage(2); + +die pod2usage(-exitval => 1, -message => "No domain") unless ($#ARGV == 0); sub main { - my ($bprov) = @_; - - call_prov_exists( $bprov, 'get_domain', - { - domain => $ARGV[0] - } - ); - call_prov( $bprov, 'delete_domain', - { - domain => $ARGV[0] - } - ); - exit; + my $domain = shift; + my $urlbase = 'https://'.$opts->{host}.':'.$opts->{port}; + + my $ua = LWP::UserAgent->new(); + # set to 0 if using a self-signed certificate + $ua->ssl_opts(verify_hostname => 0); + $ua->credentials($opts->{host}.':'.$opts->{port}, 'api_admin_http', + $opts->{auth_user}, $opts->{auth_pwd}); + # debug!! + if($opts->{verbose}) { + $ua->show_progress(1); + $ua->add_handler("request_send", sub { shift->dump; return }); + $ua->add_handler("response_done", sub { shift->dump; return }); + } + my $domain_url = get_domain_url($ua, $urlbase, $domain); + my $res = do_request($ua, $domain_url); + if($res->is_success) { + print $res->status_line . "\n"; + } else { + die $res->as_string; + } + return; } -sub call_prov_exists { - # scalar, scalar, hash-ref - my ($bprov, $function, $parameter) = @_; - my $result; - - eval { - $result = $bprov->handle_request( $function, - { - authentication => { - type => 'system', - username => $CONFIG{admin}, - password => $CONFIG{password}, - }, - parameters => $parameter, - }); - }; - - if($@) { - if(ref $@ eq 'SOAP::Fault') { - exit 0 if($@->faultstring =~ 'unknown domain'); - die "Billing\::$function failed: ". $@->faultstring; - } else { - die "Billing\::$function failed: $@"; +sub get_domain_url { + my $ua = shift; + my $urlbase = shift; + my $domain = shift; + my $url = $urlbase."/api/domains/?domain=".$domain; + my $req = HTTP::Request->new('GET', $url); + my $domain_url; + + my $res = $ua->request($req); + if($res->is_success) { + my $collection = JSON::from_json($res->decoded_content); + if ($collection->{total_count} == 1) { + $domain_url = $urlbase . + $collection->{_links}->{'ngcp:domains'}->{href}; + } + else { + pod2usage(-exitval => 3, -message => "Domain not found"); } } - - return $result; + else { + die $res->as_string; + } + return $domain_url; } -sub call_prov { - # scalar, scalar, hash-ref - my ($bprov, $function, $parameter) = @_; - my $result; - - eval { - $result = $bprov->handle_request( $function, - { - authentication => { - type => 'system', - username => $CONFIG{admin}, - password => $CONFIG{password}, - }, - parameters => $parameter, - }); - }; - - if($@) { - if(ref $@ eq 'SOAP::Fault') { - die "Billing\::$function failed: ". $@->faultstring; - } else { - die "Billing\::$function failed: $@"; - } - } +sub do_request { + my $ua = shift; + my $url = shift; - return $result; + my $req = HTTP::Request->new('DELETE', $url); + return $ua->request($req); } -sub usage { - die "Usage:\n$0 \n". - "\ne.g.: $0 sip.sipwise.com\n"; -} +main($ARGV[0]); + +__END__ +=head1 NAME + +ngcp-delete_domain - delete a domain on NGCP + +=head1 SYNOPSIS + +ngcp-delete_domain [options] domain + +=head1 OPTIONS + +=over 8 + +=item B<-help> + +Print a brief help message and exits. + +=item B<-reseller_id> + +The reseller id to assign this domain to. Defaults to 1. + +=item B<-auth_user> + +Authentication username . Defaults to 'administrator'. + +=item B<-auth_pwd> + +Authentication password . Defaults to 'administrator'. + +=item B<-host> + +Host where the send queries. Defaults to '127.0.0.1'. + +=item B<-port> + +Port where the send queries. Defaults to 1443. + +=item B<-verbose> + +See debug information. Default false. + +=back + +=head1 DESCRIPTION + +B will delete a domain at NGCP. + +=head1 USAGE + +ngcp-delete_domain -host 1.2.3.4 -reseller_id 4 test.example.org + +=head1 REQUIRED ARGUMENTS + +B to be deleted + +=head1 EXIT STATUS + +Exit code 0 means that everything should have went fine otherwise error. + +=head1 DIAGNOSTICS + +=head1 CONFIGURATION + +/etc/default/ngcp-api for default values + +=head1 DEPENDENCIES + +ngcp-delete_domain relies on a bunch of Perl modules, all of them specified as +dependencies through the ngcp-ossbss-clients-perl Debian package. + +=head1 INCOMPATIBILITIES + +No known at this time. + +=head1 BUGS AND LIMITATIONS + +Please report problems you notice to the Sipwise +Development Team . + +=head1 AUTHOR + +Victor Seva + +=head1 LICENSE + +Copyright (c) 2015 Sipwise GmbH, Austria. +All rights reserved. You may not copy, distribute +or modify without prior written permission from +Sipwise GmbH, Austria. -my $bprov = Sipwise::Provisioning::Billing->new(); +=head1 LICENSE AND COPYRIGHT -main($bprov); +Copyright (c) 2015 Sipwise GmbH, Austria. +You should have received a copy of the licence terms together with the +software. +=cut diff --git a/bin/ngcp-get_customer b/bin/ngcp-get_customer new file mode 100755 index 0000000..d01b805 --- /dev/null +++ b/bin/ngcp-get_customer @@ -0,0 +1,225 @@ +#!/usr/bin/perl +use strict; +use warnings; +use Config::Tiny; +use English; +use Getopt::Long; +use JSON qw(); +use LWP::UserAgent; +use Pod::Usage; +use Data::Dumper; + +my $config = Config::Tiny->read('/etc/default/ngcp-api'); +my $opts = { + host => '127.0.0.1', + port => 1443, + auth_user => 'administrator', + auth_pwd => 'administrator', + verbose => 0, + admin => 0 +}; + +if ($config) { + $opts->{host} = $config->{_}->{NGCP_API_IP}; + $opts->{port} = $config->{_}->{NGCP_API_PORT}; +} + +GetOptions( $opts, + "help|h" => sub { pod2usage(-exitval =>0); }, + "host=s", + "port=i", + "auth_user=s", + "auth_pwd=s", + "verbose", + "man" => sub { pod2usage(-exitval => 0, -verbose => 2); }, + "username|u=s", + "domain|d=s", + "account_id|i=i" +) or pod2usage(2); + +die pod2usage(-exitval => 1, -message => "Missing parameters") unless + (defined $opts->{account_id} and not defined $opts->{username} and + not defined $opts->{domain}) or + (defined $opts->{username} and defined $opts->{domain} and + not defined $opts->{account_id}); + +sub main { + my $urlbase = 'https://'.$opts->{host}.':'.$opts->{port}; + my $ua = LWP::UserAgent->new(); + # set to 0 if using a self-signed certificate + $ua->ssl_opts(verify_hostname => 0); + $ua->credentials($opts->{host}.':'.$opts->{port}, 'api_admin_http', + $opts->{auth_user}, $opts->{auth_pwd}); + # debug!! + if($opts->{verbose}) { + $ua->show_progress(1); + $ua->add_handler("request_send", sub { shift->dump; return }); + $ua->add_handler("response_done", sub { shift->dump; return }); + } + my $customer_url; + if ($opts->{account_id}) { + $customer_url = $urlbase . '/api/customers/'.$opts->{account_id}; + } + else { + $customer_url= get_customer_url($ua, $urlbase); + } + my $res = do_request($ua, $customer_url); + if($res->is_success) { + # use no indentation/linebreaks, for syslog logging + $Data::Dumper::Indent = 1; + # don't print useless variable names + $Data::Dumper::Terse = 1; + # sort hash keys, so parameters always have the same order + $Data::Dumper::Sortkeys = 1; + my $collection = JSON::from_json($res->decoded_content); + print "VoIP account information:\n", Dumper $collection; + } else { + die $res->as_string; + } + return; +} + +sub get_customer_url { + my $ua = shift; + my $urlbase = shift; + my $url = $urlbase."/api/subscribers/?domain=".$opts->{domain}. + '&username='.$opts->{username}; + my $req = HTTP::Request->new('GET', $url); + my $customer_url; + + my $res = $ua->request($req); + if($res->is_success) { + my $collection = JSON::from_json($res->decoded_content); + if ($collection->{total_count} == 1) { + my $lnks = $collection->{_embedded}->{'ngcp:subscribers'}->{_links}; + $customer_url = $urlbase . $lnks->{'ngcp:customers'}->{href}; + } + else { + pod2usage(-exitval => 3, -message => "customer not found"); + } + } + else { + die $res->as_string; + } + return $customer_url; +} + +sub do_request { + my $ua = shift; + my $url = shift; + my $req = HTTP::Request->new('GET', $url); + $req->header('Content-Type' => 'application/json'); + $req->header('Prefer' => 'return=representation'); + + return $ua->request($req); +} + +main(); + +__END__ +=head1 NAME + +ngcp-get_customer - show info of a customer on NGCP + +=head1 SYNOPSIS + +ngcp-get_customer [options] + +=head1 OPTIONS + +=over 8 + +=item B<-help> + +Print a brief help message and exits. + +=item B<-auth_user> + +Authentication username . Defaults to 'administrator'. + +=item B<-auth_pwd> + +Authentication password . Defaults to 'administrator'. + +=item B<-host> + +Host where the send queries. Defaults to '127.0.0.1'. + +=item B<-port> + +Port where the send queries. Defaults to 1443. + +=item B<-account_id|i> + +the unique ID of an existing account. + +=item B<-username|u> + +SIP username. + +=item B<-domain|d> + +existing domain for subscriber. + +=item B<-verbose> + +See debug information. Default false. + +=back + +=head1 DESCRIPTION + +B will show info of a customer at NGCP. + +=head1 USAGE + +ngcp-get_customer -host 1.2.3.4 -d test.example.org -u test + +ngcp-get_customer -host 1.2.3.4 -i 45 + +=head1 REQUIRED ARGUMENTS + +B and B or B + +=head1 EXIT STATUS + +Exit code 0 means that everything should have went fine otherwise error. + +=head1 DIAGNOSTICS + +=head1 CONFIGURATION + +/etc/default/ngcp-api for default values + +=head1 DEPENDENCIES + +ngcp-get_customer relies on a bunch of Perl modules, all of them specified as +dependencies through the ngcp-ossbss-clients-perl Debian package. + +=head1 INCOMPATIBILITIES + +No known at this time. + +=head1 BUGS AND LIMITATIONS + +Please report problems you notice to the Sipwise +Development Team . + +=head1 AUTHOR + +Victor Seva + +=head1 LICENSE + +Copyright (c) 2015 Sipwise GmbH, Austria. +All rights reserved. You may not copy, distribute +or modify without prior written permission from +Sipwise GmbH, Austria. + +=head1 LICENSE AND COPYRIGHT + +Copyright (c) 2015 Sipwise GmbH, Austria. +You should have received a copy of the licence terms together with the +software. + +=cut diff --git a/bin/ngcp-terminate_customer b/bin/ngcp-terminate_customer new file mode 100755 index 0000000..71969f1 --- /dev/null +++ b/bin/ngcp-terminate_customer @@ -0,0 +1,233 @@ +#!/usr/bin/perl +use strict; +use warnings; +use Config::Tiny; +use English; +use Getopt::Long; +use JSON qw(); +use LWP::UserAgent; +use Pod::Usage; + +my $config = Config::Tiny->read('/etc/default/ngcp-api'); +my $opts = { + host => '127.0.0.1', + port => 1443, + auth_user => 'administrator', + auth_pwd => 'administrator', + verbose => 0, + admin => 0 +}; + +if ($config) { + $opts->{host} = $config->{_}->{NGCP_API_IP}; + $opts->{port} = $config->{_}->{NGCP_API_PORT}; +} + +GetOptions( $opts, + "help|h" => sub { pod2usage(-exitval =>0); }, + "host=s", + "port=i", + "auth_user=s", + "auth_pwd=s", + "verbose", + "man" => sub { pod2usage(-exitval => 0, -verbose => 2); }, + "username|u=s", + "domain|d=s", + "account_id|i=i" +) or pod2usage(2); + +die pod2usage(-exitval => 1, -message => "Missing parameters") unless + (defined $opts->{account_id} and not defined $opts->{username} and + not defined $opts->{domain}) or + (defined $opts->{username} and defined $opts->{domain} and + not defined $opts->{account_id}); + +sub main { + my $urlbase = 'https://'.$opts->{host}.':'.$opts->{port}; + my $ua = LWP::UserAgent->new(); + # set to 0 if using a self-signed certificate + $ua->ssl_opts(verify_hostname => 0); + $ua->credentials($opts->{host}.':'.$opts->{port}, 'api_admin_http', + $opts->{auth_user}, $opts->{auth_pwd}); + # debug!! + if($opts->{verbose}) { + $ua->show_progress(1); + $ua->add_handler("request_send", sub { shift->dump; return }); + $ua->add_handler("response_done", sub { shift->dump; return }); + } + my $customer_url; + if ($opts->{account_id}) { + $customer_url = $urlbase . '/api/customers/'.$opts->{account_id}; + } + else { + $customer_url= get_customer_url($ua, $urlbase); + } + my $res = do_request($ua, $customer_url); + if($res->is_success) { + print $res->status_line . "\n"; + } else { + die $res->as_string; + } + return; +} + +sub get_customer_url { + my $ua = shift; + my $urlbase = shift; + my $url = $urlbase."/api/subscribers/?domain=".$opts->{domain}. + '&username='.$opts->{username}; + my $req = HTTP::Request->new('GET', $url); + my $customer_url; + + my $res = $ua->request($req); + if($res->is_success) { + my $collection = JSON::from_json($res->decoded_content); + if ($collection->{total_count} == 1) { + $customer_url = $urlbase . + $collection->{_links}->{'ngcp:customers'}->{href}; + } + else { + pod2usage(-exitval => 3, -message => "customer not found"); + } + } + else { + die $res->as_string; + } + return $customer_url; +} + +sub do_request { + my $ua = shift; + my $url = shift; + my $data = { + status => 'terminated', + contact_id => 0, + billing_profile_id => 0 + }; + my $req = HTTP::Request->new('GET', $url); + my $res = $ua->request($req); + if($res->is_success) { + my $collection = JSON::from_json($res->decoded_content); + $data->{contact_id} = $collection->{contact_id}; + $data->{billing_profile_id} = $collection->{billing_profile_id}; + } + else { + die $res->as_string; + } + $req = HTTP::Request->new('PUT', $url); + $req->header('Content-Type' => 'application/json'); + $req->header('Prefer' => 'return=minimal'); + $req->content(JSON::to_json($data)); + + return $ua->request($req); +} + +main(); + +__END__ +=head1 NAME + +ngcp-terminate_customer - terminates a customer on NGCP + +=head1 SYNOPSIS + +ngcp-terminate_customer [options] + +=head1 OPTIONS + +=over 8 + +=item B<-help> + +Print a brief help message and exits. + +=item B<-auth_user> + +Authentication username . Defaults to 'administrator'. + +=item B<-auth_pwd> + +Authentication password . Defaults to 'administrator'. + +=item B<-host> + +Host where the send queries. Defaults to '127.0.0.1'. + +=item B<-port> + +Port where the send queries. Defaults to 1443. + +=item B<-account_id|i> + +the unique ID of an existing account. + +=item B<-username|u> + +SIP username. + +=item B<-domain|d> + +existing domain for subscriber. + +=item B<-verbose> + +See debug information. Default false. + +=back + +=head1 DESCRIPTION + +B will terminate a customer at NGCP. + +=head1 USAGE + +ngcp-terminate_customer -host 1.2.3.4 -d test.example.org -u test + +ngcp-terminate_customer -host 1.2.3.4 -i 45 + +=head1 REQUIRED ARGUMENTS + +B and B or B + +=head1 EXIT STATUS + +Exit code 0 means that everything should have went fine otherwise error. + +=head1 DIAGNOSTICS + +=head1 CONFIGURATION + +/etc/default/ngcp-api for default values + +=head1 DEPENDENCIES + +ngcp-terminate_customer relies on a bunch of Perl modules, all of them specified as +dependencies through the ngcp-ossbss-clients-perl Debian package. + +=head1 INCOMPATIBILITIES + +No known at this time. + +=head1 BUGS AND LIMITATIONS + +Please report problems you notice to the Sipwise +Development Team . + +=head1 AUTHOR + +Victor Seva + +=head1 LICENSE + +Copyright (c) 2015 Sipwise GmbH, Austria. +All rights reserved. You may not copy, distribute +or modify without prior written permission from +Sipwise GmbH, Austria. + +=head1 LICENSE AND COPYRIGHT + +Copyright (c) 2015 Sipwise GmbH, Austria. +You should have received a copy of the licence terms together with the +software. + +=cut diff --git a/bin/ngcp-terminate_subscriber b/bin/ngcp-terminate_subscriber index db0684a..84032b1 100755 --- a/bin/ngcp-terminate_subscriber +++ b/bin/ngcp-terminate_subscriber @@ -1,90 +1,237 @@ #!/usr/bin/perl use strict; +use warnings; +use Config::Tiny; +use English; +use Getopt::Long; +use JSON qw(); +use LWP::UserAgent; +use Pod::Usage; + +my $config = Config::Tiny->read('/etc/default/ngcp-api'); +my $opts = { + host => '127.0.0.1', + port => 1443, + auth_user => 'administrator', + auth_pwd => 'administrator', + verbose => 0, + admin => 0 +}; + +if ($config) { + $opts->{host} = $config->{_}->{NGCP_API_IP}; + $opts->{port} = $config->{_}->{NGCP_API_PORT}; +} + +GetOptions( $opts, + "help|h", + "host=s", + "port=i", + "auth_user=s", + "auth_pwd=s", + "verbose", + "man" => sub { pod2usage(-exitval => 0, -verbose => 2); }, + "username|u=s", + "domain|d=s", + "account_id|i=i" +) or pod2usage(2); + +die pod2usage(-exitval => 1, -message => "Missing parameters") unless + (defined $opts->{account_id} and not defined $opts->{username} and + not defined $opts->{domain}) or + (defined $opts->{username} and defined $opts->{domain} and + not defined $opts->{account_id}); -use Getopt::Std; -use Sipwise::Provisioning::Billing; -use Sipwise::Provisioning::Config; +sub main { + my $urlbase = 'https://'.$opts->{host}.':'.$opts->{port}; + my $ua = LWP::UserAgent->new(); + # set to 0 if using a self-signed certificate + $ua->ssl_opts(verify_hostname => 0); + $ua->credentials($opts->{host}.':'.$opts->{port}, 'api_admin_http', + $opts->{auth_user}, $opts->{auth_pwd}); + # debug!! + if($opts->{verbose}) { + $ua->show_progress(1); + $ua->add_handler("request_send", sub { shift->dump; return }); + $ua->add_handler("response_done", sub { shift->dump; return }); + } + my $subscriber_url; + if ($opts->{account_id}) { + $subscriber_url = $urlbase . '/api/customers/'.$opts->{account_id}; + } + else { + $subscriber_url= get_subscriber_url($ua, $urlbase); + } + my $res = do_request($ua, $subscriber_url); + if($res->is_success) { + print $res->status_line . "\n"; + } else { + die $res->as_string; + } + return; +} -my %CONFIG = (admin => 'cmd'); +sub get_subscriber_url { + my $ua = shift; + my $urlbase = shift; + my $url = $urlbase."/api/subscribers/?domain=".$opts->{domain}. + '&username='.$opts->{username}; + my $req = HTTP::Request->new('GET', $url); + my $subscriber_url; + + my $res = $ua->request($req); + if($res->is_success) { + my $collection = JSON::from_json($res->decoded_content); + if ($collection->{total_count} == 1) { + $subscriber_url = $urlbase . + $collection->{_links}->{'ngcp:subscribers'}->{href}; + } + else { + pod2usage(-exitval => 3, -message => "subscriber not found"); + } + } + else { + die $res->as_string; + } + return $subscriber_url; +} -my $config = Sipwise::Provisioning::Config->new()->get_config(); +sub do_request { + my $ua = shift; + my $url = shift; + my $data = { + status => 'terminated', + domain_id => 0, + customer_id => 0, + username => '', + password => '' + }; + my $req = HTTP::Request->new('GET', $url); + my $res = $ua->request($req); + if($res->is_success) { + my $collection = JSON::from_json($res->decoded_content); + $data->{domain_id} = $collection->{domain_id}; + $data->{customer_id} = $collection->{customer_id}; + $data->{username} = $collection->{username}; + $data->{password} = $collection->{password}; + } + else { + die $res->as_string; + } + $req = HTTP::Request->new('PUT', $url); + $req->header('Content-Type' => 'application/json'); + $req->header('Prefer' => 'return=minimal'); + $req->content(JSON::to_json($data)); -unless ($CONFIG{password} = $config->{acl}->{$CONFIG{admin}}->{password}) { - die "Error: No provisioning password found for user $CONFIG{admin}\n"; + return $ua->request($req); } -sub main; -sub usage; -sub call_prov; +main(); -my %opts; -getopts('i:u:d:', \%opts); +__END__ +=head1 NAME -die usage() unless (defined $opts{i} and !defined $opts{u} and !defined $opts{d}) - or (defined $opts{u} and defined $opts{d} and !defined $opts{i}); +ngcp-terminate_subscriber - terminates a subscriber on NGCP -my $bprov = Sipwise::Provisioning::Billing->new(); -my $vprov = Sipwise::Provisioning::Voip->new(); +=head1 SYNOPSIS -main; +ngcp-terminate_subscriber [options] -sub main { - my $subscriber = defined $opts{i} - ? call_prov('get_subscriber_by_id', { subscriber_id => $opts{i} }, $vprov) - : call_prov('get_subscriber', { - username => $opts{u}, - domain => $opts{d} - }, $vprov); - - call_prov( 'terminate_voip_account_subscriber', - { - id => $$subscriber{account_id}, - username => $$subscriber{username}, - domain => $$subscriber{domain}, - }, - $bprov, - ); - - print "Terminated Voip account subscriber.\n"; - - exit; -} +=head1 OPTIONS +=over 8 -sub call_prov { - # scalar, hash-ref - my ($function, $parameter, $backend) = @_; - my $result; - - eval { - $result = $backend->handle_request( $function, - { - authentication => { - type => 'system', - username => $CONFIG{admin}, - password => $CONFIG{password}, - }, - parameters => $parameter, - }); - }; +=item B<-help> - if($@) { - if(ref $@ eq 'SOAP::Fault') { - die "Billing\::$function failed: ". $@->faultstring ."\n"; - } else { - die "Billing\::$function failed: $@\n"; - } - } +Print a brief help message and exits. - return $result; -} +=item B<-auth_user> -sub usage { - die "Usage:\n $0 -i \nor:\n $0 -u -d \n\n". - "e.g.: $0 -u test -d sip.sipwise.com\n\n". - "Options:\n". - " -i the numeric ID of the subscriber\n". - " -u the local-part of the subscriber's SIP URI\n". - " -d the domain-part of the subscriber's SIP URI\n". - ""; -} +Authentication username . Defaults to 'administrator'. + +=item B<-auth_pwd> + +Authentication password . Defaults to 'administrator'. + +=item B<-host> + +Host where the send queries. Defaults to '127.0.0.1'. + +=item B<-port> + +Port where the send queries. Defaults to 1443. + +=item B<-account_id|i> + +the unique ID of an existing account. + +=item B<-username|u> + +SIP username. + +=item B<-domain|d> + +existing domain for subscriber. + +=item B<-verbose> + +See debug information. Default false. + +=back + +=head1 DESCRIPTION + +B will terminate a subscriber at NGCP. + +=head1 USAGE + +ngcp-terminate_subscriber -host 1.2.3.4 -d test.example.org -u test + +ngcp-terminate_subscriber -host 1.2.3.4 -i 45 + +=head1 REQUIRED ARGUMENTS + +B and B or B + +=head1 EXIT STATUS + +Exit code 0 means that everything should have went fine otherwise error. + +=head1 DIAGNOSTICS + +=head1 CONFIGURATION + +/etc/default/ngcp-api for default values + +=head1 DEPENDENCIES + +ngcp-terminate_subscriber relies on a bunch of Perl modules, all of them specified as +dependencies through the ngcp-ossbss-clients-perl Debian package. + +=head1 INCOMPATIBILITIES + +No known at this time. + +=head1 BUGS AND LIMITATIONS + +Please report problems you notice to the Sipwise +Development Team . + +=head1 AUTHOR + +Victor Seva + +=head1 LICENSE + +Copyright (c) 2015 Sipwise GmbH, Austria. +All rights reserved. You may not copy, distribute +or modify without prior written permission from +Sipwise GmbH, Austria. + +=head1 LICENSE AND COPYRIGHT + +Copyright (c) 2015 Sipwise GmbH, Austria. +You should have received a copy of the licence terms together with the +software. + +=cut