get subscriber deletion working and remove devel cruft, 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 = $method eq 'PUT' ? $ns->buildQuery( { @_ } ) : '';
43     $content =~ s/^\?//;
44     push @args, $content;
45   }
46
47   my $auth =
48     encode_base64( $self->option('login'). ':'. $self->option('password') );
49   push @args, { 'Authorization' => "Basic $auth" };
50
51   $ns->$method( @args );
52   $ns;
53 }
54
55 sub ns_subscriber {
56   my($self, $svc_phone) = (shift, shift);
57
58   my $domain = $self->option('domain');
59   my $phonenum = $svc_phone->phonenum;
60
61   "/domains_config/$domain/subscriber_config/$phonenum";
62 }
63
64 sub ns_create_or_update {
65   my($self, $svc_phone, $dial_policy) = (shift, shift, shift);
66
67   my $domain = $self->option('domain');
68   my $phonenum = $svc_phone->phonenum;
69
70   my( $firstname, $lastname );
71   if ( $svc_phone->phone_name =~ /^\s*(\S+)\s+(\S.*\S)\s*$/ ) {
72     $firstname = $1;
73     $lastname  = $2;
74   } else {
75     #deal w/unaudited netsapiens services?
76     my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
77     $firstname = $cust_main->get('first');
78     $lastname  = $cust_main->get('last');
79   }
80
81   my $ns = $self->ns_command( 'PUT', $self->ns_subscriber($svc_phone), 
82                                 'subscriber_login' => $phonenum.'@'.$domain,
83                                 'firstname'        => $firstname,
84                                 'lastname'         => $lastname,
85                                 'subscriber_pin'   => $svc_phone->pin,
86                                 'dial_plan'        => 'Default', #config?
87                                 'dial_policy'      => $dial_policy,
88                             );
89
90   if ( $ns->responseCode !~ /^2/ ) {
91      return $ns->responseCode. ' '.
92             join(', ', $self->ns_parse_response( $ns->responseContent ) );
93   }
94
95   '';
96 }
97
98 sub ns_delete {
99   my($self, $svc_phone) = (shift, shift);
100
101   my $ns = $self->ns_command( 'DELETE', $self->ns_subscriber($svc_phone) );
102
103   if ( $ns->responseCode !~ /^2/ ) {
104      return $ns->responseCode. ' '.
105             join(', ', $self->ns_parse_response( $ns->responseContent ) );
106   }
107
108   '';
109
110 }
111
112 sub ns_parse_response {
113   my( $self, $content ) = ( shift, shift );
114
115   #try to screen-scrape something useful
116   tie my %hash, Tie::IxHash;
117   while ( $content =~ s/^.*?<p>\s*<b>(.+?)<\/b>\s*(.+?)\s*<\/p>//is ) {
118     ( $hash{$1} = $2 ) =~ s/^\s*<(\w+)>(.+?)<\/\1>/$2/is;
119   }
120
121   %hash;
122 }
123
124 sub _export_insert {
125   my($self, $svc_phone) = (shift, shift);
126   $self->ns_create_or_update($svc_phone, 'Permit All');
127 }
128
129 sub _export_replace {
130   my( $self, $new, $old ) = (shift, shift, shift);
131   return "can't change phonenum with NetSapiens (unprovision and reprovision?)"
132     if $old->phonenum ne $new->phonenum;
133   $self->_export_insert($new);
134 }
135
136 sub _export_delete {
137   my( $self, $svc_phone ) = (shift, shift);
138
139   $self->ns_delete($svc_phone);
140 }
141
142 sub _export_suspend {
143   my( $self, $svc_phone ) = (shift, shift);
144   $self->ns_create_or_udpate($svc_phone, 'Deny');
145 }
146
147 sub _export_unsuspend {
148   my( $self, $svc_phone ) = (shift, shift);
149   #$self->ns_create_or_update($svc_phone, 'Permit All');
150   $self->_export_insert($svc_phone);
151 }
152
153 sub export_links {
154   my($self, $svc_phone, $arrayref) = (shift, shift, shift);
155   #push @$arrayref, qq!<A HREF="http://example.com/~!. $svc_phone->username.
156   #                 qq!">!. $svc_phone->username. qq!</A>!;
157   '';
158 }
159
160 1;