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