add DID association w/user? docs from netsapiens rough... RT#5226
[freeside.git] / FS / FS / part_export / netsapiens.pm
1 package FS::part_export::netsapiens;
2
3 use vars qw(@ISA %info);
4 use URI;
5 use MIME::Base64;
6 use Tie::IxHash;
7 use FS::part_export;
8
9 @ISA = qw(FS::part_export);
10
11 tie my %options, 'Tie::IxHash',
12   'login'         => { label=>'NetSapiens tac2 API username' },
13   'password'      => { label=>'NetSapiens tac2 API password' },
14   'url'           => { label=>'NetSapiens tac2 URL' },
15   'domain'        => { label=>'NetSapiens Domain' },
16 ;
17
18 %info = (
19   'svc'      => 'svc_phone',
20   'desc'     => 'Provision phone numbers to NetSapiens',
21   'options'  => \%options,
22   'notes'    => <<'END'
23 Requires installation of
24 <a href="http://search.cpan.org/dist/REST-Client">REST::Client</a>
25 from CPAN.
26 END
27 );
28
29 sub rebless { shift; }
30
31 sub ns_command {
32   my( $self, $method, $command ) = splice(@_,0,3);
33
34   eval 'use REST::Client';
35   die $@ if $@;
36
37   my $ns = new REST::Client 'host'=>$self->option('url');
38
39   my @args = ( $command );
40
41   if ( $method eq 'PUT' ) {
42     my $content = $ns->buildQuery( { @_ } );
43     $content =~ s/^\?//;
44     push @args, $content;
45   } elsif ( $method eq 'GET' ) {
46     $args[0] .= $ns->buildQuery( { @_ } );
47   }
48
49   my $auth =
50     encode_base64( $self->option('login'). ':'. $self->option('password') );
51   push @args, { 'Authorization' => "Basic $auth" };
52
53   $ns->$method( @args );
54   $ns;
55 }
56
57 sub ns_subscriber {
58   my($self, $svc_phone) = (shift, shift);
59
60   my $domain = $self->option('domain');
61   my $phonenum = $svc_phone->phonenum;
62
63   "/domains_config/$domain/subscriber_config/$phonenum";
64 }
65
66 sub ns_dialplan {
67   my($self, $svc_phone) = (shift, shift);
68
69   my $countrycode = $svc_phone->countrycode;
70   my $phonenum    = $svc_phone->phonenum;
71
72   "/dialplans/DID+Table/dialplan_config/sip:$countrycode$phonenum@*"
73 }
74
75 sub ns_create_or_update {
76   my($self, $svc_phone, $dial_policy) = (shift, shift, shift);
77
78   my $domain = $self->option('domain');
79   my $phonenum = $svc_phone->phonenum;
80
81   my( $firstname, $lastname );
82   if ( $svc_phone->phone_name =~ /^\s*(\S+)\s+(\S.*\S)\s*$/ ) {
83     $firstname = $1;
84     $lastname  = $2;
85   } else {
86     #deal w/unaudited netsapiens services?
87     my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
88     $firstname = $cust_main->get('first');
89     $lastname  = $cust_main->get('last');
90   }
91
92   #create user
93
94   my $ns = $self->ns_command( 'PUT', $self->ns_subscriber($svc_phone), 
95                                 'subscriber_login' => $phonenum.'@'.$domain,
96                                 'firstname'        => $firstname,
97                                 'lastname'         => $lastname,
98                                 'subscriber_pin'   => $svc_phone->pin,
99                                 'dial_plan'        => 'Default', #config?
100                                 'dial_policy'      => $dial_policy,
101                             );
102
103   if ( $ns->responseCode !~ /^2/ ) {
104      return $ns->responseCode. ' '.
105             join(', ', $self->ns_parse_response( $ns->responseContent ) );
106   }
107
108   #map DID to user
109   my $ns2 = $self->ns_command( 'PUT', $self->ns_dialplan($svc_phone),
110                                  'to_user' => $phonenum.'@'.$domain,
111                                  'to_host' => $domain,
112                              );
113
114   if ( $ns2->responseCode !~ /^2/ ) {
115      return $ns2->responseCode. ' '.
116             join(', ', $self->ns_parse_response( $ns2->responseContent ) );
117   }
118
119   '';
120 }
121
122 sub ns_delete {
123   my($self, $svc_phone) = (shift, shift);
124
125   my $ns = $self->ns_command( 'DELETE', $self->ns_subscriber($svc_phone) );
126
127   if ( $ns->responseCode !~ /^2/ ) {
128      return $ns->responseCode. ' '.
129             join(', ', $self->ns_parse_response( $ns->responseContent ) );
130   }
131
132   '';
133
134 }
135
136 sub ns_parse_response {
137   my( $self, $content ) = ( shift, shift );
138
139   #try to screen-scrape something useful
140   tie my %hash, Tie::IxHash;
141   while ( $content =~ s/^.*?<p>\s*<b>(.+?)<\/b>\s*(.+?)\s*<\/p>//is ) {
142     ( $hash{$1} = $2 ) =~ s/^\s*<(\w+)>(.+?)<\/\1>/$2/is;
143   }
144
145   %hash;
146 }
147
148 sub _export_insert {
149   my($self, $svc_phone) = (shift, shift);
150   $self->ns_create_or_update($svc_phone, 'Permit All');
151 }
152
153 sub _export_replace {
154   my( $self, $new, $old ) = (shift, shift, shift);
155   return "can't change phonenum with NetSapiens (unprovision and reprovision?)"
156     if $old->phonenum ne $new->phonenum;
157   $self->_export_insert($new);
158 }
159
160 sub _export_delete {
161   my( $self, $svc_phone ) = (shift, shift);
162
163   $self->ns_delete($svc_phone);
164 }
165
166 sub _export_suspend {
167   my( $self, $svc_phone ) = (shift, shift);
168   $self->ns_create_or_udpate($svc_phone, 'Deny');
169 }
170
171 sub _export_unsuspend {
172   my( $self, $svc_phone ) = (shift, shift);
173   #$self->ns_create_or_update($svc_phone, 'Permit All');
174   $self->_export_insert($svc_phone);
175 }
176
177 sub export_links {
178   my($self, $svc_phone, $arrayref) = (shift, shift, shift);
179   #push @$arrayref, qq!<A HREF="http://example.com/~!. $svc_phone->username.
180   #                 qq!">!. $svc_phone->username. qq!</A>!;
181   '';
182 }
183
184 1;