admin operations from svc_acct records
[freeside.git] / FS / FS / part_export / thirdlane.pm
1 package FS::part_export::thirdlane;
2
3 use base qw( FS::part_export );
4
5 use vars qw(%info $me);
6 use Tie::IxHash;
7 use Frontier::Client;
8
9 $me = '['.__PACKAGE__.']';
10
11 tie my %options, 'Tie::IxHash',
12   #'server'           => { label => 'Thirdlane server name or IP address', },
13   'username'         => { label => 'Thirdlane username', },
14   'password'         => { label => 'Thirdlane password', },
15   'prototype_tenant' => { label => 'Prototype tenant name', },
16   'debug'            => { label => 'Checkbox label', type => 'checkbox' },
17 #  'select_option'   => { label   => 'Select option description',
18 #                         type    => 'select', options=>[qw(chocolate vanilla)],
19 #                         default => 'vanilla',
20 #                       },
21 #  'textarea_option' => { label   => 'Textarea option description',
22 #                         type    => 'textarea',
23 #                         default => 'Default text.',
24 #                      },
25 ;
26
27 %info = (
28   'svc'      => [qw( svc_pbx svc_phone svc_acct )],
29   'desc'     =>
30     'Export tenants, DIDs and admins to Thirdlane PBX manager',
31   'options'  => \%options,
32   'notes'    => <<'END'
33 Exports tenants and DIDs to Thirdlane PBX manager using the XML-RPC API.
34 END
35 );
36
37 sub rebless { shift; }
38
39 sub _export_insert {
40   my($self, $svc_x) = (shift, shift);
41
42   if ( $svc_x->isa('FS::svc_pbx') ) {
43
44     return 'Name must be 19 characters or less (thirdlane restriction?)'
45       if length($svc_x->title) > 19;
46
47     return 'Name must consist of alphanumerics and spaces only (thirdlane restriction?)'
48       unless $svc_x->title =~ /^[\w\s]+$/;
49
50     my $tenant = {
51       'tenant'   => $svc_x->title,
52       'maxusers' => $svc_x->max_extensions,
53       #others?  will they not clone?
54     };
55
56     @what_to_clone = qw(routes schedules menus queues voiceprompts moh);
57
58     my $result = $self->_thirdlane_command( 'asterisk::rpc_tenant_create',
59                                             $tenant,
60                                             $self->option('prototype_tenant'),
61                                             \@what_to_clone,
62                                           );
63
64     #use Data::Dumper;
65     #warn Dumper(\$result);
66     $result eq '0' ? '' : 'Thirdlane API failure (rpc_tenant_create)';
67
68   } elsif ( $svc_x->isa('FS::svc_phone') ) {
69
70     my $result = $self->_thirdlane_command(
71       'asterisk::rpc_did_create',
72       $svc_x->countrycode. $svc_x->phonenum,
73     );
74
75     #use Data::Dumper;
76     #warn Dumper(\$result);
77     $result eq '0' or return 'Thirdlane API failure (rpc_did_create)';
78
79     return '' unless $svc_x->pbxsvc;
80
81     $result = $self->_thirdlane_command(
82       'asterisk::rpc_did_assign',
83       $svc_x->countrycode. $svc_x->phonenum,
84       $svc_x->pbx_title,
85     );
86
87     #use Data::Dumper;
88     #warn Dumper(\$result);
89     $result eq '0' ? '' : 'Thirdlane API failure (rpc_did_assign)';
90
91   } elsif ( $svc_x->isa('FS::svc_acct') ) {
92
93     return 'Must select a PBX' unless $svc_x->pbxsvc;
94
95     my $result = $self->_thirdlane_command(
96       'asterisk::rpc_admin_create',
97       $svc_x->username,
98       $svc_x->_password,
99       $svc_x->pbx_title,
100     );
101
102     #use Data::Dumper;
103     #warn Dumper(\$result);
104     $result eq '0' ? '' : 'Thirdlane API failure (rpc_admin_create)';
105
106   } else {
107     die "guru meditation #10: $svc_x is not FS::svc_pbx, FS::svc_phone or FS::svc_acct";
108   }
109
110 }
111
112 sub _export_replace {
113   my($self, $new, $old) = (shift, shift, shift);
114
115 #  #return "can't change username with thirdlane"
116 #  #  if $old->username ne $new->username;
117 #  #return '' unless $old->_password ne $new->_password;
118 #  $err_or_queue = $self->thirdlane_queue( $new->svcnum,
119 #    'replace', $new->username, $new->_password );
120 #  ref($err_or_queue) ? '' : $err_or_queue;
121
122   if ( $new->isa('FS::svc_pbx') ) {
123
124     #need more info on how the API works for changing names.. can it?
125     return "can't change PBX name with thirdlane (yet?)"
126       if $old->title ne $new->title;
127
128     my $tenant = {
129       'tenant'   => $old->title,
130       'maxusers' => $new->max_extensions,
131       #others?  will they not clone?
132     };
133
134     my $result = $self->_thirdlane_command( 'asterisk::rpc_tenant_update',
135                                             $tenant
136                                           );
137
138     #use Data::Dumper;
139     #warn Dumper(\$result);
140     $result eq '0' ? '' : 'Thirdlane API failure (rpc_tenant_update)';
141
142   } elsif ( $new->isa('FS::svc_phone') ) {
143
144     return "can't change DID countrycode with thirdlane"
145       if $old->countrycode ne $new->countrycode;
146     return "can't change DID number with thirdlane"
147       if $old->phonenum ne $new->phonenum;
148
149     if ( $old->pbxsvc != $new->pbxsvc ) {
150
151       if ( $old->pbxsvc ) {
152         my $result = $self->_thirdlane_command(
153           'asterisk::rpc_did_unassign',
154           $new->countrycode. $new->phonenum,
155         );
156         $result eq '0' or return 'Thirdlane API failure (rpc_did_unassign)';
157       }
158
159       if ( $new->pbxsvc ) {
160         my $result = $self->_thirdlane_command(
161           'asterisk::rpc_did_assign',
162           $new->countrycode. $new->phonenum,
163           $new->pbx_title,
164         );
165         $result eq '0' or return 'Thirdlane API failure (rpc_did_assign)';
166       }
167
168
169     }
170
171     '';
172
173   } elsif ( $new->isa('FS::svc_acct') ) {
174
175     return "can't change uesrname with thirdlane"
176       if $old->username ne $new->username;
177
178     return "can't change password with thirdlane"
179       if $old->_password ne $new->_password;
180
181     return "can't change PBX for user with thirdlane"
182       if $old->pbxsvc != $new->pbxsvc;
183
184     ''; #we don't care then
185
186   } else {
187     die "guru meditation #11: $svc_x is not FS::svc_pbx, FS::svc_phone or FS::svc_acct";
188   }
189
190 }
191
192 sub _export_delete {
193   my($self, $svc_x) = (shift, shift);
194   #my( $self, $svc_something ) = (shift, shift);
195   #$err_or_queue = $self->thirdlane_queue( $svc_something->svcnum,
196   #  'delete', $svc_something->username );
197   #ref($err_or_queue) ? '' : $err_or_queue;
198
199   if ( $svc_x->isa('FS::svc_pbx') ) {
200
201     my $result = $self->_thirdlane_command( 'asterisk::rpc_tenant_delete',
202                                             $svc_x->title,
203                                           );
204
205     #use Data::Dumper;
206     #warn Dumper(\$result);
207     #$result eq '0' ? '' : 'Thirdlane API failure (rpc_tenant_delete)';
208     warn "Thirdlane API failure (rpc_tenant_delete); deleting anyway\n"
209       if $result ne '0';
210     '';
211
212   } elsif ( $svc_x->isa('FS::svc_phone') ) {
213
214     if ( $svc_x->pbxsvc ) {
215       my $result = $self->_thirdlane_command(
216         'asterisk::rpc_did_unassign',
217         $svc_x->countrycode. $svc_x->phonenum,
218       );
219       $result eq '0' or return 'Thirdlane API failure (rpc_did_unassign)';
220     }
221
222     my $result = $self->_thirdlane_command(
223       'asterisk::rpc_did_delete',
224       $svc_x->countrycode. $svc_x->phonenum,
225     );
226     $result eq '0' ? '' : 'Thirdlane API failure (rpc_did_delete)';
227
228   } elsif ( $svc_x->isa('FS::svc_acct') ) {
229
230     return '' unless $svc_x->pbxsvc; #error out?  nah
231
232     my $result = $self->_thirdlane_command(
233       'asterisk::rpc_admin_delete',
234       $svc_x->username,
235       $svc_x->pbx_title,
236     );
237
238     #use Data::Dumper;
239     #warn Dumper(\$result);
240     #$result eq '0' ? '' : 'Thirdlane API failure (rpc_admin_delete)';
241     warn "Thirdlane API failure (rpc_admin_delete); deleting anyway\n"
242       if $result ne '0';
243     '';
244
245   } else {
246     die "guru meditation #12: $svc_x is not FS::svc_pbx, FS::svc_phone or FS::svc_acct";
247   }
248
249 }
250
251 sub _thirdlane_command {
252   my($self, @param) = @_;
253
254   my $url = 'http://'.
255               $self->option('username'). ':'. $self->option('password'). '@'.
256               $self->machine. '/xmlrpc.cgi';
257
258   warn "$me connecting to $url\n"
259     if $self->option('debug');
260   my $conn = Frontier::Client->new( 'url'   => $url,
261                                     #no, spews output to browser
262                                     #'debug' => $self->option('debug'),
263                                   );
264
265   warn "$me sending command: ". join(' ', @param). "\n"
266     if $self->option('debug');
267   $conn->call(@param);
268   
269 }
270
271   #my( $self, $svc_something ) = (shift, shift);
272   #$err_or_queue = $self->thirdlane_queue( $svc_something->svcnum,
273   #  'delete', $svc_something->username );
274   #ref($err_or_queue) ? '' : $err_or_queue;
275
276 #these three are optional
277 ## fallback for svc_acct will change and restore password
278 #sub _export_suspend {
279 #  my( $self, $svc_something ) = (shift, shift);
280 #  $err_or_queue = $self->thirdlane_queue( $svc_something->svcnum,
281 #    'suspend', $svc_something->username );
282 #  ref($err_or_queue) ? '' : $err_or_queue;
283 #}
284 #
285 #sub _export_unsuspend {
286 #  my( $self, $svc_something ) = (shift, shift);
287 #  $err_or_queue = $self->thirdlane_queue( $svc_something->svcnum,
288 #    'unsuspend', $svc_something->username );
289 #  ref($err_or_queue) ? '' : $err_or_queue;
290 #}
291 #
292 #sub export_links {
293 #  my($self, $svc_something, $arrayref) = (shift, shift, shift);
294 #  #push @$arrayref, qq!<A HREF="http://example.com/~!. $svc_something->username.
295 #  #                 qq!">!. $svc_something->username. qq!</A>!;
296 #  '';
297 #}
298
299 ####
300 #
301 ##a good idea to queue anything that could fail or take any time
302 #sub thirdlane_queue {
303 #  my( $self, $svcnum, $method ) = (shift, shift, shift);
304 #  my $queue = new FS::queue {
305 #    'svcnum' => $svcnum,
306 #    'job'    => "FS::part_export::thirdlane::thirdlane_$method",
307 #  };
308 #  $queue->insert( @_ ) or $queue;
309 #}
310 #
311 #sub thirdlane_insert { #subroutine, not method
312 #  my( $username, $password ) = @_;
313 #  #do things with $username and $password
314 #}
315 #
316 #sub thirdlane_replace { #subroutine, not method
317 #}
318 #
319 #sub thirdlane_delete { #subroutine, not method
320 #  my( $username ) = @_;
321 #  #do things with $username
322 #}
323 #
324 #sub thirdlane_suspend { #subroutine, not method
325 #}
326 #
327 #sub thirdlane_unsuspend { #subroutine, not method
328 #}
329
330 1;