| 1 | package Net::FreeIPA::Common; | ||
| 2 | $Net::FreeIPA::Common::VERSION = '3.0.3'; | ||
| 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 as is except C<__noerror>. | ||
| 99 | |||
| 100 | Following error-type are not reported as error | ||
| 101 | (set/added as defaults for C<__noerror>) | ||
| 102 | (still returns C<undef>): | ||
| 103 | |||
| 104 | =over | ||
| 105 | |||
| 106 | =item DuplicateEntry: when C<method> is C<add>, | ||
| 107 | an existing entry is not reported as an error | ||
| 108 | |||
| 109 | =item AlreadyInactive: when C<method> is C<disable>, | ||
| 110 | an already inactive/disabled entry is not reported as an error | ||
| 111 | |||
| 112 | =item NotFound: when C<method> is not C<mod> or any of the previous, | ||
| 113 | an missing entry is not reported as an error | ||
| 114 | |||
| 115 | =back | ||
| 116 | |||
| 117 | Returns the result attribute on success, or undef otherwise. | ||
| 118 | |||
| 119 | =cut | ||
| 120 | |||
| 121 | sub do_one | ||
| 122 | { | ||
| 123 | my ($self, $api, $method, $name, %opts) = @_; | ||
| 124 | |||
| 125 | my $api_method = $Net::FreeIPA::API::API_METHOD_PREFIX.$api."_$method"; | ||
| 126 | |||
| 127 | my $msg_map = { | ||
| 128 | $Net::FreeIPA::Error::DUPLICATE_ENTRY => "already exists", | ||
| 129 | $Net::FreeIPA::Error::ALREADY_INACTIVE => "already inactive/disabled", | ||
| 130 | $Net::FreeIPA::Error::NOT_FOUND => "does not exist", | ||
| 131 | }; | ||
| 132 | |||
| 133 | if ($method eq 'add') { | ||
| 134 | # For add, do not report existing name as error | ||
| 135 | push(@{$opts{__noerror}}, $Net::FreeIPA::Error::DUPLICATE_ENTRY); | ||
| 136 | } elsif ($method eq 'disable') { | ||
| 137 | # For disable, do not report already disabled name as error | ||
| 138 | push(@{$opts{__noerror}}, $Net::FreeIPA::Error::ALREADY_INACTIVE); | ||
| 139 | } | ||
| 140 | |||
| 141 | if (! grep {$method eq $_ } qw(add mod)) { | ||
| 142 | # For methods except add/mod, do not report missing name as error | ||
| 143 | push(@{$opts{__noerror}}, $Net::FreeIPA::Error::NOT_FOUND); | ||
| 144 | } | ||
| 145 | |||
| 146 | my $response = $self->$api_method($name, %opts); | ||
| 147 | |||
| 148 | my $name_msg = ref($name) eq 'ARRAY' ? join(', ', @$name) : $name; | ||
| 149 | my $msg; | ||
| 150 | if ($response) { | ||
| 151 | $msg = "successfully $method-ed $api $name_msg"; | ||
| 152 | } else { | ||
| 153 | $msg = "failed to $method $api $name_msg"; | ||
| 154 | |||
| 155 | foreach my $noerror (sort keys %$msg_map) { | ||
| 156 | $msg .= " $api ".$msg_map->{$noerror} if ($response->{error} == $noerror); | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | $self->debug("$api_method: $msg"); | ||
| 161 | |||
| 162 | return $response ? $response->{result} : undef; | ||
| 163 | }; | ||
| 164 | |||
| 165 | |||
| 166 | =pod | ||
| 167 | |||
| 168 | =back | ||
| 169 | |||
| 170 | =cut | ||
| 171 | |||
| 172 | 1; |