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 my $domain = $self->option('domain');
89 my $countrycode = $svc_phone->countrycode;
90 my $phonenum = $svc_phone->phonenum;
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->option('domain');
100 my $countrycode = $svc_phone->countrycode;
101 my $phonenum = $svc_phone->phonenum;
103 "sip:$countrycode$phonenum@$domain";
107 my($self, $svc_phone) = (shift, shift);
109 my $countrycode = $svc_phone->countrycode;
110 my $phonenum = $svc_phone->phonenum;
112 "/dialplans/DID+Table/dialplan_config/sip:$countrycode$phonenum@*"
116 my($self, $svc_phone, $phone_device ) = (shift, shift, shift);
118 #my $countrycode = $svc_phone->countrycode;
119 #my $phonenum = $svc_phone->phonenum;
121 "/phones_config/". $phone_device->mac_addr;
124 sub ns_create_or_update {
125 my($self, $svc_phone, $dial_policy) = (shift, shift, shift);
127 my $domain = $self->option('domain');
128 my $countrycode = $svc_phone->countrycode;
129 my $phonenum = $svc_phone->phonenum;
131 my( $firstname, $lastname );
132 if ( $svc_phone->phone_name =~ /^\s*(\S+)\s+(\S.*\S)\s*$/ ) {
136 #deal w/unaudited netsapiens services?
137 my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
138 $firstname = $cust_main->get('first');
139 $lastname = $cust_main->get('last');
142 # Piece 1 (already done) - User creation
144 my $ns = $self->ns_command( 'PUT', $self->ns_subscriber($svc_phone),
145 'subscriber_login' => $phonenum.'@'.$domain,
146 'firstname' => $firstname,
147 'lastname' => $lastname,
148 'subscriber_pin' => $svc_phone->pin,
149 'dial_plan' => 'Default', #config?
150 'dial_policy' => $dial_policy,
153 if ( $ns->responseCode !~ /^2/ ) {
154 return $ns->responseCode. ' '.
155 join(', ', $self->ns_parse_response( $ns->responseContent ) );
158 #Piece 2 - sip device creation
160 my $ns2 = $self->ns_command( 'PUT', $self->ns_registrar($svc_phone),
163 if ( $ns2->responseCode !~ /^2/ ) {
164 return $ns2->responseCode. ' '.
165 join(', ', $self->ns_parse_response( $ns2->responseContent ) );
168 #Piece 3 - DID mapping to user
170 my $ns3 = $self->ns_command( 'PUT', $self->ns_dialplan($svc_phone),
171 'to_user' => $countrycode.$phonenum,
172 'to_host' => $domain,
175 if ( $ns3->responseCode !~ /^2/ ) {
176 return $ns3->responseCode. ' '.
177 join(', ', $self->ns_parse_response( $ns3->responseContent ) );
184 my($self, $svc_phone) = (shift, shift);
186 my $ns = $self->ns_command( 'DELETE', $self->ns_subscriber($svc_phone) );
188 #delete other things?
190 if ( $ns->responseCode !~ /^2/ ) {
191 return $ns->responseCode. ' '.
192 join(', ', $self->ns_parse_response( $ns->responseContent ) );
199 sub ns_parse_response {
200 my( $self, $content ) = ( shift, shift );
202 #try to screen-scrape something useful
203 tie my %hash, Tie::IxHash;
204 while ( $content =~ s/^.*?<p>\s*<b>(.+?)<\/b>\s*(.+?)\s*<\/p>//is ) {
205 ( $hash{$1} = $2 ) =~ s/^\s*<(\w+)>(.+?)<\/\1>/$2/is;
212 my($self, $svc_phone) = (shift, shift);
213 $self->ns_create_or_update($svc_phone, 'Permit All');
216 sub _export_replace {
217 my( $self, $new, $old ) = (shift, shift, shift);
218 return "can't change phonenum with NetSapiens (unprovision and reprovision?)"
219 if $old->phonenum ne $new->phonenum;
220 $self->_export_insert($new);
224 my( $self, $svc_phone ) = (shift, shift);
226 $self->ns_delete($svc_phone);
229 sub _export_suspend {
230 my( $self, $svc_phone ) = (shift, shift);
231 $self->ns_create_or_update($svc_phone, 'Deny');
234 sub _export_unsuspend {
235 my( $self, $svc_phone ) = (shift, shift);
236 #$self->ns_create_or_update($svc_phone, 'Permit All');
237 $self->_export_insert($svc_phone);
240 sub export_device_insert {
241 my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
243 #my $domain = $self->option('domain');
244 my $countrycode = $svc_phone->countrycode;
245 my $phonenum = $svc_phone->phonenum;
247 my $device = $self->ns_devicename($svc_phone);
249 my $ns = $self->ns_device_command(
250 'PUT', $self->ns_device($svc_phone, $phone_device),
251 'line1_enable' => 'yes',
252 'device1' => $self->ns_devicename($svc_phone),
253 'line1_ext' => $countrycode.$phonenum,
255 #'line2_enable' => 'yes',
261 'domain' => $self->option('domain'),
263 'brand' => $phone_device->part_device->devicename,
267 if ( $ns->responseCode !~ /^2/ ) {
268 return $ns->responseCode. ' '.
269 join(', ', $self->ns_parse_response( $ns->responseContent ) );
276 sub export_device_delete {
277 my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
279 my $ns = $self->ns_device_command(
280 'DELETE', $self->ns_device($svc_phone, $phone_device),
283 if ( $ns->responseCode !~ /^2/ ) {
284 return $ns->responseCode. ' '.
285 join(', ', $self->ns_parse_response( $ns->responseContent ) );
293 sub export_device_replace {
294 my( $self, $svc_phone, $new_phone_device, $old_phone_device ) =
295 (shift, shift, shift, shift);
298 $self->export_device_insert( $svc_phone, $new_phone_device );
303 my($self, $svc_phone, $arrayref) = (shift, shift, shift);
304 #push @$arrayref, qq!<A HREF="http://example.com/~!. $svc_phone->username.
305 # qq!">!. $svc_phone->username. qq!</A>!;