FreeIPA/Common.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 83 SCAR 47.0
CC
10
Cov
0%
find_one
42.0
do_one
30.0
BEGIN
2.0
CRAP 110.0
1 package Net::FreeIPA::Common;
2 $Net::FreeIPA::Common::VERSION = '3.0.0';
3 use strict;
4 use warnings;
5  
6 use Net::FreeIPA::API::Magic;
7 use Net::FreeIPA::Error;
8  
9 use Readonly;
10  
11 # Convert find one API method in attribute name to find
12 # This map might be API version dependant
13 Readonly::Hash our %FIND_ONE => {
14 aci => 'aciname',
15 cert => 'cn',
16 delegation => 'aciname',
17 dnsforwardzone => 'idnsname',
18 dnsrecord => 'idnsname',
19 dnszone => 'idnsname',
20 group => 'cn',
21 host => 'fqdn',
22 hostgroup => 'fqdn',
23 server => 'cn',
24 service => 'krbprincipalname',
25 trust => 'cn',
26 user => 'uid',
27 vault => 'cn',
28 };
29  
30 =head1 NAME
31  
32 Net::FreeIPA::Common provides common convenience methods for Net::FreeIPA
33  
34 =head2 Public methods
35  
36 =over
37  
38 =item find_one
39  
40 Use the C<api> method C<<api_<api>_find>> to retrieve a single answer.
41  
42 The C<criteria> argument of the C<<api_<api>_find>> is the empty string,
43 the C<all> option is set.
44  
45 (Warns if more than one is found, and returns the first one
46 in that case).
47  
48 Returns undef in case of problem or if there is no result.
49  
50 =cut
51  
52 sub find_one
53 {
54 my ($self, $api, $value) = @_;
55  
56 my $res;
57  
58 # Do not use ->can with autoload'ed magic
59 # use API::retrieve (as function)
60 my $func = $api."_find";
61 my $method = "$Net::FreeIPA::API::API_METHOD_PREFIX$func";
62  
63 my ($cmds, $fail) = Net::FreeIPA::API::Magic::retrieve($func);
64  
65 if ($fail) {
66 $self->error("find_one: unknown API method $method");
67 } else {
68 my $attr = $FIND_ONE{$api};
69 if ($attr) {
70 my $response = $self->$method("", $attr => $value, all => 1);
71 if ($response) {
72 my $count = $response->{answer}->{result}->{count};
73 if (! $count) {
74 $self->debug("one_find method $method and value $value returns 0 answers.");
75 } else {
76 if ($count > 1) {
77 $self->warn("one_find method $method and value $value returns $count answers");
78 };
79 $res = $response->{result}->[0];
80 }
81 } else {
82 # error is already logged.
83 $self->debug("find_one: method $method failed.");
84 };
85 } else {
86 $self->error("find_one: no supported attribute for api $api");
87 }
88 };
89  
90 return $res;
91 }
92  
93 =item do_one
94  
95 Wrapper for simple call using C<api> and C<method> via
96 C<<api_<api>_<method>(C<name>)>>.
97  
98 Any options are passed.
99  
100 An error-type is not reported as error
101 (still returns C<undef>):
102  
103 =over
104  
105 =item DuplicateEntry: when C<method> is C<add>,
106 an existing entry is not reported as an error
107  
108 =item NotFound: when C<method> is not C<add>,
109 an missing entry is not reported as an error
110  
111 =back
112  
113 Returns the result attribute on success, or undef otherwise.
114  
115 =cut
116  
117 sub do_one
118 {
119 my ($self, $api, $method, $name, %opts) = @_;
120  
121 my $api_method = $Net::FreeIPA::API::API_METHOD_PREFIX.$api."_$method";
122  
123 # For add, do not report existing name as error
124 # for other methods, do not report missing name as error
125 my ($noerror, $errormethod, $noerrormsg);
126 if ($method eq 'add') {
127 $noerror = $Net::FreeIPA::Error::DUPLICATE_ENTRY;
128 $noerrormsg = "already exists";
129 } else {
130 $noerror = $Net::FreeIPA::Error::NOT_FOUND;
131 $noerrormsg = "does not exist";
132 }
133 $opts{__noerror} = [$noerror];
134  
135 my $response = $self->$api_method($name, %opts);
136  
137 my $msg;
138 if ($response) {
139 $msg = "succesfully $method-ed $api $name";
140 } else {
141 $msg = "failed to $method $api $name";
142 if ($response->{error} == $noerror) {
143 $msg .= " $api $noerrormsg";
144 }
145 }
146  
147 $self->debug("$api_method: $msg");
148  
149 return $response ? $response->{result} : undef;
150 };
151  
152  
153 =pod
154  
155 =back
156  
157 =cut
158  
159 1;