| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Net::FreeIPA::API::Magic; | 
| 2 |  |  |  |  |  |  | $Net::FreeIPA::API::Magic::VERSION = '3.0.0'; | 
| 3 | 8 |  |  | 8 |  | 22268 | use strict; | 
|  | 8 |  |  |  |  | 8 |  | 
|  | 8 |  |  |  |  | 175 |  | 
| 4 | 8 |  |  | 8 |  | 77 | use warnings; | 
|  | 8 |  |  |  |  | 11 |  | 
|  | 8 |  |  |  |  | 144 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 8 |  |  | 8 |  | 23 | use Types::Serialiser; # is used by JSON::XS | 
|  | 8 |  |  |  |  | 3 |  | 
|  | 8 |  |  |  |  | 126 |  | 
| 7 | 8 |  |  | 8 |  | 20 | use JSON::XS; | 
|  | 8 |  |  |  |  | 7 |  | 
|  | 8 |  |  |  |  | 308 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 8 |  |  | 8 |  | 12173 | use Net::FreeIPA::API::Data; | 
|  | 8 |  |  |  |  | 15 |  | 
|  | 8 |  |  |  |  | 285 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 8 |  |  | 8 |  | 37 | use Readonly; | 
|  | 8 |  |  |  |  | 8 |  | 
|  | 8 |  |  |  |  | 303 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 8 |  |  | 8 |  | 27 | use base qw(Exporter); | 
|  | 8 |  |  |  |  | 8 |  | 
|  | 8 |  |  |  |  | 3841 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | our @EXPORT_OK = qw(retrieve version); | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | Readonly my $TRUE => Types::Serialiser::true; | 
| 18 |  |  |  |  |  |  | Readonly my $FALSE => Types::Serialiser::false; | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | # Cache these command keys | 
| 21 |  |  |  |  |  |  | Readonly::Array our @CACHE_KEYS => qw( | 
| 22 |  |  |  |  |  |  | name | 
| 23 |  |  |  |  |  |  | takes_args takes_options | 
| 24 |  |  |  |  |  |  | ); | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | # Cache these keys from the takes_ CACHE_KEYS | 
| 27 |  |  |  |  |  |  | Readonly::Array our @CACHE_TAKES_KEYS => qw( | 
| 28 |  |  |  |  |  |  | name type class | 
| 29 |  |  |  |  |  |  | required autofill multivalue | 
| 30 |  |  |  |  |  |  | ); | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | Readonly::Hash our %CACHE_TAKES_DEFAULT => { | 
| 33 |  |  |  |  |  |  | autofill => $FALSE, | 
| 34 |  |  |  |  |  |  | class => 'unknown_class', | 
| 35 |  |  |  |  |  |  | multivalue => $FALSE, | 
| 36 |  |  |  |  |  |  | name => 'unknown_name', | 
| 37 |  |  |  |  |  |  | required => $FALSE, | 
| 38 |  |  |  |  |  |  | type => 'unknown_type', | 
| 39 |  |  |  |  |  |  | }; | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | # hashref to store cached command data | 
| 42 |  |  |  |  |  |  | my $cmd_cache = {}; | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | =head2 Public functions | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | =over | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | =item flush_cache | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | Reset the cache | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | =cut | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | sub flush_cache | 
| 55 |  |  |  |  |  |  | { | 
| 56 | 1 |  |  | 1 | 1 | 911 | $cmd_cache = {}; | 
| 57 | 1 |  |  |  |  | 15 | return $cmd_cache; | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | =item cache | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | Given C command hashref, cache (and return) the relevant | 
| 63 |  |  |  |  |  |  | (filtered) command data. | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | C is typically the decoded JSON command response. | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | =cut | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | sub cache | 
| 70 |  |  |  |  |  |  | { | 
| 71 | 5 |  |  | 5 | 1 | 5715 | my ($data) = @_; | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 5 |  |  |  |  | 11 | my $name = $data->{name}; | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 5 |  |  |  |  | 34 | foreach my $key (sort keys %$data) { | 
| 76 | 21 | 100 |  |  |  | 63 | if ($key =~ m/^takes_/) { | 
| 77 | 10 |  |  |  |  | 13 | my $newdata = []; | 
| 78 | 10 |  |  |  |  | 13 | foreach my $tdata (@{$data->{$key}}) { | 
|  | 10 |  |  |  |  | 22 |  | 
| 79 | 99 | 100 |  |  |  | 213 | if (ref($tdata) eq '') { | 
| 80 | 1 |  |  |  |  | 3 | my $value = $tdata; | 
| 81 | 1 |  |  |  |  | 5 | $value =~ s/[*+?]$//; | 
| 82 | 1 |  |  |  |  | 6 | $tdata = {%CACHE_TAKES_DEFAULT}; | 
| 83 | 1 |  |  |  |  | 57 | $tdata->{name} = $value; | 
| 84 |  |  |  |  |  |  | } else { | 
| 85 |  |  |  |  |  |  | # Arrayref of command hashrefs | 
| 86 | 98 |  |  |  |  | 522 | foreach my $tkey (sort keys %$tdata) { | 
| 87 | 669 | 100 |  |  |  | 1474 | delete $tdata->{$tkey} if (! grep {$tkey eq $_} @CACHE_TAKES_KEYS); | 
|  | 4014 |  |  |  |  | 18391 |  | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  | } | 
| 90 | 99 |  |  |  |  | 252 | push(@$newdata, $tdata); | 
| 91 |  |  |  |  |  |  | } | 
| 92 | 10 |  |  |  |  | 32 | $data->{$key} = $newdata; | 
| 93 |  |  |  |  |  |  | } else { | 
| 94 | 11 | 100 |  |  |  | 33 | delete $data->{$key} if (! grep {$key eq $_} @CACHE_KEYS); | 
|  | 33 |  |  |  |  | 235 |  | 
| 95 |  |  |  |  |  |  | } | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 5 |  |  |  |  | 12 | $cmd_cache->{$name} = $data; | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 5 |  |  |  |  | 16 | return $data; | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | =item version | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | Return the API version from C | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | =cut | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | sub version | 
| 110 |  |  |  |  |  |  | { | 
| 111 | 1 |  |  | 1 | 1 | 1285 | return $Net::FreeIPA::API::Data::VERSION; | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | =item retrieve | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | Retrieve the command data for command C. | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | (For now, the data is loaded from the C that is | 
| 119 |  |  |  |  |  |  | distributed with this package and is a fixed version only). | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | Returns the cache command hashref and undef errormessage on SUCCESS, | 
| 122 |  |  |  |  |  |  | an emptyhashref and actual errormessage otherwise. | 
| 123 |  |  |  |  |  |  | If the command is already in cache, return the cached version | 
| 124 |  |  |  |  |  |  | (and undef errormessage). | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | =cut | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | sub retrieve | 
| 129 |  |  |  |  |  |  | { | 
| 130 | 4 |  |  | 4 | 1 | 2003 | my ($name) = @_; | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | # Return already cached data | 
| 133 | 4 | 100 |  |  |  | 19 | return ($cmd_cache->{$name}, undef) if defined($cmd_cache->{$name}); | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | # Get the JSON data from Net::FreeIPA::API::Data | 
| 136 |  |  |  |  |  |  | # TODO: get the JSON data from the JSON api | 
| 137 |  |  |  |  |  |  |  | 
| 138 | 3 |  |  |  |  | 4 | my $data; | 
| 139 | 3 |  |  |  |  | 18 | my $json = $Net::FreeIPA::API::Data::API_DATA{$name}; | 
| 140 | 3 | 100 |  |  |  | 25 | if (! $json) { | 
| 141 | 1 |  |  |  |  | 7 | return {}, "retrieve name $name failed: no JSON data"; | 
| 142 |  |  |  |  |  |  | } | 
| 143 |  |  |  |  |  |  |  | 
| 144 | 2 |  |  |  |  | 4 | local $@; | 
| 145 | 2 |  |  |  |  | 3 | eval { | 
| 146 | 2 |  |  |  |  | 252 | $data = decode_json($json); | 
| 147 |  |  |  |  |  |  | }; | 
| 148 |  |  |  |  |  |  |  | 
| 149 | 2 | 50 |  |  |  | 8 | if ($@) { | 
| 150 | 0 |  |  |  |  | 0 | return {}, "retrieve name $name failed decode_json with $@"; | 
| 151 |  |  |  |  |  |  | } else { | 
| 152 | 2 |  |  |  |  | 5 | return cache($data), undef; | 
| 153 |  |  |  |  |  |  | }; | 
| 154 |  |  |  |  |  |  | } | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | =item all_command_names | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | Return all possible commandsnames sorted. | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | Does not update cache. | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | =cut | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | # Used mainly to generate the API::Function exports | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | sub all_command_names | 
| 167 |  |  |  |  |  |  | { | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | # Get the JSON data from Net::FreeIPA::API::Data | 
| 170 |  |  |  |  |  |  | # TODO: get the JSON data from the JSON api | 
| 171 |  |  |  |  |  |  | # If the JSON API doesn't allow to just get the names, | 
| 172 |  |  |  |  |  |  |  | 
| 173 | 1 |  |  | 1 | 1 | 35912 | return sort keys %Net::FreeIPA::API::Data::API_DATA; | 
| 174 |  |  |  |  |  |  | } | 
| 175 |  |  |  |  |  |  |  | 
| 176 |  |  |  |  |  |  | =pod | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | =back | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | =cut | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  |  | 
| 183 |  |  |  |  |  |  | 1; |