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'). 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);
78 my $domain = $self->option('domain');
79 my $phonenum = $svc_phone->phonenum;
81 "/domains_config/$domain/subscriber_config/$phonenum";
85 my($self, $svc_phone) = (shift, shift);
87 $self->ns_subscriber($svc_phone).
88 '/registrar_config/'. $self->ns_devicename($svc_phone);
92 my( $self, $svc_phone ) = (shift, shift);
94 my $domain = $self->option('domain');
95 #my $countrycode = $svc_phone->countrycode;
96 my $phonenum = $svc_phone->phonenum;
98 #"sip:$countrycode$phonenum\@$domain";
99 "sip:$phonenum\@$domain";
103 my($self, $svc_phone) = (shift, shift);
105 #my $countrycode = $svc_phone->countrycode;
106 my $phonenum = $svc_phone->phonenum;
108 #"/dialplans/DID+Table/dialplan_config/sip:$countrycode$phonenum\@*"
109 "/dialplans/DID+Table/dialplan_config/sip:$phonenum\@*"
113 my($self, $svc_phone, $phone_device ) = (shift, shift, shift);
115 #my $countrycode = $svc_phone->countrycode;
116 #my $phonenum = $svc_phone->phonenum;
118 "/phones_config/". lc($phone_device->mac_addr);
121 sub ns_create_or_update {
122 my($self, $svc_phone, $dial_policy) = (shift, shift, shift);
124 my $domain = $self->option('domain');
125 #my $countrycode = $svc_phone->countrycode;
126 my $phonenum = $svc_phone->phonenum;
128 my( $firstname, $lastname );
129 if ( $svc_phone->phone_name =~ /^\s*(\S+)\s+(\S.*\S)\s*$/ ) {
133 #deal w/unaudited netsapiens services?
134 my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
135 $firstname = $cust_main->get('first');
136 $lastname = $cust_main->get('last');
139 # Piece 1 (already done) - User creation
141 my $ns = $self->ns_command( 'PUT', $self->ns_subscriber($svc_phone),
142 'subscriber_login' => $phonenum.'@'.$domain,
143 'firstname' => $firstname,
144 'lastname' => $lastname,
145 'subscriber_pin' => $svc_phone->pin,
146 'dial_plan' => 'Default', #config?
147 'dial_policy' => $dial_policy,
150 if ( $ns->responseCode !~ /^2/ ) {
151 return $ns->responseCode. ' '.
152 join(', ', $self->ns_parse_response( $ns->responseContent ) );
155 #Piece 2 - sip device creation
157 my $ns2 = $self->ns_command( 'PUT', $self->ns_registrar($svc_phone),
158 'termination_match' => $self->ns_devicename($svc_phone)
161 if ( $ns2->responseCode !~ /^2/ ) {
162 return $ns2->responseCode. ' '.
163 join(', ', $self->ns_parse_response( $ns2->responseContent ) );
166 #Piece 3 - DID mapping to user
168 my $ns3 = $self->ns_command( 'PUT', $self->ns_dialplan($svc_phone),
169 'to_user' => $phonenum,
170 'to_host' => $domain,
173 if ( $ns3->responseCode !~ /^2/ ) {
174 return $ns3->responseCode. ' '.
175 join(', ', $self->ns_parse_response( $ns3->responseContent ) );
182 my($self, $svc_phone) = (shift, shift);
184 my $ns = $self->ns_command( 'DELETE', $self->ns_subscriber($svc_phone) );
186 #delete other things?
188 if ( $ns->responseCode !~ /^2/ ) {
189 return $ns->responseCode. ' '.
190 join(', ', $self->ns_parse_response( $ns->responseContent ) );
197 sub ns_parse_response {
198 my( $self, $content ) = ( shift, shift );
200 #try to screen-scrape something useful
201 tie my %hash, Tie::IxHash;
202 while ( $content =~ s/^.*?<p>\s*<b>(.+?)<\/b>\s*(.+?)\s*<\/p>//is ) {
203 ( $hash{$1} = $2 ) =~ s/^\s*<(\w+)>(.+?)<\/\1>/$2/is;
210 my($self, $svc_phone) = (shift, shift);
211 $self->ns_create_or_update($svc_phone, 'Permit All');
214 sub _export_replace {
215 my( $self, $new, $old ) = (shift, shift, shift);
216 return "can't change phonenum with NetSapiens (unprovision and reprovision?)"
217 if $old->phonenum ne $new->phonenum;
218 $self->_export_insert($new);
222 my( $self, $svc_phone ) = (shift, shift);
224 $self->ns_delete($svc_phone);
227 sub _export_suspend {
228 my( $self, $svc_phone ) = (shift, shift);
229 $self->ns_create_or_update($svc_phone, 'Deny');
232 sub _export_unsuspend {
233 my( $self, $svc_phone ) = (shift, shift);
234 #$self->ns_create_or_update($svc_phone, 'Permit All');
235 $self->_export_insert($svc_phone);
238 sub export_device_insert {
239 my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
241 #my $domain = $self->option('domain');
242 my $countrycode = $svc_phone->countrycode;
243 my $phonenum = $svc_phone->phonenum;
245 my $device = $self->ns_devicename($svc_phone);
247 my $ns = $self->ns_device_command(
248 'PUT', $self->ns_device($svc_phone, $phone_device),
249 'line1_enable' => 'yes',
250 'device1' => $self->ns_devicename($svc_phone),
251 'line1_ext' => $phonenum,
253 #'line2_enable' => 'yes',
259 'domain' => $self->option('domain'),
261 'brand' => $phone_device->part_device->devicename,
265 if ( $ns->responseCode !~ /^2/ ) {
266 return $ns->responseCode. ' '.
267 join(', ', $self->ns_parse_response( $ns->responseContent ) );
274 sub export_device_delete {
275 my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
277 my $ns = $self->ns_device_command(
278 'DELETE', $self->ns_device($svc_phone, $phone_device),
281 if ( $ns->responseCode !~ /^2/ ) {
282 return $ns->responseCode. ' '.
283 join(', ', $self->ns_parse_response( $ns->responseContent ) );
291 sub export_device_replace {
292 my( $self, $svc_phone, $new_phone_device, $old_phone_device ) =
293 (shift, shift, shift, shift);
296 $self->export_device_insert( $svc_phone, $new_phone_device );
301 my($self, $svc_phone, $arrayref) = (shift, shift, shift);
302 #push @$arrayref, qq!<A HREF="http://example.com/~!. $svc_phone->username.
303 # qq!">!. $svc_phone->username. qq!</A>!;