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' },
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').
65 " $command ". join(', ', @_). "\n"
66 if $self->option('debug');
68 my $auth = encode_base64( $self->option($prefix.'login'). ':'.
69 $self->option($prefix.'password') );
70 push @args, { 'Authorization' => "Basic $auth" };
72 $ns->$method( @args );
77 my($self, $svc_phone) = (shift, shift);
79 my $domain = $self->option('domain');
80 my $phonenum = $svc_phone->phonenum;
82 "/domains_config/$domain/subscriber_config/$phonenum";
86 my($self, $svc_phone) = (shift, shift);
88 $self->ns_subscriber($svc_phone).
89 '/registrar_config/'. $self->ns_devicename($svc_phone);
93 my( $self, $svc_phone ) = (shift, shift);
95 my $domain = $self->option('domain');
96 #my $countrycode = $svc_phone->countrycode;
97 my $phonenum = $svc_phone->phonenum;
99 #"sip:$countrycode$phonenum\@$domain";
100 "sip:$phonenum\@$domain";
104 my($self, $svc_phone) = (shift, shift);
106 #my $countrycode = $svc_phone->countrycode;
107 my $phonenum = $svc_phone->phonenum;
109 #"/dialplans/DID+Table/dialplan_config/sip:$countrycode$phonenum\@*"
110 "/dialplans/DID+Table/dialplan_config/sip:$phonenum\@*"
114 my($self, $svc_phone, $phone_device ) = (shift, shift, shift);
116 #my $countrycode = $svc_phone->countrycode;
117 #my $phonenum = $svc_phone->phonenum;
119 "/phones_config/". lc($phone_device->mac_addr);
122 sub ns_create_or_update {
123 my($self, $svc_phone, $dial_policy) = (shift, shift, shift);
125 my $domain = $self->option('domain');
126 #my $countrycode = $svc_phone->countrycode;
127 my $phonenum = $svc_phone->phonenum;
129 my( $firstname, $lastname );
130 if ( $svc_phone->phone_name =~ /^\s*(\S+)\s+(\S.*\S)\s*$/ ) {
134 #deal w/unaudited netsapiens services?
135 my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
136 $firstname = $cust_main->get('first');
137 $lastname = $cust_main->get('last');
140 # Piece 1 (already done) - User creation
142 my $ns = $self->ns_command( 'PUT', $self->ns_subscriber($svc_phone),
143 'subscriber_login' => $phonenum.'@'.$domain,
144 'firstname' => $firstname,
145 'lastname' => $lastname,
146 'subscriber_pin' => $svc_phone->pin,
147 'dial_plan' => 'Default', #config?
148 'dial_policy' => $dial_policy,
151 if ( $ns->responseCode !~ /^2/ ) {
152 return $ns->responseCode. ' '.
153 join(', ', $self->ns_parse_response( $ns->responseContent ) );
156 #Piece 2 - sip device creation
158 my $ns2 = $self->ns_command( 'PUT', $self->ns_registrar($svc_phone),
159 'termination_match' => $self->ns_devicename($svc_phone)
162 if ( $ns2->responseCode !~ /^2/ ) {
163 return $ns2->responseCode. ' '.
164 join(', ', $self->ns_parse_response( $ns2->responseContent ) );
167 #Piece 3 - DID mapping to user
169 my $ns3 = $self->ns_command( 'PUT', $self->ns_dialplan($svc_phone),
170 'to_user' => $phonenum,
171 'to_host' => $domain,
174 if ( $ns3->responseCode !~ /^2/ ) {
175 return $ns3->responseCode. ' '.
176 join(', ', $self->ns_parse_response( $ns3->responseContent ) );
183 my($self, $svc_phone) = (shift, shift);
185 my $ns = $self->ns_command( 'DELETE', $self->ns_subscriber($svc_phone) );
187 #delete other things?
189 if ( $ns->responseCode !~ /^2/ ) {
190 return $ns->responseCode. ' '.
191 join(', ', $self->ns_parse_response( $ns->responseContent ) );
198 sub ns_parse_response {
199 my( $self, $content ) = ( shift, shift );
201 #try to screen-scrape something useful
202 tie my %hash, Tie::IxHash;
203 while ( $content =~ s/^.*?<p>\s*<b>(.+?)<\/b>\s*(.+?)\s*<\/p>//is ) {
204 ( $hash{$1} = $2 ) =~ s/^\s*<(\w+)>(.+?)<\/\1>/$2/is;
211 my($self, $svc_phone) = (shift, shift);
212 $self->ns_create_or_update($svc_phone, 'Permit All');
215 sub _export_replace {
216 my( $self, $new, $old ) = (shift, shift, shift);
217 return "can't change phonenum with NetSapiens (unprovision and reprovision?)"
218 if $old->phonenum ne $new->phonenum;
219 $self->_export_insert($new);
223 my( $self, $svc_phone ) = (shift, shift);
225 $self->ns_delete($svc_phone);
228 sub _export_suspend {
229 my( $self, $svc_phone ) = (shift, shift);
230 $self->ns_create_or_update($svc_phone, 'Deny');
233 sub _export_unsuspend {
234 my( $self, $svc_phone ) = (shift, shift);
235 #$self->ns_create_or_update($svc_phone, 'Permit All');
236 $self->_export_insert($svc_phone);
239 sub export_device_insert {
240 my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
242 #my $domain = $self->option('domain');
243 my $countrycode = $svc_phone->countrycode;
244 my $phonenum = $svc_phone->phonenum;
246 my $device = $self->ns_devicename($svc_phone);
248 my $ns = $self->ns_device_command(
249 'PUT', $self->ns_device($svc_phone, $phone_device),
250 'line1_enable' => 'yes',
251 'device1' => $self->ns_devicename($svc_phone),
252 'line1_ext' => $phonenum,
254 #'line2_enable' => 'yes',
260 'domain' => $self->option('domain'),
262 'brand' => $phone_device->part_device->devicename,
266 if ( $ns->responseCode !~ /^2/ ) {
267 return $ns->responseCode. ' '.
268 join(', ', $self->ns_parse_response( $ns->responseContent ) );
275 sub export_device_delete {
276 my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
278 my $ns = $self->ns_device_command(
279 'DELETE', $self->ns_device($svc_phone, $phone_device),
282 if ( $ns->responseCode !~ /^2/ ) {
283 return $ns->responseCode. ' '.
284 join(', ', $self->ns_parse_response( $ns->responseContent ) );
292 sub export_device_replace {
293 my( $self, $svc_phone, $new_phone_device, $old_phone_device ) =
294 (shift, shift, shift, shift);
297 $self->export_device_insert( $svc_phone, $new_phone_device );
302 my($self, $svc_phone, $arrayref) = (shift, shift, shift);
303 #push @$arrayref, qq!<A HREF="http://example.com/~!. $svc_phone->username.
304 # qq!">!. $svc_phone->username. qq!</A>!;