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