]>
Commit | Line | Data |
---|---|---|
1 | package PVE::RESTHandler; | |
2 | ||
3 | use strict; | |
4 | no strict 'refs'; # our autoload requires this | |
5 | use warnings; | |
6 | use PVE::SafeSyslog; | |
7 | use PVE::Exception qw(raise raise_param_exc); | |
8 | use PVE::JSONSchema; | |
9 | use HTTP::Status qw(:constants :is status_message); | |
10 | use Text::Wrap; | |
11 | use Clone qw(clone); | |
12 | ||
13 | my $method_registry = {}; | |
14 | my $method_by_name = {}; | |
15 | my $method_path_lookup = {}; | |
16 | ||
17 | our $AUTOLOAD; # it's a package global | |
18 | ||
19 | sub api_clone_schema { | |
20 | my ($schema) = @_; | |
21 | ||
22 | my $res = {}; | |
23 | my $ref = ref($schema); | |
24 | die "not a HASH reference" if !($ref && $ref eq 'HASH'); | |
25 | ||
26 | foreach my $k (keys %$schema) { | |
27 | my $d = $schema->{$k}; | |
28 | if ($k ne 'properties') { | |
29 | $res->{$k} = ref($d) ? clone($d) : $d; | |
30 | next; | |
31 | } | |
32 | # convert indexed parameters like -net\d+ to -net[n] | |
33 | foreach my $p (keys %$d) { | |
34 | my $pd = $d->{$p}; | |
35 | if ($p =~ m/^([a-z]+)(\d+)$/) { | |
36 | my ($name, $idx) = ($1, $2); | |
37 | if ($idx == 0 && defined($d->{"${name}1"})) { | |
38 | $p = "${name}[n]"; | |
39 | } elsif (defined($d->{"${name}0"})) { | |
40 | next; # only handle once for -xx0, but only if -xx0 exists | |
41 | } | |
42 | } | |
43 | my $tmp = ref($pd) ? clone($pd) : $pd; | |
44 | # NOTE: add typetext property for more complex types, to | |
45 | # make the web api viewer code simpler | |
46 | if (!(defined($tmp->{enum}) || defined($tmp->{pattern}))) { | |
47 | my $typetext = PVE::JSONSchema::schema_get_type_text($tmp); | |
48 | if ($tmp->{type} && ($tmp->{type} ne $typetext)) { | |
49 | $tmp->{typetext} = $typetext; | |
50 | } | |
51 | } | |
52 | $res->{$k}->{$p} = $tmp; | |
53 | } | |
54 | } | |
55 | ||
56 | return $res; | |
57 | } | |
58 | ||
59 | sub api_dump_full { | |
60 | my ($tree, $index, $class, $prefix) = @_; | |
61 | ||
62 | $prefix = '' if !$prefix; | |
63 | ||
64 | my $ma = $method_registry->{$class}; | |
65 | ||
66 | foreach my $info (@$ma) { | |
67 | ||
68 | my $path = "$prefix/$info->{path}"; | |
69 | $path =~ s/\/+$//; | |
70 | ||
71 | if ($info->{subclass}) { | |
72 | api_dump_full($tree, $index, $info->{subclass}, $path); | |
73 | } else { | |
74 | next if !$path; | |
75 | ||
76 | # check if method is unique | |
77 | my $realpath = $path; | |
78 | $realpath =~ s/\{[^\}]+\}/\{\}/g; | |
79 | my $fullpath = "$info->{method} $realpath"; | |
80 | die "duplicate path '$realpath'" if $index->{$fullpath}; | |
81 | $index->{$fullpath} = $info; | |
82 | ||
83 | # insert into tree | |
84 | my $treedir = $tree; | |
85 | my $res; | |
86 | my $sp = ''; | |
87 | foreach my $dir (split('/', $path)) { | |
88 | next if !$dir; | |
89 | $sp .= "/$dir"; | |
90 | $res = (grep { $_->{text} eq $dir } @$treedir)[0]; | |
91 | if ($res) { | |
92 | $res->{children} = [] if !$res->{children}; | |
93 | $treedir = $res->{children}; | |
94 | } else { | |
95 | $res = { | |
96 | path => $sp, | |
97 | text => $dir, | |
98 | children => [], | |
99 | }; | |
100 | push @$treedir, $res; | |
101 | $treedir = $res->{children}; | |
102 | } | |
103 | } | |
104 | ||
105 | if ($res) { | |
106 | my $data = {}; | |
107 | foreach my $k (keys %$info) { | |
108 | next if $k eq 'code' || $k eq "match_name" || $k eq "match_re" || | |
109 | $k eq "path"; | |
110 | ||
111 | my $d = $info->{$k}; | |
112 | ||
113 | if ($k eq 'parameters') { | |
114 | $data->{$k} = api_clone_schema($d); | |
115 | } else { | |
116 | ||
117 | $data->{$k} = ref($d) ? clone($d) : $d; | |
118 | } | |
119 | } | |
120 | $res->{info}->{$info->{method}} = $data; | |
121 | }; | |
122 | } | |
123 | } | |
124 | }; | |
125 | ||
126 | sub api_dump_cleanup_tree { | |
127 | my ($tree) = @_; | |
128 | ||
129 | foreach my $rec (@$tree) { | |
130 | delete $rec->{children} if $rec->{children} && !scalar(@{$rec->{children}}); | |
131 | if ($rec->{children}) { | |
132 | $rec->{leaf} = 0; | |
133 | api_dump_cleanup_tree($rec->{children}); | |
134 | } else { | |
135 | $rec->{leaf} = 1; | |
136 | } | |
137 | } | |
138 | ||
139 | } | |
140 | ||
141 | sub api_dump { | |
142 | my ($class, $prefix) = @_; | |
143 | ||
144 | my $tree = []; | |
145 | ||
146 | my $index = {}; | |
147 | api_dump_full($tree, $index, $class); | |
148 | api_dump_cleanup_tree($tree); | |
149 | return $tree; | |
150 | }; | |
151 | ||
152 | sub validate_method_schemas { | |
153 | ||
154 | foreach my $class (keys %$method_registry) { | |
155 | my $ma = $method_registry->{$class}; | |
156 | ||
157 | foreach my $info (@$ma) { | |
158 | PVE::JSONSchema::validate_method_info($info); | |
159 | } | |
160 | } | |
161 | } | |
162 | ||
163 | sub register_method { | |
164 | my ($self, $info) = @_; | |
165 | ||
166 | my $match_re = []; | |
167 | my $match_name = []; | |
168 | ||
169 | my $errprefix; | |
170 | ||
171 | my $method; | |
172 | if ($info->{subclass}) { | |
173 | $errprefix = "register subclass $info->{subclass} at ${self}/$info->{path} -"; | |
174 | $method = 'SUBCLASS'; | |
175 | } else { | |
176 | $errprefix = "register method ${self}/$info->{path} -"; | |
177 | $info->{method} = 'GET' if !$info->{method}; | |
178 | $method = $info->{method}; | |
179 | } | |
180 | ||
181 | $method_path_lookup->{$self} = {} if !defined($method_path_lookup->{$self}); | |
182 | my $path_lookup = $method_path_lookup->{$self}; | |
183 | ||
184 | die "$errprefix no path" if !defined($info->{path}); | |
185 | ||
186 | foreach my $comp (split(/\/+/, $info->{path})) { | |
187 | die "$errprefix path compoment has zero length\n" if $comp eq ''; | |
188 | my ($name, $regex); | |
189 | if ($comp =~ m/^\{(\w+)(:(.*))?\}$/) { | |
190 | $name = $1; | |
191 | $regex = $3 ? $3 : '\S+'; | |
192 | push @$match_re, $regex; | |
193 | push @$match_name, $name; | |
194 | } else { | |
195 | $name = $comp; | |
196 | push @$match_re, $name; | |
197 | push @$match_name, undef; | |
198 | } | |
199 | ||
200 | if ($regex) { | |
201 | $path_lookup->{regex} = {} if !defined($path_lookup->{regex}); | |
202 | ||
203 | my $old_name = $path_lookup->{regex}->{match_name}; | |
204 | die "$errprefix found changed regex match name\n" | |
205 | if defined($old_name) && ($old_name ne $name); | |
206 | my $old_re = $path_lookup->{regex}->{match_re}; | |
207 | die "$errprefix found changed regex\n" | |
208 | if defined($old_re) && ($old_re ne $regex); | |
209 | $path_lookup->{regex}->{match_name} = $name; | |
210 | $path_lookup->{regex}->{match_re} = $regex; | |
211 | ||
212 | die "$errprefix path match error - regex and fixed items\n" | |
213 | if defined($path_lookup->{folders}); | |
214 | ||
215 | $path_lookup = $path_lookup->{regex}; | |
216 | ||
217 | } else { | |
218 | $path_lookup->{folders}->{$name} = {} if !defined($path_lookup->{folders}->{$name}); | |
219 | ||
220 | die "$errprefix path match error - regex and fixed items\n" | |
221 | if defined($path_lookup->{regex}); | |
222 | ||
223 | $path_lookup = $path_lookup->{folders}->{$name}; | |
224 | } | |
225 | } | |
226 | ||
227 | die "$errprefix duplicate method definition\n" | |
228 | if defined($path_lookup->{$method}); | |
229 | ||
230 | if ($method eq 'SUBCLASS') { | |
231 | foreach my $m (qw(GET PUT POST DELETE)) { | |
232 | die "$errprefix duplicate method definition SUBCLASS and $m\n" if $path_lookup->{$m}; | |
233 | } | |
234 | } | |
235 | $path_lookup->{$method} = $info; | |
236 | ||
237 | $info->{match_re} = $match_re; | |
238 | $info->{match_name} = $match_name; | |
239 | ||
240 | $method_by_name->{$self} = {} if !defined($method_by_name->{$self}); | |
241 | ||
242 | if ($info->{name}) { | |
243 | die "$errprefix method name already defined\n" | |
244 | if defined($method_by_name->{$self}->{$info->{name}}); | |
245 | ||
246 | $method_by_name->{$self}->{$info->{name}} = $info; | |
247 | } | |
248 | ||
249 | push @{$method_registry->{$self}}, $info; | |
250 | } | |
251 | ||
252 | sub DESTROY {}; # avoid problems with autoload | |
253 | ||
254 | sub AUTOLOAD { | |
255 | my ($this) = @_; | |
256 | ||
257 | # also see "man perldiag" | |
258 | ||
259 | my $sub = $AUTOLOAD; | |
260 | (my $method = $sub) =~ s/.*:://; | |
261 | ||
262 | my $info = $this->map_method_by_name($method); | |
263 | ||
264 | *{$sub} = sub { | |
265 | my $self = shift; | |
266 | return $self->handle($info, @_); | |
267 | }; | |
268 | goto &$AUTOLOAD; | |
269 | } | |
270 | ||
271 | sub method_attributes { | |
272 | my ($self) = @_; | |
273 | ||
274 | return $method_registry->{$self}; | |
275 | } | |
276 | ||
277 | sub map_method_by_name { | |
278 | my ($self, $name) = @_; | |
279 | ||
280 | my $info = $method_by_name->{$self}->{$name}; | |
281 | die "no such method '${self}::$name'\n" if !$info; | |
282 | ||
283 | return $info; | |
284 | } | |
285 | ||
286 | sub map_path_to_methods { | |
287 | my ($class, $stack, $uri_param, $pathmatchref) = @_; | |
288 | ||
289 | my $path_lookup = $method_path_lookup->{$class}; | |
290 | ||
291 | # Note: $pathmatchref can be used to obtain path including | |
292 | # uri patterns like '/cluster/firewall/groups/{group}'. | |
293 | # Used by pvesh to display help | |
294 | if (defined($pathmatchref)) { | |
295 | $$pathmatchref = '' if !$$pathmatchref; | |
296 | } | |
297 | ||
298 | while (defined(my $comp = shift @$stack)) { | |
299 | return undef if !$path_lookup; # not registerd? | |
300 | if ($path_lookup->{regex}) { | |
301 | my $name = $path_lookup->{regex}->{match_name}; | |
302 | my $regex = $path_lookup->{regex}->{match_re}; | |
303 | ||
304 | return undef if $comp !~ m/^($regex)$/; | |
305 | $uri_param->{$name} = $1; | |
306 | $path_lookup = $path_lookup->{regex}; | |
307 | $$pathmatchref .= '/{' . $name . '}' if defined($pathmatchref); | |
308 | } elsif ($path_lookup->{folders}) { | |
309 | $path_lookup = $path_lookup->{folders}->{$comp}; | |
310 | $$pathmatchref .= '/' . $comp if defined($pathmatchref); | |
311 | } else { | |
312 | die "internal error"; | |
313 | } | |
314 | ||
315 | return undef if !$path_lookup; | |
316 | ||
317 | if (my $info = $path_lookup->{SUBCLASS}) { | |
318 | $class = $info->{subclass}; | |
319 | ||
320 | my $fd = $info->{fragmentDelimiter}; | |
321 | ||
322 | if (defined($fd)) { | |
323 | # we only support the empty string '' (match whole URI) | |
324 | die "unsupported fragmentDelimiter '$fd'" | |
325 | if $fd ne ''; | |
326 | ||
327 | $stack = [ join ('/', @$stack) ] if scalar(@$stack) > 1; | |
328 | } | |
329 | $path_lookup = $method_path_lookup->{$class}; | |
330 | } | |
331 | } | |
332 | ||
333 | return undef if !$path_lookup; | |
334 | ||
335 | return ($class, $path_lookup); | |
336 | } | |
337 | ||
338 | sub find_handler { | |
339 | my ($class, $method, $path, $uri_param, $pathmatchref) = @_; | |
340 | ||
341 | my $stack = [ grep { length($_) > 0 } split('\/+' , $path)]; # skip empty fragments | |
342 | ||
343 | my ($handler_class, $path_info); | |
344 | eval { | |
345 | ($handler_class, $path_info) = $class->map_path_to_methods($stack, $uri_param, $pathmatchref); | |
346 | }; | |
347 | my $err = $@; | |
348 | syslog('err', $err) if $err; | |
349 | ||
350 | return undef if !($handler_class && $path_info); | |
351 | ||
352 | my $method_info = $path_info->{$method}; | |
353 | ||
354 | return undef if !$method_info; | |
355 | ||
356 | return ($handler_class, $method_info); | |
357 | } | |
358 | ||
359 | sub handle { | |
360 | my ($self, $info, $param) = @_; | |
361 | ||
362 | my $func = $info->{code}; | |
363 | ||
364 | if (!($info->{name} && $func)) { | |
365 | raise("Method lookup failed ('$info->{name}')\n", | |
366 | code => HTTP_INTERNAL_SERVER_ERROR); | |
367 | } | |
368 | ||
369 | if (my $schema = $info->{parameters}) { | |
370 | # warn "validate ". Dumper($param}) . "\n" . Dumper($schema); | |
371 | PVE::JSONSchema::validate($param, $schema); | |
372 | # untaint data (already validated) | |
373 | my $extra = delete $param->{'extra-args'}; | |
374 | while (my ($key, $val) = each %$param) { | |
375 | ($param->{$key}) = $val =~ /^(.*)$/s; | |
376 | } | |
377 | $param->{'extra-args'} = [map { /^(.*)$/ } @$extra] if $extra; | |
378 | } | |
379 | ||
380 | my $result = &$func($param); | |
381 | ||
382 | # todo: this is only to be safe - disable? | |
383 | if (my $schema = $info->{returns}) { | |
384 | PVE::JSONSchema::validate($result, $schema, "Result verification failed\n"); | |
385 | } | |
386 | ||
387 | return $result; | |
388 | } | |
389 | ||
390 | # format option, display type and description | |
391 | # $name: option name | |
392 | # $display_name: for example "-$name" of "<$name>", pass undef to use "$name:" | |
393 | # $phash: json schema property hash | |
394 | # $format: 'asciidoc', 'short', 'long' or 'full' | |
395 | # $style: 'config', 'config-sub', 'arg' or 'fixed' | |
396 | my $get_property_description = sub { | |
397 | my ($name, $style, $phash, $format, $hidepw, $fileparams) = @_; | |
398 | ||
399 | my $res = ''; | |
400 | ||
401 | $format = 'asciidoc' if !defined($format); | |
402 | ||
403 | my $descr = $phash->{description} || "no description available"; | |
404 | ||
405 | if ($phash->{verbose_description} && | |
406 | ($style eq 'config' || $style eq 'config-sub')) { | |
407 | $descr = $phash->{verbose_description}; | |
408 | } | |
409 | ||
410 | chomp $descr; | |
411 | ||
412 | my $type_text = PVE::JSONSchema::schema_get_type_text($phash, $style); | |
413 | ||
414 | if ($hidepw && $name eq 'password') { | |
415 | $type_text = ''; | |
416 | } | |
417 | ||
418 | if ($fileparams && $phash->{type} eq 'string') { | |
419 | foreach my $elem (@$fileparams) { | |
420 | if ($name eq $elem) { | |
421 | $type_text = '<filepath>'; | |
422 | last; | |
423 | } | |
424 | } | |
425 | } | |
426 | ||
427 | if ($format eq 'asciidoc') { | |
428 | ||
429 | if ($style eq 'config') { | |
430 | $res .= "`$name`: "; | |
431 | } elsif ($style eq 'config-sub') { | |
432 | $res .= "`$name`="; | |
433 | } elsif ($style eq 'arg') { | |
434 | $res .= "`--$name` "; | |
435 | } elsif ($style eq 'fixed') { | |
436 | $res .= "`<$name>`: "; | |
437 | } else { | |
438 | die "unknown style '$style'"; | |
439 | } | |
440 | ||
441 | $res .= "`$type_text` " if $type_text; | |
442 | ||
443 | if (defined(my $dv = $phash->{default})) { | |
444 | $res .= "('default =' `$dv`)"; | |
445 | } | |
446 | ||
447 | if ($style eq 'config-sub') { | |
448 | $res .= ";;\n\n"; | |
449 | } else { | |
450 | $res .= "::\n\n"; | |
451 | } | |
452 | ||
453 | my $wdescr = $descr; | |
454 | chomp $wdescr; | |
455 | $wdescr =~ s/^$/+/mg; | |
456 | ||
457 | $res .= $wdescr . "\n"; | |
458 | ||
459 | if (my $req = $phash->{requires}) { | |
460 | my $tmp .= ref($req) ? join(', ', @$req) : $req; | |
461 | $res .= "+\nNOTE: Requires option(s): `$tmp`\n"; | |
462 | } | |
463 | $res .= "\n"; | |
464 | ||
465 | } elsif ($format eq 'short' || $format eq 'long' || $format eq 'full') { | |
466 | ||
467 | my $defaulttxt = ''; | |
468 | if (defined(my $dv = $phash->{default})) { | |
469 | $defaulttxt = " (default=$dv)"; | |
470 | } | |
471 | ||
472 | my $display_name; | |
473 | if ($style eq 'config') { | |
474 | $display_name = "$name:"; | |
475 | } elsif ($style eq 'arg') { | |
476 | $display_name = "-$name"; | |
477 | } elsif ($style eq 'fixed') { | |
478 | $display_name = "<$name>"; | |
479 | } else { | |
480 | die "unknown style '$style'"; | |
481 | } | |
482 | ||
483 | my $tmp = sprintf " %-10s %s$defaulttxt\n", $display_name, "$type_text"; | |
484 | my $indend = " "; | |
485 | ||
486 | $res .= Text::Wrap::wrap('', $indend, ($tmp)); | |
487 | $res .= "\n", | |
488 | $res .= Text::Wrap::wrap($indend, $indend, ($descr)) . "\n\n"; | |
489 | ||
490 | if (my $req = $phash->{requires}) { | |
491 | my $tmp = "Requires option(s): "; | |
492 | $tmp .= ref($req) ? join(', ', @$req) : $req; | |
493 | $res .= Text::Wrap::wrap($indend, $indend, ($tmp)). "\n\n"; | |
494 | } | |
495 | ||
496 | } else { | |
497 | die "unknown format '$format'"; | |
498 | } | |
499 | ||
500 | return $res; | |
501 | }; | |
502 | ||
503 | # generate usage information for command line tools | |
504 | # | |
505 | # $name ... the name of the method | |
506 | # $prefix ... usually something like "$exename $cmd" ('pvesm add') | |
507 | # $arg_param ... list of parameters we want to get as ordered arguments | |
508 | # on the command line (or single parameter name for lists) | |
509 | # $fixed_param ... do not generate and info about those parameters | |
510 | # $format: | |
511 | # 'long' ... default (text, list all options) | |
512 | # 'short' ... command line only (text, one line) | |
513 | # 'full' ... text, include description | |
514 | # 'asciidoc' ... generate asciidoc for man pages (like 'full') | |
515 | # $hidepw ... hide password option (use this if you provide a read passwork callback) | |
516 | # $stringfilemap ... mapping for string parameters to file path parameters | |
517 | sub usage_str { | |
518 | my ($self, $name, $prefix, $arg_param, $fixed_param, $format, $hidepw, $stringfilemap) = @_; | |
519 | ||
520 | $format = 'long' if !$format; | |
521 | ||
522 | my $info = $self->map_method_by_name($name); | |
523 | my $schema = $info->{parameters}; | |
524 | my $prop = $schema->{properties}; | |
525 | ||
526 | my $out = ''; | |
527 | ||
528 | my $arg_hash = {}; | |
529 | ||
530 | my $args = ''; | |
531 | ||
532 | $arg_param = [ $arg_param ] if $arg_param && !ref($arg_param); | |
533 | ||
534 | foreach my $p (@$arg_param) { | |
535 | next if !$prop->{$p}; # just to be sure | |
536 | my $pd = $prop->{$p}; | |
537 | ||
538 | $arg_hash->{$p} = 1; | |
539 | $args .= " " if $args; | |
540 | if ($pd->{format} && $pd->{format} =~ m/-list/) { | |
541 | $args .= "{<$p>}"; | |
542 | } else { | |
543 | $args .= $pd->{optional} ? "[<$p>]" : "<$p>"; | |
544 | } | |
545 | } | |
546 | ||
547 | my $argdescr = ''; | |
548 | foreach my $k (@$arg_param) { | |
549 | next if defined($fixed_param->{$k}); # just to be sure | |
550 | next if !$prop->{$k}; # just to be sure | |
551 | $argdescr .= &$get_property_description($k, 'fixed', $prop->{$k}, $format, 0); | |
552 | } | |
553 | ||
554 | my $idx_param = {}; # -vlan\d+ -scsi\d+ | |
555 | ||
556 | my $opts = ''; | |
557 | foreach my $k (sort keys %$prop) { | |
558 | next if $arg_hash->{$k}; | |
559 | next if defined($fixed_param->{$k}); | |
560 | ||
561 | my $type_text = $prop->{$k}->{type} || 'string'; | |
562 | ||
563 | next if $hidepw && ($k eq 'password') && !$prop->{$k}->{optional}; | |
564 | ||
565 | my $base = $k; | |
566 | if ($k =~ m/^([a-z]+)(\d+)$/) { | |
567 | my ($name, $idx) = ($1, $2); | |
568 | next if $idx_param->{$name}; | |
569 | if ($idx == 0 && defined($prop->{"${name}1"})) { | |
570 | $idx_param->{$name} = 1; | |
571 | $base = "${name}[n]"; | |
572 | } | |
573 | } | |
574 | ||
575 | my $mapping = defined($stringfilemap) ? &$stringfilemap($name) : undef; | |
576 | $opts .= &$get_property_description($base, 'arg', $prop->{$k}, $format, | |
577 | $hidepw, $mapping); | |
578 | ||
579 | if (!$prop->{$k}->{optional}) { | |
580 | $args .= " " if $args; | |
581 | $args .= "--$base <$type_text>" | |
582 | } | |
583 | } | |
584 | ||
585 | if ($format eq 'asciidoc') { | |
586 | $out .= "*${prefix}*"; | |
587 | $out .= " `$args`" if $args; | |
588 | $out .= $opts ? " `[OPTIONS]`\n" : "\n"; | |
589 | } else { | |
590 | $out .= "USAGE: " if $format ne 'short'; | |
591 | $out .= "$prefix $args"; | |
592 | $out .= $opts ? " [OPTIONS]\n" : "\n"; | |
593 | } | |
594 | ||
595 | return $out if $format eq 'short'; | |
596 | ||
597 | if ($info->{description}) { | |
598 | if ($format eq 'asciidoc') { | |
599 | my $desc = Text::Wrap::wrap('', '', ($info->{description})); | |
600 | $out .= "\n$desc\n\n"; | |
601 | } elsif ($format eq 'full') { | |
602 | my $desc = Text::Wrap::wrap(' ', ' ', ($info->{description})); | |
603 | $out .= "\n$desc\n\n"; | |
604 | } | |
605 | } | |
606 | ||
607 | $out .= $argdescr if $argdescr; | |
608 | ||
609 | $out .= $opts if $opts; | |
610 | ||
611 | return $out; | |
612 | } | |
613 | ||
614 | # generate docs from JSON schema properties | |
615 | sub dump_properties { | |
616 | my ($prop, $format, $style, $filterFn) = @_; | |
617 | ||
618 | my $raw = ''; | |
619 | ||
620 | $style //= 'config'; | |
621 | ||
622 | my $idx_param = {}; # -vlan\d+ -scsi\d+ | |
623 | ||
624 | foreach my $k (sort keys %$prop) { | |
625 | my $phash = $prop->{$k}; | |
626 | ||
627 | next if defined($filterFn) && &$filterFn($k, $phash); | |
628 | next if $phash->{alias}; | |
629 | ||
630 | my $base = $k; | |
631 | if ($k =~ m/^([a-z]+)(\d+)$/) { | |
632 | my ($name, $idx) = ($1, $2); | |
633 | next if $idx_param->{$name}; | |
634 | if ($idx == 0 && defined($prop->{"${name}1"})) { | |
635 | $idx_param->{$name} = 1; | |
636 | $base = "${name}[n]"; | |
637 | } | |
638 | } | |
639 | ||
640 | $raw .= &$get_property_description($base, $style, $phash, $format, 0); | |
641 | ||
642 | next if $style ne 'config'; | |
643 | ||
644 | my $prop_fmt = $phash->{format}; | |
645 | next if !$prop_fmt; | |
646 | ||
647 | if (ref($prop_fmt) ne 'HASH') { | |
648 | $prop_fmt = PVE::JSONSchema::get_format($prop_fmt); | |
649 | } | |
650 | ||
651 | next if !(ref($prop_fmt) && (ref($prop_fmt) eq 'HASH')); | |
652 | ||
653 | $raw .= dump_properties($prop_fmt, $format, 'config-sub') | |
654 | ||
655 | } | |
656 | ||
657 | return $raw; | |
658 | } | |
659 | ||
660 | my $replace_file_names_with_contents = sub { | |
661 | my ($param, $mapping) = @_; | |
662 | ||
663 | if ($mapping) { | |
664 | foreach my $elem ( @$mapping ) { | |
665 | $param->{$elem} = PVE::Tools::file_get_contents($param->{$elem}) | |
666 | if defined($param->{$elem}); | |
667 | } | |
668 | } | |
669 | ||
670 | return $param; | |
671 | }; | |
672 | ||
673 | sub cli_handler { | |
674 | my ($self, $prefix, $name, $args, $arg_param, $fixed_param, $pwcallback, $stringfilemap) = @_; | |
675 | ||
676 | my $info = $self->map_method_by_name($name); | |
677 | ||
678 | my $res; | |
679 | eval { | |
680 | my $param = PVE::JSONSchema::get_options($info->{parameters}, $args, $arg_param, $fixed_param, $pwcallback); | |
681 | &$replace_file_names_with_contents($param, &$stringfilemap($name)) | |
682 | if defined($stringfilemap); | |
683 | $res = $self->handle($info, $param); | |
684 | }; | |
685 | if (my $err = $@) { | |
686 | my $ec = ref($err); | |
687 | ||
688 | die $err if !$ec || $ec ne "PVE::Exception" || !$err->is_param_exc(); | |
689 | ||
690 | $err->{usage} = $self->usage_str($name, $prefix, $arg_param, $fixed_param, 'short', $pwcallback, $stringfilemap); | |
691 | ||
692 | die $err; | |
693 | } | |
694 | ||
695 | return $res; | |
696 | } | |
697 | ||
698 | # utility methods | |
699 | # note: this modifies the original hash by adding the id property | |
700 | sub hash_to_array { | |
701 | my ($hash, $idprop) = @_; | |
702 | ||
703 | my $res = []; | |
704 | return $res if !$hash; | |
705 | ||
706 | foreach my $k (keys %$hash) { | |
707 | $hash->{$k}->{$idprop} = $k; | |
708 | push @$res, $hash->{$k}; | |
709 | } | |
710 | ||
711 | return $res; | |
712 | } | |
713 | ||
714 | 1; |