line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::FreeIPA::API::Magic; |
2
|
|
|
|
|
|
|
$Net::FreeIPA::API::Magic::VERSION = '3.0.1'; |
3
|
8
|
|
|
8
|
|
22532
|
use strict; |
|
8
|
|
|
|
|
9
|
|
|
8
|
|
|
|
|
317
|
|
4
|
8
|
|
|
8
|
|
27
|
use warnings; |
|
8
|
|
|
|
|
6
|
|
|
8
|
|
|
|
|
177
|
|
5
|
|
|
|
|
|
|
|
6
|
8
|
|
|
8
|
|
26
|
use Types::Serialiser; # is used by JSON::XS |
|
8
|
|
|
|
|
7
|
|
|
8
|
|
|
|
|
145
|
|
7
|
8
|
|
|
8
|
|
25
|
use JSON::XS; |
|
8
|
|
|
|
|
9
|
|
|
8
|
|
|
|
|
339
|
|
8
|
|
|
|
|
|
|
|
9
|
8
|
|
|
8
|
|
13128
|
use Net::FreeIPA::API::Data; |
|
8
|
|
|
|
|
18
|
|
|
8
|
|
|
|
|
366
|
|
10
|
|
|
|
|
|
|
|
11
|
8
|
|
|
8
|
|
45
|
use Readonly; |
|
8
|
|
|
|
|
10
|
|
|
8
|
|
|
|
|
368
|
|
12
|
|
|
|
|
|
|
|
13
|
8
|
|
|
8
|
|
30
|
use base qw(Exporter); |
|
8
|
|
|
|
|
10
|
|
|
8
|
|
|
|
|
4500
|
|
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
|
533
|
$cmd_cache = {}; |
57
|
1
|
|
|
|
|
11
|
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
|
3795
|
my ($data) = @_; |
72
|
|
|
|
|
|
|
|
73
|
5
|
|
|
|
|
8
|
my $name = $data->{name}; |
74
|
|
|
|
|
|
|
|
75
|
5
|
|
|
|
|
22
|
foreach my $key (sort keys %$data) { |
76
|
21
|
100
|
|
|
|
43
|
if ($key =~ m/^takes_/) { |
77
|
10
|
|
|
|
|
9
|
my $newdata = []; |
78
|
10
|
|
|
|
|
8
|
foreach my $tdata (@{$data->{$key}}) { |
|
10
|
|
|
|
|
13
|
|
79
|
99
|
100
|
|
|
|
103
|
if (ref($tdata) eq '') { |
80
|
1
|
|
|
|
|
2
|
my $value = $tdata; |
81
|
1
|
|
|
|
|
3
|
$value =~ s/[*+?]$//; |
82
|
1
|
|
|
|
|
6
|
$tdata = {%CACHE_TAKES_DEFAULT}; |
83
|
1
|
|
|
|
|
35
|
$tdata->{name} = $value; |
84
|
|
|
|
|
|
|
} else { |
85
|
|
|
|
|
|
|
# Arrayref of command hashrefs |
86
|
98
|
|
|
|
|
257
|
foreach my $tkey (sort keys %$tdata) { |
87
|
669
|
100
|
|
|
|
835
|
delete $tdata->{$tkey} if (! grep {$tkey eq $_} @CACHE_TAKES_KEYS); |
|
4014
|
|
|
|
|
10196
|
|
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
} |
90
|
99
|
|
|
|
|
123
|
push(@$newdata, $tdata); |
91
|
|
|
|
|
|
|
} |
92
|
10
|
|
|
|
|
18
|
$data->{$key} = $newdata; |
93
|
|
|
|
|
|
|
} else { |
94
|
11
|
100
|
|
|
|
24
|
delete $data->{$key} if (! grep {$key eq $_} @CACHE_KEYS); |
|
33
|
|
|
|
|
142
|
|
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
5
|
|
|
|
|
9
|
$cmd_cache->{$name} = $data; |
99
|
|
|
|
|
|
|
|
100
|
5
|
|
|
|
|
13
|
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
|
779
|
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
|
1132
|
my ($name) = @_; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
# Return already cached data |
133
|
4
|
100
|
|
|
|
16
|
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
|
|
|
|
|
3
|
my $data; |
139
|
3
|
|
|
|
|
11
|
my $json = $Net::FreeIPA::API::Data::API_DATA{$name}; |
140
|
3
|
100
|
|
|
|
16
|
if (! $json) { |
141
|
1
|
|
|
|
|
4
|
return {}, "retrieve name $name failed: no JSON data"; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
2
|
|
|
|
|
2
|
local $@; |
145
|
2
|
|
|
|
|
3
|
eval { |
146
|
2
|
|
|
|
|
156
|
$data = decode_json($json); |
147
|
|
|
|
|
|
|
}; |
148
|
|
|
|
|
|
|
|
149
|
2
|
50
|
|
|
|
7
|
if ($@) { |
150
|
0
|
|
|
|
|
0
|
return {}, "retrieve name $name failed decode_json with $@"; |
151
|
|
|
|
|
|
|
} else { |
152
|
2
|
|
|
|
|
9
|
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
|
19580
|
return sort keys %Net::FreeIPA::API::Data::API_DATA; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=pod |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=back |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=cut |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
1; |