initial thirdlane export w/tenant insert/delete/replace, RT#7051
[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 )],
29   'desc'     =>
30     'Export tenants and DIDs 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     my $tenant = {
45       'tenant'   => $svc_x->title,
46       'maxusers' => $svc_x->max_extensions,
47       #others?  will they not clone?
48     };
49
50     @what_to_clone = qw(routes schedules menus queues voiceprompts moh);
51
52     my $result = $self->_thirdlane_command( 'asterisk::rpc_tenant_create',
53                                             $tenant,
54                                             $self->option('prototype_tenant'),
55                                             \@what_to_clone,
56                                           );
57
58     use Data::Dumper;
59     warn Dumper(\$result);
60
61     #$result == 0 means okay??
62
63     #$error;
64
65     '';
66
67   } elsif ( $svc_x->isa('FS::svc_phone') ) {
68
69   } else {
70     die "guru meditation #10: $svc_x is not FS::svc_pbx or FS::svc_phone";
71   }
72
73 }
74
75 sub _export_insert {
76   my($self, $svc_x) = (shift, shift);
77
78   if ( $svc_x->isa('FS::svc_pbx') ) {
79
80     my $result = $self->_thirdlane_command( 'asterisk::rpc_tenant_delete',
81                                             $svc_x->title,
82                                           );
83
84     use Data::Dumper;
85     warn Dumper(\$result);
86
87     #$result == 0 means okay??
88
89     #$error;
90
91     '';
92
93   } elsif ( $svc_x->isa('FS::svc_phone') ) {
94
95   } else {
96     die "guru meditation #11: $svc_x is not FS::svc_pbx or FS::svc_phone";
97   }
98
99 }
100
101 sub _export_replace {
102   my($self, $svc_x) = (shift, shift);
103
104   if ( $svc_x->isa('FS::svc_pbx') ) {
105
106     my $tenant = {
107       'tenant'   => $svc_x->title,
108       'maxusers' => $svc_x->max_extensions,
109       #others?  will they not clone?
110     };
111
112     my $result = $self->_thirdlane_command( 'asterisk::rpc_tenant_update',
113                                             $tenant
114                                           );
115
116     use Data::Dumper;
117     warn Dumper(\$result);
118
119     #$result == 0 means okay??
120
121     #$error;
122
123     '';
124
125   } elsif ( $svc_x->isa('FS::svc_phone') ) {
126
127   } else {
128     die "guru meditation #11: $svc_x is not FS::svc_pbx or FS::svc_phone";
129   }
130
131 }
132
133
134 sub _thirdlane_command {
135   my($self, @param) = @_;
136
137   my $url = 'http://'.
138               $self->option('username'). ':'. $self->option('password'). '@'.
139               $self->machine. '/xmlrpc.cgi';
140
141   warn "$me connecting to $url\n"
142     if $self->option('debug');
143   my $conn = Frontier::Client->new( 'url'   => $url,
144                                     'debug' => $self->option('debug'),
145                                   );
146
147   warn "$me sending command: ". join(' ', @param). "\n"
148     if $self->option('debug');
149   $conn->call(@param);
150   
151 }
152
153 sub _export_replace {
154   my( $self, $new, $old ) = (shift, shift, shift);
155   #return "can't change username with thirdlane"
156   #  if $old->username ne $new->username;
157   #return '' unless $old->_password ne $new->_password;
158   $err_or_queue = $self->thirdlane_queue( $new->svcnum,
159     'replace', $new->username, $new->_password );
160   ref($err_or_queue) ? '' : $err_or_queue;
161 }
162
163 sub _export_delete {
164   '';
165   #my( $self, $svc_something ) = (shift, shift);
166   #$err_or_queue = $self->thirdlane_queue( $svc_something->svcnum,
167   #  'delete', $svc_something->username );
168   #ref($err_or_queue) ? '' : $err_or_queue;
169 }
170
171 #these three are optional
172 # fallback for svc_acct will change and restore password
173 sub _export_suspend {
174   my( $self, $svc_something ) = (shift, shift);
175   $err_or_queue = $self->thirdlane_queue( $svc_something->svcnum,
176     'suspend', $svc_something->username );
177   ref($err_or_queue) ? '' : $err_or_queue;
178 }
179
180 sub _export_unsuspend {
181   my( $self, $svc_something ) = (shift, shift);
182   $err_or_queue = $self->thirdlane_queue( $svc_something->svcnum,
183     'unsuspend', $svc_something->username );
184   ref($err_or_queue) ? '' : $err_or_queue;
185 }
186
187 sub export_links {
188   my($self, $svc_something, $arrayref) = (shift, shift, shift);
189   #push @$arrayref, qq!<A HREF="http://example.com/~!. $svc_something->username.
190   #                 qq!">!. $svc_something->username. qq!</A>!;
191   '';
192 }
193
194 ####
195 #
196 ##a good idea to queue anything that could fail or take any time
197 #sub thirdlane_queue {
198 #  my( $self, $svcnum, $method ) = (shift, shift, shift);
199 #  my $queue = new FS::queue {
200 #    'svcnum' => $svcnum,
201 #    'job'    => "FS::part_export::thirdlane::thirdlane_$method",
202 #  };
203 #  $queue->insert( @_ ) or $queue;
204 #}
205 #
206 #sub thirdlane_insert { #subroutine, not method
207 #  my( $username, $password ) = @_;
208 #  #do things with $username and $password
209 #}
210 #
211 #sub thirdlane_replace { #subroutine, not method
212 #}
213 #
214 #sub thirdlane_delete { #subroutine, not method
215 #  my( $username ) = @_;
216 #  #do things with $username
217 #}
218 #
219 #sub thirdlane_suspend { #subroutine, not method
220 #}
221 #
222 #sub thirdlane_unsuspend { #subroutine, not method
223 #}
224
225 1;