ZimbraManager/SOAP/Friendly.pm untested

filter: Statementstmt 0.0% 0 / 146 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 180 CC 8 SCAR 39.7
CC
8
Cov
0%
callFriendly
72.0
callFriendlyLegacy
2.0
BEGIN
2.0
CRAP 52.9
1 package ZimbraManager::SOAP::Friendly;
2  
3 use Mojo::Base 'ZimbraManager::SOAP';
4 use Mojo::Util qw(dumper);
5  
6  
7 my $MAP = {
8 auth => {
9 args => [ qw(password accountName)],
10 out => sub {
11 my ($password, $accountName) = @_;
12 return { persistAuthTokenCookie => 1,
13 password => $password,
14 account => {
15 by => 'name',
16 _ => $accountName}
17 };
18 },
19 in => sub {
20 my $ret = shift;
21 return $ret;
22 },
23 },
24 getAccountInfo => {
25 args => [ qw(accountName)],
26 out => sub {
27 my ($accountName) = @_;
28 return { account => {
29 by => 'name',
30 _ => $accountName}
31 };
32 },
33 in => sub {
34 my $ret = shift;
35 my $a = $ret->{a};
36 my $parsed = {};
37 for my $elem (@$a) {
38 $parsed->{$elem->{n}} = $elem->{'_'};
39 }
40 return $parsed;
41 },
42 },
43 getAccount => {
44 args => [ qw(accountName attribute?) ],
45 out => sub {
46 my ($accountName, $attribute) = @_;
47 my $argHash;
48 if (defined $attribute) { # optional; get just this element
49 $argHash = { account => {
50 by => 'name',
51 _ => $accountName},
52 attrs => $attribute,
53 };
54 }
55 else {
56 $argHash = { account => {
57 by => 'name',
58 _ => $accountName}
59 };
60 }
61 return $argHash;
62 },
63 in => sub {
64 my $ret = shift;
65 # $ret->{account} is a hash with
66 # keys 'a' (all account settings), 'id' (UUID), 'name' (email)
67 my $a = $ret->{account}->{a};
68 my $parsed = {};
69 for my $elem (@$a) {
70 $parsed->{$elem->{n}} = $elem->{'_'};
71 }
72 return $parsed;
73 },
74 },
75 getAllAccounts => {
76 args => [ qw(serverName domainName)],
77 out => sub {
78 my ($serverName, $domainName) = @_;
79 return { server => {
80 by => 'name',
81 _ => $serverName },
82 domain => {
83 by => 'name',
84 _ => $domainName }
85 };
86 },
87 in => sub {
88 my $ret = shift;
89 return $ret;
90 },
91 },
92 modifyAccount => {
93 args => [ qw(zimbraUUID modifyKey modifyValue)],
94 out => sub {
95 my ($zimbraUUID, $modifyKey, $modifyValue ) = @_;
96 return { id => $zimbraUUID,
97 a => {
98 n => $modifyKey,
99 _ => $modifyValue
100 }
101 };
102 },
103 in => sub {
104 my $ret = shift;
105 # $ret->{account} is a hash with
106 # keys 'a' (all account settings), 'id' (UUID), 'name' (email)
107 my $a = $ret->{account}{a};
108 my $parsed = {};
109 for my $elem (@$a) {
110 $parsed->{$elem->{n}} = $elem->{'_'};
111 }
112 return $parsed;
113 },
114 },
115 getAllDomains => {
116 args => [ ],
117 out => sub {
118 return {};
119 },
120 in => sub {
121 my $ret = shift;
122 return $ret;
123 },
124 },
125 getDomainInfo => {
126 args => [ qw(domainName)],
127 out => sub {
128 my ($domainName) = @_;
129 return { domain => {
130 by => 'name',
131 _ => $domainName }
132 };
133 },
134 in => sub {
135 my $ret = shift;
136 my $domain = $ret->{domain};
137 if ($domain->{id} eq 'globalconfig-dummy-id') {
138 return {
139 id => undef
140 };
141 }
142 else {
143 return $domain;
144 }
145 },
146 },
147 createAccount => {
148 args => [ qw(uid defaultEmailDomain plainPassword givenName surName country displayName localeLang cosId) ],
149 out => sub {
150 my ($uid, $defaultEmailDomain, $plainPassword, $givenName, $surName, $country, $displayName, $localeLang, $cosId) = @_;
151 return {
152 name => $uid . '@' . $defaultEmailDomain,
153 password => $plainPassword,
154 a => [
155 {
156 n => 'givenName',
157 _ => $givenName,
158 },
159 {
160 n => 'sn',
161 _ => $surName,
162 },
163 {
164 n => 'c',
165 _ => $country,
166 },
167 {
168 n => 'displayName',
169 _ => $displayName,
170 },
171 {
172 n => 'zimbraPrefLocale',
173 _ => $localeLang,
174 },
175 {
176 n => 'zimbraPasswordMustChange',
177 _ => 'FALSE',
178 },
179 {
180 n => 'zimbraCOSid',
181 _ => $cosId,
182 },
183 ],
184 };
185 },
186 in => sub {
187 my $ret = shift;
188 # $ret->{account} is a hash with
189 # keys 'a' (all account settings), 'id' (UUID), 'name' (email)
190 my $a = $ret->{account}{a};
191 my $parsed = {};
192 for my $elem (@$a) {
193 $parsed->{$elem->{n}} = $elem->{'_'};
194 }
195 return $parsed;
196 },
197 },
198 addAccountAlias => {
199 args => [ qw(zimbraUUID emailAlias) ],
200 out => sub {
201 my ($zimbraUUID, $emailAlias) = @_;
202 return {
203 id => $zimbraUUID,
204 alias => $emailAlias
205 };
206 },
207 in => sub {
208 my $ret = shift;
209 return $ret;
210 },
211 },
212 removeAccountAlias => {
213 args => [ qw(zimbraUUID emailAlias) ],
214 out => sub {
215 my ($zimbraUUID, $emailAlias) = @_;
216 return {
217 id => $zimbraUUID,
218 alias => $emailAlias
219 };
220 },
221 in => sub {
222 my $ret = shift;
223 return $ret;
224 },
225 },
226 deleteAccount => {
227 args => [ qw(zimbraUUID) ],
228 out => sub {
229 my ($zimbraUUID) = @_;
230 return {
231 id => $zimbraUUID
232 };
233 },
234 in => sub {
235 my $ret = shift;
236 return $ret;
237 },
238 },
239 getCos => {
240 args => [ qw(cosName) ],
241 out => sub {
242 my ($cosName) = @_;
243 return { cos => {
244 by => 'name',
245 _ => $cosName }
246 };
247 },
248 in => sub {
249 my $ret = shift;
250 # $ret->{cos} is a hash with
251 # 'id' (UUID), 'name' (email)
252 return $ret->{cos};
253 }
254 },
255 };
256  
257  
258 my $helperHashingAllAccounts = sub {
259 my $ret = shift;
260 my $accounts;
261 for my $za ( @{ $ret->{account} } ) {
262 my $name = $za->{name};
263 my $id = $za->{id};
264 my %kv = map { $_->{'n'} => $_->{'_'} } @{$za->{a}};
265 $accounts->{$name} = {
266 'name' => $name,
267 'id' => $id,
268 'kv' => \%kv,
269 };
270 }
271 return $accounts;
272 };
273  
274  
275 sub callFriendlyLegacy {
276 my $self = shift;
277 my $authToken = shift;
278 my $action = shift;
279 my $args = shift;
280 my $namedParameters = {
281 action => $action,
282 args => $args,
283 authToken => $authToken,
284 };
285 return $self->callFriendly($namedParameters);
286 }
287  
288 sub callFriendly {
289 my $self = shift;
290 my $namedParameters = shift;
291 if (ref $namedParmeters ne 'HASH') {
292 $namedParameters = { @_ };
293 }
294 my $action = $namedParameters->{action};
295 my $args = $namedParameters->{args};
296 my $authToken = $namedParameters->{authToken};
297  
298 # check input
299 if (not exists $MAP->{$action}) {
300 die "no valid function ($action) given";
301 }
302 if ($action ne 'auth' and not defined $authToken) {
303 die 'parameter authToken missing';
304 }
305  
306 my $soapArgsBuild = {};
307 if (scalar @{$MAP->{$action}{args}}) {
308 if ((not defined $args) or (ref $args ne 'HASH')) {
309 die 'parameter args is not a HASH';
310 }
311 for my $key (@{$MAP->{$action}->{args}}) {
312 next if $key =~ m/\?$/; # optional argument
313 if (not exists $args->{$key}) {
314 die "function $action: mandatory key/value (key: $key) not given";
315 }
316 }
317  
318 # build argument list
319 my @orderedArgs;
320 for my $key (@{$MAP->{$action}->{args}}) {
321 my $localKey = $key;
322 $localKey =~ s/\?$//; # remove optional flag but keep key
323 push @orderedArgs, $args->{$localKey};
324 }
325  
326 # build SOAP arguments
327 $soapArgsBuild = $MAP->{$action}->{out}->(@orderedArgs);
328 }
329  
330 # build SOAP action name
331 my $actionRequest = $action . 'Request';
332  
333 $self->log->debug(dumper({
334 _function => 'ZimbraManager::Soap::callFriendly',
335 action =>$action,
336 args =>$args,
337 authToken =>$authToken
338 }));
339  
340 my ($ret, $err) = $self->call({
341 action =>$actionRequest,
342 args =>$soapArgsBuild,
343 authToken =>$authToken,
344 });
345  
346 # parse SOAP answer
347 my $parsedAnswer = $MAP->{$action}->{in}->($ret);
348 return ($parsedAnswer, $err);
349 }
350  
351 1;
352  
353 =pod
354  
355 =encoding UTF-8
356  
357 =head1 NAME
358  
359 ZimbraManager::SOAP::Friendly
360  
361 =head1 VERSION
362  
363 version 0.15
364  
365 =head1 SYNOPSIS
366  
367 use ZimbraManager::SOAP::Friendly;
368  
369 my $authToken = 'VERY_LONG_TOKEN_LINE_FROM_SESSION';
370 my $action = 'createAccount';
371 my $args = {
372 uid => 'rplessl',
373 defaultEmailDomain => 'oetiker.ch',
374 givenName => 'Roman',
375 surName => 'Plessl',
376 country => 'CH',
377 displayName => 'Roman Plessl',
378 localeLang => 'de',
379 cosId => 'ABCD-EFGH-1234',
380 };
381 my $namedParameters = {
382 action => $action,
383 args => $args,
384 authToken => $authToken,
385 };
386 my ($ret, $err) = $self->soap->callFriendly($namedParameters);
387  
388 also
389  
390 $self->soap->callFriendly(
391 authToken => $authToken,
392 action => $action,
393 args => { },
394 );
395  
396 is valid
397  
398 =head1 DESCRIPTION
399  
400 Helper class for Zimbra adminstration with a user friendly interface
401  
402 =head1 NAME
403  
404 ZimbraManager::SOAP::Friendly - class to manage Zimbra with perl and SOAP
405  
406 =head1 ATTRIBUTES
407  
408 =head2 $MAP
409  
410 This hash defines arguments and out-going (to Zimbra) and in-coming
411 (from Zimbra) mapping subroutines for SOAP actions called with the
412 callFriendly() method below.
413  
414 =head1 METHODS
415  
416 All the methods of L<Mojo::Base> plus:
417  
418 =head2 Private functions
419  
420 Private functions used in the startup function
421  
422 =head3 helperHashingAllAccounts
423  
424 Helper function for processing AllAccounts SOAP call
425  
426 =head2 Public methods
427  
428 =head3 callFriendly
429  
430 Calls Zimbra with the given argument and returns the SOAP response as perl hash.
431  
432 =head1 COPYRIGHT
433  
434 Copyright (c) 2014 by Roman Plessl. All rights reserved.
435  
436 =head1 LICENSE
437  
438 This program is free software: you can redistribute it and/or modify it
439 under the terms of the GNU General Public License as published by the Free
440 Software Foundation, either version 3 of the License, or (at your option)
441 any later version.
442  
443 This program is distributed in the hope that it will be useful, but WITHOUT
444 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
445 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
446 more details.
447  
448 You should have received a copy of the GNU General Public License along with
449 this program. If not, see L<http://www.gnu.org/licenses/>.
450  
451 =head1 AUTHOR
452  
453 S<Roman Plessl E<lt>roman@plessl.infoE<gt>>
454  
455 =head1 HISTORY
456  
457 2014-04-29 rp Initial Version
458  
459 =head1 AUTHOR
460  
461 Roman Plessl <rplessl@cpan.org>
462  
463 =head1 COPYRIGHT AND LICENSE
464  
465 This software is Copyright (c) 2014 by Roman Plessl.
466  
467 This is free software, licensed under:
468  
469 The GNU General Public License, Version 3, June 2007
470  
471 =cut
472  
473 __END__