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 User API username' },
13 'password' => { label=>'NetSapiens tac2 User API password' },
14 'url' => { label=>'NetSapiens tac2 User URL' },
15 'device_login' => { label=>'NetSapiens tac2 Device API username' },
16 'device_password' => { label=>'NetSapiens tac2 Device API password' },
17 'device_url' => { label=>'NetSapiens tac2 Device URL' },
18 'domain' => { label=>'NetSapiens Domain' },
23 'desc' => 'Provision phone numbers to NetSapiens',
24 'options' => \%options,
26 Requires installation of
27 <a href="http://search.cpan.org/dist/REST-Client">REST::Client</a>
32 sub rebless { shift; }
36 $self->_ns_command('', @_);
39 sub ns_device_command {
41 $self->_ns_command('device', @_);
45 my( $self, $prefix, $method, $command ) = splice(@_,0,4);
47 eval 'use REST::Client';
50 my $ns = new REST::Client 'host'=>$self->option($prefix.'url');
52 my @args = ( $command );
54 if ( $method eq 'PUT' ) {
55 my $content = $ns->buildQuery( { @_ } );
58 } elsif ( $method eq 'GET' ) {
59 $args[0] .= $ns->buildQuery( { @_ } );
62 my $auth = encode_base64( $self->option($prefix.'login'). ':'.
63 $self->option($prefix.'password') );
64 push @args, { 'Authorization' => "Basic $auth" };
66 $ns->$method( @args );
71 my($self, $svc_phone) = (shift, shift);
73 my $domain = $self->option('domain');
74 my $phonenum = $svc_phone->phonenum;
76 "/domains_config/$domain/subscriber_config/$phonenum";
80 my($self, $svc_phone) = (shift, shift);
82 my $domain = $self->option('domain');
83 my $countrycode = $svc_phone->countrycode;
84 my $phonenum = $svc_phone->phonenum;
86 $self->ns_subscriber($svc_phone).
87 '/registrar_config/'. $self->ns_devicename($svc_phone);
91 my( $self, $svc_phone ) = (shift, shift);
93 my $domain = $self->option('domain');
94 my $countrycode = $svc_phone->countrycode;
95 my $phonenum = $svc_phone->phonenum;
97 "sip:$countrycode$phonenum@$domain";
101 my($self, $svc_phone) = (shift, shift);
103 my $countrycode = $svc_phone->countrycode;
104 my $phonenum = $svc_phone->phonenum;
106 "/dialplans/DID+Table/dialplan_config/sip:$countrycode$phonenum@*"
110 my($self, $svc_phone, $phone_device ) = (shift, shift, shift);
112 #my $countrycode = $svc_phone->countrycode;
113 #my $phonenum = $svc_phone->phonenum;
115 "/phones_config/". $phone_device->mac_addr;
118 sub ns_create_or_update {
119 my($self, $svc_phone, $dial_policy) = (shift, shift, shift);
121 my $domain = $self->option('domain');
122 my $countrycode = $svc_phone->countrycode;
123 my $phonenum = $svc_phone->phonenum;
125 my( $firstname, $lastname );
126 if ( $svc_phone->phone_name =~ /^\s*(\S+)\s+(\S.*\S)\s*$/ ) {
130 #deal w/unaudited netsapiens services?
131 my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
132 $firstname = $cust_main->get('first');
133 $lastname = $cust_main->get('last');
136 # Piece 1 (already done) - User creation
138 my $ns = $self->ns_command( 'PUT', $self->ns_subscriber($svc_phone),
139 'subscriber_login' => $phonenum.'@'.$domain,
140 'firstname' => $firstname,
141 'lastname' => $lastname,
142 'subscriber_pin' => $svc_phone->pin,
143 'dial_plan' => 'Default', #config?
144 'dial_policy' => $dial_policy,
147 if ( $ns->responseCode !~ /^2/ ) {
148 return $ns->responseCode. ' '.
149 join(', ', $self->ns_parse_response( $ns->responseContent ) );
152 #Piece 2 - sip device creation
154 my $ns2 = $self->ns_command( 'PUT', $self->ns_registrar($svc_phone),
157 if ( $ns2->responseCode !~ /^2/ ) {
158 return $ns2->responseCode. ' '.
159 join(', ', $self->ns_parse_response( $ns2->responseContent ) );
162 #Piece 3 - DID mapping to user
164 my $ns3 = $self->ns_command( 'PUT', $self->ns_dialplan($svc_phone),
165 'to_user' => $countrycode.$phonenum,
166 'to_host' => $domain,
169 if ( $ns3->responseCode !~ /^2/ ) {
170 return $ns3->responseCode. ' '.
171 join(', ', $self->ns_parse_response( $ns3->responseContent ) );
178 my($self, $svc_phone) = (shift, shift);
180 my $ns = $self->ns_command( 'DELETE', $self->ns_subscriber($svc_phone) );
182 #delete other things?
184 if ( $ns->responseCode !~ /^2/ ) {
185 return $ns->responseCode. ' '.
186 join(', ', $self->ns_parse_response( $ns->responseContent ) );
193 sub ns_parse_response {
194 my( $self, $content ) = ( shift, shift );
196 #try to screen-scrape something useful
197 tie my %hash, Tie::IxHash;
198 while ( $content =~ s/^.*?<p>\s*<b>(.+?)<\/b>\s*(.+?)\s*<\/p>//is ) {
199 ( $hash{$1} = $2 ) =~ s/^\s*<(\w+)>(.+?)<\/\1>/$2/is;
206 my($self, $svc_phone) = (shift, shift);
207 $self->ns_create_or_update($svc_phone, 'Permit All');
210 sub _export_replace {
211 my( $self, $new, $old ) = (shift, shift, shift);
212 return "can't change phonenum with NetSapiens (unprovision and reprovision?)"
213 if $old->phonenum ne $new->phonenum;
214 $self->_export_insert($new);
218 my( $self, $svc_phone ) = (shift, shift);
220 $self->ns_delete($svc_phone);
223 sub _export_suspend {
224 my( $self, $svc_phone ) = (shift, shift);
225 $self->ns_create_or_update($svc_phone, 'Deny');
228 sub _export_unsuspend {
229 my( $self, $svc_phone ) = (shift, shift);
230 #$self->ns_create_or_update($svc_phone, 'Permit All');
231 $self->_export_insert($svc_phone);
234 sub export_device_insert {
235 my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
237 #my $domain = $self->option('domain');
238 my $countrycode = $svc_phone->countrycode;
239 my $phonenum = $svc_phone->phonenum;
241 my $device = $self->ns_devicename($svc_phone);
243 my $ns = $self->ns_device_command(
244 'PUT', $self->ns_device($svc_phone, $phone_device),
245 'line1_enable' => 'yes',
246 'device1' => $self->ns_devicename($svc_phone),
247 'line1_ext' => $countrycode.$phonenum,
249 #'line2_enable' => 'yes',
255 'domain' => $self->option('domain'),
257 'brand' => $phone_device->part_device->devicename,
261 if ( $ns->responseCode !~ /^2/ ) {
262 return $ns->responseCode. ' '.
263 join(', ', $self->ns_parse_response( $ns->responseContent ) );
270 sub export_device_delete {
271 my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
273 my $ns = $self->ns_device_command(
274 'DELETE', $self->ns_device($svc_phone, $phone_device),
277 if ( $ns->responseCode !~ /^2/ ) {
278 return $ns->responseCode. ' '.
279 join(', ', $self->ns_parse_response( $ns->responseContent ) );
287 sub export_device_replace {
288 my( $self, $svc_phone, $new_phone_device, $old_phone_device ) =
289 (shift, shift, shift, shift);
292 $self->export_device_insert( $svc_phone, $new_phone_device );
297 my($self, $svc_phone, $arrayref) = (shift, shift, shift);
298 #push @$arrayref, qq!<A HREF="http://example.com/~!. $svc_phone->username.
299 # qq!">!. $svc_phone->username. qq!</A>!;