]>
Commit | Line | Data |
---|---|---|
29505e2c DM |
1 | package PVE::APIClient::Helpers; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
ab79ce78 | 6 | use Storable; |
29505e2c | 7 | use JSON; |
df89fcb9 DM |
8 | use File::Path qw(make_path); |
9 | ||
51da1502 | 10 | use PVE::APIClient::JSONSchema; |
29505e2c | 11 | use PVE::APIClient::Exception qw(raise); |
29505e2c DM |
12 | use Encode::Locale; |
13 | use Encode; | |
14 | use HTTP::Status qw(:constants); | |
15 | ||
16 | my $pve_api_definition; | |
29505e2c | 17 | |
ab79ce78 | 18 | my $pve_api_definition_fn = "/usr/share/pve-client/pve-api-definition.dat"; |
29505e2c | 19 | |
b133a905 DM |
20 | my $method_map = { |
21 | create => 'POST', | |
22 | set => 'PUT', | |
23 | get => 'GET', | |
24 | delete => 'DELETE', | |
25 | }; | |
26 | ||
b5aeedb0 | 27 | my $default_output_format = 'text'; |
61ad3df5 DM |
28 | my $client_output_format = $default_output_format; |
29 | ||
30 | sub set_output_format { | |
31 | my ($format) = @_; | |
32 | ||
33 | if (!defined($format)) { | |
34 | $client_output_format = $default_output_format; | |
35 | } else { | |
36 | $client_output_format = $format; | |
37 | } | |
38 | } | |
39 | ||
40 | sub get_output_format { | |
41 | return $client_output_format; | |
42 | } | |
43 | ||
4db9ff67 DM |
44 | sub print_result { |
45 | my ($data, $result_schema) = @_; | |
46 | ||
47 | my $format = get_output_format(); | |
48 | ||
49 | return if $result_schema->{type} eq 'null'; | |
50 | ||
51 | # TODO: implement different output formats ($format) | |
52 | ||
53 | if ($format eq 'json') { | |
54 | print to_json($data, {utf8 => 1, allow_nonref => 1, canonical => 1, pretty => 1 }); | |
b5aeedb0 | 55 | } elsif ($format eq 'text') { |
4db9ff67 DM |
56 | my $type = $result_schema->{type}; |
57 | if ($type eq 'object') { | |
58 | die "implement me"; | |
59 | } elsif ($type eq 'array') { | |
60 | my $item_type = $result_schema->{items}->{type}; | |
61 | if ($item_type eq 'object') { | |
62 | die "implement me"; | |
63 | } elsif ($item_type eq 'array') { | |
64 | die "implement me"; | |
65 | } else { | |
66 | foreach my $el (@$data) { | |
67 | print "$el\n" | |
68 | } | |
69 | } | |
70 | } else { | |
71 | print "$data\n"; | |
72 | } | |
73 | } else { | |
74 | die "internal error: unknown output format"; # should not happen | |
75 | } | |
76 | } | |
77 | ||
51da1502 DM |
78 | |
79 | my $__real_remove_formats; $__real_remove_formats = sub { | |
80 | my ($properties) = @_; | |
81 | ||
82 | foreach my $pname (keys %$properties) { | |
83 | if (my $d = $properties->{$pname}) { | |
84 | if (defined(my $format = $d->{format})) { | |
85 | if (ref($format)) { | |
86 | $__real_remove_formats->($format); | |
87 | } elsif (!PVE::APIClient::JSONSchema::get_format($format)) { | |
88 | # simply remove unknown format definitions | |
89 | delete $d->{format}; | |
90 | } | |
91 | } | |
92 | } | |
93 | } | |
94 | }; | |
95 | ||
96 | my $remove_unknown_formats; $remove_unknown_formats = sub { | |
97 | my ($tree) = @_; | |
98 | ||
99 | my $class = ref($tree); | |
100 | return if !$class; | |
101 | ||
102 | if ($class eq 'ARRAY') { | |
103 | foreach my $el (@$tree) { | |
104 | $remove_unknown_formats->($el); | |
105 | } | |
106 | } elsif ($class eq 'HASH') { | |
107 | if (my $info = $tree->{info}) { | |
108 | for my $method (qw(GET PUT PUSH DELETE)) { | |
109 | next if !$info->{$method}; | |
110 | my $properties = $info->{$method}->{parameters}->{properties}; | |
111 | $__real_remove_formats->($properties) if $properties; | |
112 | } | |
113 | } | |
114 | if ($tree->{children}) { | |
115 | $remove_unknown_formats->($tree->{children}); | |
116 | } | |
117 | } | |
118 | return; | |
119 | }; | |
120 | ||
29505e2c DM |
121 | sub get_api_definition { |
122 | ||
123 | if (!defined($pve_api_definition)) { | |
29505e2c DM |
124 | open(my $fh, '<', $pve_api_definition_fn) || |
125 | die "unable to open '$pve_api_definition_fn' - $!\n"; | |
ab79ce78 | 126 | $pve_api_definition = Storable::fd_retrieve($fh); |
51da1502 DM |
127 | |
128 | $remove_unknown_formats->($pve_api_definition); | |
29505e2c DM |
129 | } |
130 | ||
29505e2c DM |
131 | return $pve_api_definition; |
132 | } | |
133 | ||
520f543e DM |
134 | my $map_path_to_info = sub { |
135 | my ($child_list, $stack, $uri_param) = @_; | |
136 | ||
137 | while (defined(my $comp = shift @$stack)) { | |
138 | foreach my $child (@$child_list) { | |
139 | my $text = $child->{text}; | |
140 | ||
141 | if ($text eq $comp) { | |
142 | # found | |
143 | } elsif ($text =~ m/^\{(\S+)\}$/) { | |
144 | # found | |
145 | $uri_param->{$1} = $comp; | |
146 | } else { | |
147 | next; # text next child | |
148 | } | |
149 | if ($child->{leaf} || !scalar(@$stack)) { | |
150 | return $child; | |
151 | } else { | |
152 | $child_list = $child->{children}; | |
153 | last; # test next path component | |
154 | } | |
155 | } | |
156 | } | |
157 | return undef; | |
158 | }; | |
29505e2c | 159 | |
520f543e DM |
160 | sub find_method_info { |
161 | my ($path, $method, $uri_param, $noerr) = @_; | |
29505e2c | 162 | |
520f543e | 163 | $uri_param //= {}; |
635c0511 | 164 | |
520f543e | 165 | my $stack = [ grep { length($_) > 0 } split('\/+' , $path)]; # skip empty fragments |
29505e2c | 166 | |
520f543e | 167 | my $child = $map_path_to_info->(get_api_definition(), $stack, $uri_param); |
635c0511 | 168 | |
520f543e | 169 | if (!($child && $child->{info} && $child->{info}->{$method})) { |
635c0511 | 170 | return undef if $noerr; |
29505e2c | 171 | die "unable to find API method '$method' for path '$path'\n"; |
635c0511 | 172 | } |
29505e2c | 173 | |
520f543e | 174 | return $child->{info}->{$method}; |
29505e2c DM |
175 | } |
176 | ||
635c0511 DM |
177 | sub complete_api_call_options { |
178 | my ($cmd, $prop, $prev, $cur, $args) = @_; | |
179 | ||
180 | my $print_result = sub { | |
181 | foreach my $p (@_) { | |
182 | print "$p\n" if $p =~ m/^$cur/; | |
183 | } | |
184 | }; | |
185 | ||
186 | my $print_parameter_completion = sub { | |
187 | my ($pname) = @_; | |
188 | my $d = $prop->{$pname}; | |
189 | if ($d->{completion}) { | |
190 | my $vt = ref($d->{completion}); | |
191 | if ($vt eq 'CODE') { | |
192 | my $res = $d->{completion}->($cmd, $pname, $cur, $args); | |
193 | &$print_result(@$res); | |
194 | } | |
195 | } elsif ($d->{type} eq 'boolean') { | |
196 | &$print_result('0', '1'); | |
197 | } elsif ($d->{enum}) { | |
198 | &$print_result(@{$d->{enum}}); | |
199 | } | |
200 | }; | |
201 | ||
202 | my @option_list = (); | |
203 | foreach my $key (keys %$prop) { | |
204 | push @option_list, "--$key"; | |
205 | } | |
206 | ||
207 | if ($cur =~ m/^-/) { | |
208 | &$print_result(@option_list); | |
209 | return; | |
210 | } | |
211 | ||
212 | if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) { | |
213 | my $pname = $1; | |
214 | &$print_parameter_completion($pname); | |
215 | return; | |
216 | } | |
217 | ||
218 | &$print_result(@option_list); | |
219 | } | |
220 | ||
221 | sub complete_api_path { | |
222 | my ($text) = @_; | |
223 | ||
224 | get_api_definition(); # make sure API data is loaded | |
225 | ||
226 | $text =~ s!^/!!; | |
227 | ||
228 | my ($dir, $rest) = $text =~ m|^(?:(.*)/)?(?:([^/]*))?$|; | |
229 | ||
230 | my $info; | |
231 | if (!defined($dir)) { | |
232 | $dir = ''; | |
233 | $info = { children => $pve_api_definition }; | |
234 | } else { | |
520f543e DM |
235 | my $stack = [ grep { length($_) > 0 } split('\/+' , $dir)]; # skip empty fragments |
236 | $info = $map_path_to_info->($pve_api_definition, $stack, {}); | |
635c0511 DM |
237 | } |
238 | ||
b133a905 | 239 | my $res = []; |
635c0511 | 240 | if ($info) { |
520f543e | 241 | my $prefix = length($dir) ? "/$dir/" : '/'; |
635c0511 DM |
242 | if (my $children = $info->{children}) { |
243 | foreach my $c (@$children) { | |
520f543e DM |
244 | my $ctext = $c->{text}; |
245 | if ($ctext =~ m/^\{(\S+)\}$/) { | |
246 | push @$res, "$prefix$ctext"; | |
247 | push @$res, "$prefix$ctext/"; | |
248 | if (length($rest)) { | |
249 | push @$res, "$prefix$rest"; | |
250 | push @$res, "$prefix$rest/"; | |
251 | } | |
252 | } elsif ($ctext =~ m/^\Q$rest/) { | |
253 | push @$res, "$prefix$ctext"; | |
254 | push @$res, "$prefix$ctext/" if $c->{children}; | |
635c0511 DM |
255 | } |
256 | } | |
257 | } | |
258 | } | |
520f543e | 259 | |
b133a905 DM |
260 | return $res; |
261 | } | |
262 | ||
263 | # test for command lines with api calls (or similar bash completion calls): | |
264 | # example1: pveclient api get remote1 /cluster | |
265 | sub extract_path_info { | |
520f543e | 266 | my ($uri_param) = @_; |
b133a905 DM |
267 | |
268 | my $info; | |
269 | ||
270 | my $test_path_properties = sub { | |
271 | my ($args) = @_; | |
272 | ||
273 | return if scalar(@$args) < 5; | |
274 | return if $args->[1] ne 'api'; | |
275 | ||
276 | my $path = $args->[4]; | |
277 | if (my $method = $method_map->{$args->[2]}) { | |
520f543e | 278 | $info = find_method_info($path, $method, $uri_param, 1); |
b133a905 DM |
279 | } |
280 | }; | |
281 | ||
282 | if (defined(my $cmd = $ARGV[0])) { | |
283 | if ($cmd eq 'api') { | |
284 | $test_path_properties->([$0, @ARGV]); | |
285 | } elsif ($cmd eq 'bashcomplete') { | |
286 | my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT}); | |
ca3269f4 | 287 | my $args = PVE::APIClient::Tools::split_args($cmdline); |
b133a905 DM |
288 | $test_path_properties->($args); |
289 | } | |
290 | } | |
291 | ||
292 | return $info; | |
635c0511 DM |
293 | } |
294 | ||
2f964a75 RJ |
295 | sub get_vmid_resource { |
296 | my ($conn, $vmid) = @_; | |
297 | ||
298 | my $resources = $conn->get('api2/json/cluster/resources', {type => 'vm'}); | |
299 | ||
300 | my $resource; | |
301 | for my $tmp (@$resources) { | |
302 | if ($tmp->{vmid} eq $vmid) { | |
303 | $resource = $tmp; | |
304 | last; | |
305 | } | |
306 | } | |
307 | ||
308 | if (!defined($resource)) { | |
309 | die "\"$vmid\" not found"; | |
310 | } | |
311 | ||
312 | return $resource; | |
313 | } | |
314 | ||
315 | sub poll_task { | |
316 | my ($conn, $node, $upid) = @_; | |
317 | ||
318 | my $path = "api2/json/nodes/$node/tasks/$upid/status"; | |
319 | ||
320 | my $task_status; | |
321 | while(1) { | |
322 | $task_status = $conn->get($path, {}); | |
323 | ||
324 | if ($task_status->{status} eq "stopped") { | |
325 | last; | |
326 | } | |
327 | ||
328 | sleep(10); | |
329 | } | |
330 | ||
331 | return $task_status->{exitstatus}; | |
332 | } | |
333 | ||
0ca37028 DM |
334 | sub configuration_directory { |
335 | ||
336 | my $home = $ENV{HOME} // ''; | |
337 | my $xdg = $ENV{XDG_CONFIG_HOME} // ''; | |
338 | ||
339 | my $subdir = "pveclient"; | |
340 | ||
341 | return "$xdg/$subdir" if length($xdg); | |
342 | ||
343 | return "$home/.config/$subdir" if length($home); | |
344 | ||
345 | die "neither XDG_CONFIG_HOME nor HOME environment variable set\n"; | |
346 | } | |
347 | ||
df89fcb9 DM |
348 | my $ticket_cache_filename = "/.tickets"; |
349 | ||
350 | sub ticket_cache_lookup { | |
351 | my ($remote) = @_; | |
352 | ||
353 | my $dir = configuration_directory(); | |
354 | my $filename = "$dir/$ticket_cache_filename"; | |
355 | ||
356 | my $data = {}; | |
357 | eval { $data = from_json(PVE::APIClient::Tools::file_get_contents($filename)); }; | |
358 | # ignore errors | |
359 | ||
360 | my $ticket = $data->{$remote}; | |
361 | return undef if !defined($ticket); | |
362 | ||
363 | my $min_age = - 60; | |
364 | my $max_age = 3600*2 - 60; | |
365 | ||
366 | if ($ticket =~ m/:([a-fA-F0-9]{8})::/) { | |
367 | my $ttime = hex($1); | |
368 | my $ctime = time(); | |
369 | my $age = $ctime - $ttime; | |
370 | ||
371 | return $ticket if ($age > $min_age) && ($age < $max_age); | |
372 | } | |
373 | ||
374 | return undef; | |
375 | } | |
376 | ||
377 | sub ticket_cache_update { | |
378 | my ($remote, $ticket) = @_; | |
379 | ||
380 | my $dir = configuration_directory(); | |
381 | my $filename = "$dir/$ticket_cache_filename"; | |
382 | ||
383 | my $code = sub { | |
384 | make_path($dir); | |
385 | my $data = {}; | |
386 | if (-f $filename) { | |
387 | my $raw = PVE::APIClient::Tools::file_get_contents($filename); | |
388 | eval { $data = from_json($raw); }; | |
389 | # ignore errors | |
390 | } | |
391 | $data->{$remote} = $ticket; | |
392 | ||
393 | PVE::APIClient::Tools::file_set_contents($filename, to_json($data), 0600); | |
394 | }; | |
395 | ||
396 | PVE::APIClient::Tools::lock_file($filename, undef, $code); | |
397 | die $@ if $@; | |
398 | } | |
399 | ||
400 | ||
29505e2c | 401 | 1; |