1 package FS::part_export::netsapiens;
3 use vars qw(@ISA %info);
9 @ISA = qw(FS::part_export);
11 tie my %options, 'Tie::IxHash',
12 'login' => { label=>'NetSapiens tac2 API username' },
13 'password' => { label=>'NetSapiens tac2 API password' },
14 'url' => { label=>'NetSapiens tac2 URL' },
15 'domain' => { label=>'NetSapiens Domain' },
20 'desc' => 'Provision phone numbers to NetSapiens',
21 'options' => \%options,
23 Requires installation of
24 <a href="http://search.cpan.org/dist/REST-Client">REST::Client</a>
29 sub rebless { shift; }
32 my( $self, $method, $command ) = splice(@_,0,3);
34 eval 'use REST::Client';
37 my $ns = new REST::Client 'host'=>$self->option('url');
39 my @args = ( $command );
41 if ( $method eq 'PUT' ) {
42 my $content = $ns->buildQuery( { @_ } );
45 } elsif ( $method eq 'GET' ) {
46 $args[0] .= $ns->buildQuery( { @_ } );
50 encode_base64( $self->option('login'). ':'. $self->option('password') );
51 push @args, { 'Authorization' => "Basic $auth" };
53 $ns->$method( @args );
58 my($self, $svc_phone) = (shift, shift);
60 my $domain = $self->option('domain');
61 my $phonenum = $svc_phone->phonenum;
63 "/domains_config/$domain/subscriber_config/$phonenum";
66 sub ns_create_or_update {
67 my($self, $svc_phone, $dial_policy) = (shift, shift, shift);
69 my $domain = $self->option('domain');
70 my $phonenum = $svc_phone->phonenum;
72 my( $firstname, $lastname );
73 if ( $svc_phone->phone_name =~ /^\s*(\S+)\s+(\S.*\S)\s*$/ ) {
77 #deal w/unaudited netsapiens services?
78 my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
79 $firstname = $cust_main->get('first');
80 $lastname = $cust_main->get('last');
83 my $ns = $self->ns_command( 'PUT', $self->ns_subscriber($svc_phone),
84 'subscriber_login' => $phonenum.'@'.$domain,
85 'firstname' => $firstname,
86 'lastname' => $lastname,
87 'subscriber_pin' => $svc_phone->pin,
88 'dial_plan' => 'Default', #config?
89 'dial_policy' => $dial_policy,
92 if ( $ns->responseCode !~ /^2/ ) {
93 return $ns->responseCode. ' '.
94 join(', ', $self->ns_parse_response( $ns->responseContent ) );
101 my($self, $svc_phone) = (shift, shift);
103 my $ns = $self->ns_command( 'DELETE', $self->ns_subscriber($svc_phone) );
105 if ( $ns->responseCode !~ /^2/ ) {
106 return $ns->responseCode. ' '.
107 join(', ', $self->ns_parse_response( $ns->responseContent ) );
114 sub ns_parse_response {
115 my( $self, $content ) = ( shift, shift );
117 #try to screen-scrape something useful
118 tie my %hash, Tie::IxHash;
119 while ( $content =~ s/^.*?<p>\s*<b>(.+?)<\/b>\s*(.+?)\s*<\/p>//is ) {
120 ( $hash{$1} = $2 ) =~ s/^\s*<(\w+)>(.+?)<\/\1>/$2/is;
127 my($self, $svc_phone) = (shift, shift);
128 $self->ns_create_or_update($svc_phone, 'Permit All');
131 sub _export_replace {
132 my( $self, $new, $old ) = (shift, shift, shift);
133 return "can't change phonenum with NetSapiens (unprovision and reprovision?)"
134 if $old->phonenum ne $new->phonenum;
135 $self->_export_insert($new);
139 my( $self, $svc_phone ) = (shift, shift);
141 $self->ns_delete($svc_phone);
144 sub _export_suspend {
145 my( $self, $svc_phone ) = (shift, shift);
146 $self->ns_create_or_udpate($svc_phone, 'Deny');
149 sub _export_unsuspend {
150 my( $self, $svc_phone ) = (shift, shift);
151 #$self->ns_create_or_update($svc_phone, 'Permit All');
152 $self->_export_insert($svc_phone);
156 my($self, $svc_phone, $arrayref) = (shift, shift, shift);
157 #push @$arrayref, qq!<A HREF="http://example.com/~!. $svc_phone->username.
158 # qq!">!. $svc_phone->username. qq!</A>!;