1 package FS::part_export::netsapiens;
3 use vars qw(@ISA $me %info);
9 @ISA = qw(FS::part_export);
10 $me = '[FS::part_export::netsapiens]';
12 tie my %options, 'Tie::IxHash',
13 'login' => { label=>'NetSapiens tac2 User API username' },
14 'password' => { label=>'NetSapiens tac2 User API password' },
15 'url' => { label=>'NetSapiens tac2 User URL' },
16 'device_login' => { label=>'NetSapiens tac2 Device API username' },
17 'device_password' => { label=>'NetSapiens tac2 Device API password' },
18 'device_url' => { label=>'NetSapiens tac2 Device URL' },
19 'domain' => { label=>'NetSapiens Domain' },
20 'debug' => { label=>'Enable debugging', type=>'checkbox' },
24 'svc' => [ 'svc_phone', ], # 'part_device',
25 'desc' => 'Provision phone numbers to NetSapiens',
26 'options' => \%options,
28 Requires installation of
29 <a href="http://search.cpan.org/dist/REST-Client">REST::Client</a>
34 sub rebless { shift; }
38 $self->_ns_command('', @_);
41 sub ns_device_command {
43 $self->_ns_command('device_', @_);
47 my( $self, $prefix, $method, $command ) = splice(@_,0,4);
49 eval 'use REST::Client';
52 my $ns = new REST::Client 'host'=>$self->option($prefix.'url');
54 my @args = ( $command );
56 if ( $method eq 'PUT' ) {
57 my $content = $ns->buildQuery( { @_ } );
60 } elsif ( $method eq 'GET' ) {
61 $args[0] .= $ns->buildQuery( { @_ } );
64 warn "$me $method ". $self->option($prefix.'url'). join(', ', @args). "\n"
65 if $self->option('debug');
67 my $auth = encode_base64( $self->option($prefix.'login'). ':'.
68 $self->option($prefix.'password') );
69 push @args, { 'Authorization' => "Basic $auth" };
71 $ns->$method( @args );
76 my($self, $svc_phone) = (shift, shift);
77 $svc_phone->domain || $self->option('domain');
81 my($self, $svc_phone) = (shift, shift);
83 my $domain = $self->ns_domain($svc_phone);
84 my $phonenum = $svc_phone->phonenum;
86 "/domains_config/$domain/subscriber_config/$phonenum";
90 my($self, $svc_phone) = (shift, shift);
92 $self->ns_subscriber($svc_phone).
93 '/registrar_config/'. $self->ns_devicename($svc_phone);
97 my( $self, $svc_phone ) = (shift, shift);
99 my $domain = $self->ns_domain($svc_phone);
100 #my $countrycode = $svc_phone->countrycode;
101 my $phonenum = $svc_phone->phonenum;
103 #"sip:$countrycode$phonenum\@$domain";
104 "sip:$phonenum\@$domain";
108 my($self, $svc_phone) = (shift, shift);
110 #my $countrycode = $svc_phone->countrycode;
111 my $phonenum = $svc_phone->phonenum;
113 #"/dialplans/DID+Table/dialplan_config/sip:$countrycode$phonenum\@*"
114 "/dialplans/DID+Table/dialplan_config/sip:$phonenum\@*"
118 my($self, $svc_phone, $phone_device ) = (shift, shift, shift);
120 #my $countrycode = $svc_phone->countrycode;
121 #my $phonenum = $svc_phone->phonenum;
123 "/phones_config/". lc($phone_device->mac_addr);
126 sub ns_create_or_update {
127 my($self, $svc_phone, $dial_policy) = (shift, shift, shift);
129 my $domain = $self->ns_domain($svc_phone);
130 #my $countrycode = $svc_phone->countrycode;
131 my $phonenum = $svc_phone->phonenum;
133 my( $firstname, $lastname );
134 if ( $svc_phone->phone_name =~ /^\s*(\S+)\s+(\S.*\S)\s*$/ ) {
138 #deal w/unaudited netsapiens services?
139 my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
140 $firstname = $cust_main->get('first');
141 $lastname = $cust_main->get('last');
144 # Piece 1 (already done) - User creation
146 my $ns = $self->ns_command( 'PUT', $self->ns_subscriber($svc_phone),
147 'subscriber_login' => $phonenum.'@'.$domain,
148 'firstname' => $firstname,
149 'lastname' => $lastname,
150 'subscriber_pin' => $svc_phone->pin,
151 'dial_plan' => 'Default', #config?
152 'dial_policy' => $dial_policy,
155 if ( $ns->responseCode !~ /^2/ ) {
156 return $ns->responseCode. ' '.
157 join(', ', $self->ns_parse_response( $ns->responseContent ) );
160 #Piece 2 - sip device creation
162 my $ns2 = $self->ns_command( 'PUT', $self->ns_registrar($svc_phone),
163 'termination_match' => $self->ns_devicename($svc_phone)
166 if ( $ns2->responseCode !~ /^2/ ) {
167 return $ns2->responseCode. ' '.
168 join(', ', $self->ns_parse_response( $ns2->responseContent ) );
171 #Piece 3 - DID mapping to user
173 my $ns3 = $self->ns_command( 'PUT', $self->ns_dialplan($svc_phone),
174 'to_user' => $phonenum,
175 'to_host' => $domain,
178 if ( $ns3->responseCode !~ /^2/ ) {
179 return $ns3->responseCode. ' '.
180 join(', ', $self->ns_parse_response( $ns3->responseContent ) );
187 my($self, $svc_phone) = (shift, shift);
189 my $ns = $self->ns_command( 'DELETE', $self->ns_subscriber($svc_phone) );
191 #delete other things?
193 if ( $ns->responseCode !~ /^2/ ) {
194 return $ns->responseCode. ' '.
195 join(', ', $self->ns_parse_response( $ns->responseContent ) );
202 sub ns_parse_response {
203 my( $self, $content ) = ( shift, shift );
205 #try to screen-scrape something useful
206 tie my %hash, Tie::IxHash;
207 while ( $content =~ s/^.*?<p>\s*<b>(.+?)<\/b>\s*(.+?)\s*<\/p>//is ) {
208 ( $hash{$1} = $2 ) =~ s/^\s*<(\w+)>(.+?)<\/\1>/$2/is;
215 my($self, $svc_phone) = (shift, shift);
216 $self->ns_create_or_update($svc_phone, 'Permit All');
219 sub _export_replace {
220 my( $self, $new, $old ) = (shift, shift, shift);
221 return "can't change phonenum with NetSapiens (unprovision and reprovision?)"
222 if $old->phonenum ne $new->phonenum;
223 $self->_export_insert($new);
227 my( $self, $svc_phone ) = (shift, shift);
229 $self->ns_delete($svc_phone);
232 sub _export_suspend {
233 my( $self, $svc_phone ) = (shift, shift);
234 $self->ns_create_or_update($svc_phone, 'Deny');
237 sub _export_unsuspend {
238 my( $self, $svc_phone ) = (shift, shift);
239 #$self->ns_create_or_update($svc_phone, 'Permit All');
240 $self->_export_insert($svc_phone);
243 sub export_device_insert {
244 my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
246 my $domain = $self->ns_domain($svc_phone);
247 my $countrycode = $svc_phone->countrycode;
248 my $phonenum = $svc_phone->phonenum;
250 my $device = $self->ns_devicename($svc_phone);
252 my $ns = $self->ns_device_command(
253 'PUT', $self->ns_device($svc_phone, $phone_device),
254 'line1_enable' => 'yes',
255 'device1' => $self->ns_devicename($svc_phone),
256 'line1_ext' => $phonenum,
258 #'line2_enable' => 'yes',
266 'brand' => $phone_device->part_device->devicename,
270 if ( $ns->responseCode !~ /^2/ ) {
271 return $ns->responseCode. ' '.
272 join(', ', $self->ns_parse_response( $ns->responseContent ) );
279 sub export_device_delete {
280 my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
282 my $ns = $self->ns_device_command(
283 'DELETE', $self->ns_device($svc_phone, $phone_device),
286 if ( $ns->responseCode !~ /^2/ ) {
287 return $ns->responseCode. ' '.
288 join(', ', $self->ns_parse_response( $ns->responseContent ) );
296 sub export_device_replace {
297 my( $self, $svc_phone, $new_phone_device, $old_phone_device ) =
298 (shift, shift, shift, shift);
301 $self->export_device_insert( $svc_phone, $new_phone_device );
306 my($self, $svc_phone, $arrayref) = (shift, shift, shift);
307 #push @$arrayref, qq!<A HREF="http://example.com/~!. $svc_phone->username.
308 # qq!">!. $svc_phone->username. qq!</A>!;