]> git.proxmox.com Git - pve-common.git/blame - src/PVE/RESTHandler.pm
bump version to 8.2.1
[pve-common.git] / src / PVE / RESTHandler.pm
CommitLineData
e143e9d8
DM
1package PVE::RESTHandler;
2
3use strict;
e143e9d8 4use warnings;
0827edde
TL
5
6use Clone qw(clone);
7use HTTP::Status qw(:constants :is status_message);
8use Text::Wrap;
9
e143e9d8
DM
10use PVE::Exception qw(raise raise_param_exc);
11use PVE::JSONSchema;
0827edde 12use PVE::SafeSyslog;
dfc6cff8 13use PVE::Tools;
e143e9d8
DM
14
15my $method_registry = {};
16my $method_by_name = {};
c1089794 17my $method_path_lookup = {};
e143e9d8
DM
18
19our $AUTOLOAD; # it's a package global
20
62c66c47
DM
21our $standard_output_options = {
22 'output-format' => PVE::JSONSchema::get_standard_option('pve-output-format'),
23 noheader => {
24 description => "Do not show column headers (for 'text' format).",
25 type => 'boolean',
26 optional => 1,
3b44baed 27 default => 0,
62c66c47
DM
28 },
29 noborder => {
30 description => "Do not draw borders (for 'text' format).",
31 type => 'boolean',
32 optional => 1,
3b44baed 33 default => 0,
62c66c47
DM
34 },
35 quiet => {
36 description => "Suppress printing results.",
37 type => 'boolean',
38 optional => 1,
39 },
40 'human-readable' => {
41 description => "Call output rendering functions to produce human readable text.",
42 type => 'boolean',
43 optional => 1,
44 default => 1,
45 }
46};
47
e143e9d8 48sub api_clone_schema {
d01452bd 49 my ($schema, $no_typetext) = @_;
e143e9d8
DM
50
51 my $res = {};
52 my $ref = ref($schema);
53 die "not a HASH reference" if !($ref && $ref eq 'HASH');
54
55 foreach my $k (keys %$schema) {
56 my $d = $schema->{$k};
57 if ($k ne 'properties') {
4be9b5f7 58 $res->{$k} = ref($d) ? clone($d) : $d;
e143e9d8
DM
59 next;
60 }
61 # convert indexed parameters like -net\d+ to -net[n]
62 foreach my $p (keys %$d) {
63 my $pd = $d->{$p};
64 if ($p =~ m/^([a-z]+)(\d+)$/) {
b546f33e 65 my ($name, $idx) = ($1, $2);
b54ad320 66 if ($idx == 0 && defined($d->{"${name}1"})) {
b546f33e 67 $p = "${name}[n]";
34d89996 68 } elsif ($idx > 0 && defined($d->{"${name}0"})) {
2c2a5fd3 69 next; # only handle once for -xx0, but only if -xx0 exists
e143e9d8
DM
70 }
71 }
534d4270 72 my $tmp = ref($pd) ? clone($pd) : $pd;
a45a1df1 73 # NOTE: add typetext property for complexer types, to make the web api-viewer code simpler
d01452bd 74 if (!$no_typetext && !(defined($tmp->{enum}) || defined($tmp->{pattern}))) {
534d4270
DM
75 my $typetext = PVE::JSONSchema::schema_get_type_text($tmp);
76 if ($tmp->{type} && ($tmp->{type} ne $typetext)) {
77 $tmp->{typetext} = $typetext;
78 }
79 }
80 $res->{$k}->{$p} = $tmp;
e143e9d8
DM
81 }
82 }
83
84 return $res;
85}
86
87sub api_dump_full {
1e5ecf7b 88 my ($tree, $index, $class, $prefix, $raw_dump) = @_;
e143e9d8
DM
89
90 $prefix = '' if !$prefix;
91
92 my $ma = $method_registry->{$class};
93
94 foreach my $info (@$ma) {
95
96 my $path = "$prefix/$info->{path}";
97 $path =~ s/\/+$//;
98
99 if ($info->{subclass}) {
1e5ecf7b 100 api_dump_full($tree, $index, $info->{subclass}, $path, $raw_dump);
e143e9d8
DM
101 } else {
102 next if !$path;
103
104 # check if method is unique
105 my $realpath = $path;
106 $realpath =~ s/\{[^\}]+\}/\{\}/g;
107 my $fullpath = "$info->{method} $realpath";
108 die "duplicate path '$realpath'" if $index->{$fullpath};
109 $index->{$fullpath} = $info;
110
111 # insert into tree
112 my $treedir = $tree;
113 my $res;
114 my $sp = '';
115 foreach my $dir (split('/', $path)) {
116 next if !$dir;
117 $sp .= "/$dir";
118 $res = (grep { $_->{text} eq $dir } @$treedir)[0];
119 if ($res) {
120 $res->{children} = [] if !$res->{children};
121 $treedir = $res->{children};
122 } else {
123 $res = {
124 path => $sp,
125 text => $dir,
126 children => [],
127 };
128 push @$treedir, $res;
129 $treedir = $res->{children};
130 }
131 }
132
133 if ($res) {
134 my $data = {};
135 foreach my $k (keys %$info) {
136 next if $k eq 'code' || $k eq "match_name" || $k eq "match_re" ||
137 $k eq "path";
138
139 my $d = $info->{$k};
e143e9d8 140
1e5ecf7b
DM
141 if ($raw_dump) {
142 $data->{$k} = $d;
143 } else {
144 if ($k eq 'parameters') {
145 $data->{$k} = api_clone_schema($d);
d01452bd
DM
146 } elsif ($k eq 'returns') {
147 $data->{$k} = api_clone_schema($d, 1);
1e5ecf7b
DM
148 } else {
149 $data->{$k} = ref($d) ? clone($d) : $d;
150 }
e143e9d8 151 }
9bbc4e17 152 }
e143e9d8
DM
153 $res->{info}->{$info->{method}} = $data;
154 };
155 }
156 }
157};
158
159sub api_dump_cleanup_tree {
160 my ($tree) = @_;
161
162 foreach my $rec (@$tree) {
163 delete $rec->{children} if $rec->{children} && !scalar(@{$rec->{children}});
164 if ($rec->{children}) {
165 $rec->{leaf} = 0;
166 api_dump_cleanup_tree($rec->{children});
167 } else {
168 $rec->{leaf} = 1;
169 }
170 }
171
172}
173
903ea3ff
DM
174# api_dump_remove_refs: prepare API tree for use with to_json($tree)
175sub api_dump_remove_refs {
176 my ($tree) = @_;
177
178 my $class = ref($tree);
179 return $tree if !$class;
180
181 if ($class eq 'ARRAY') {
182 my $res = [];
183 foreach my $el (@$tree) {
184 push @$res, api_dump_remove_refs($el);
185 }
186 return $res;
187 } elsif ($class eq 'HASH') {
188 my $res = {};
189 foreach my $k (keys %$tree) {
190 if (my $itemclass = ref($tree->{$k})) {
191 if ($itemclass eq 'CODE') {
26db1a61 192 next if $k eq 'completion' || $k eq 'proxyto_callback';
903ea3ff
DM
193 }
194 $res->{$k} = api_dump_remove_refs($tree->{$k});
195 } else {
196 $res->{$k} = $tree->{$k};
197 }
198 }
199 return $res;
200 } elsif ($class eq 'Regexp') {
201 return "$tree"; # return string representation
202 } else {
203 die "unknown class '$class'\n";
204 }
205}
206
e143e9d8 207sub api_dump {
1e5ecf7b 208 my ($class, $prefix, $raw_dump) = @_;
e143e9d8
DM
209
210 my $tree = [];
211
212 my $index = {};
1e5ecf7b 213 api_dump_full($tree, $index, $class, $prefix, $raw_dump);
e143e9d8
DM
214 api_dump_cleanup_tree($tree);
215 return $tree;
216};
217
218sub validate_method_schemas {
219
220 foreach my $class (keys %$method_registry) {
221 my $ma = $method_registry->{$class};
222
223 foreach my $info (@$ma) {
224 PVE::JSONSchema::validate_method_info($info);
225 }
226 }
227}
228
229sub register_method {
230 my ($self, $info) = @_;
231
232 my $match_re = [];
233 my $match_name = [];
234
c1089794
DM
235 my $errprefix;
236
237 my $method;
238 if ($info->{subclass}) {
239 $errprefix = "register subclass $info->{subclass} at ${self}/$info->{path} -";
240 $method = 'SUBCLASS';
241 } else {
242 $errprefix = "register method ${self}/$info->{path} -";
243 $info->{method} = 'GET' if !$info->{method};
244 $method = $info->{method};
4c72ade0
FG
245
246 # apply default value
247 $info->{allowtoken} = 1 if !defined($info->{allowtoken});
c1089794
DM
248 }
249
250 $method_path_lookup->{$self} = {} if !defined($method_path_lookup->{$self});
251 my $path_lookup = $method_path_lookup->{$self};
252
253 die "$errprefix no path" if !defined($info->{path});
9bbc4e17 254
e143e9d8 255 foreach my $comp (split(/\/+/, $info->{path})) {
c1089794
DM
256 die "$errprefix path compoment has zero length\n" if $comp eq '';
257 my ($name, $regex);
dfc8643d 258 if ($comp =~ m/^\{([\w-]+)(?::(.*))?\}$/) {
c1089794 259 $name = $1;
a6766fed 260 $regex = $2 ? $2 : '\S+';
c1089794
DM
261 push @$match_re, $regex;
262 push @$match_name, $name;
e143e9d8 263 } else {
c1089794
DM
264 $name = $comp;
265 push @$match_re, $name;
266 push @$match_name, undef;
267 }
268
269 if ($regex) {
9bbc4e17 270 $path_lookup->{regex} = {} if !defined($path_lookup->{regex});
c1089794
DM
271
272 my $old_name = $path_lookup->{regex}->{match_name};
273 die "$errprefix found changed regex match name\n"
274 if defined($old_name) && ($old_name ne $name);
275 my $old_re = $path_lookup->{regex}->{match_re};
276 die "$errprefix found changed regex\n"
277 if defined($old_re) && ($old_re ne $regex);
278 $path_lookup->{regex}->{match_name} = $name;
279 $path_lookup->{regex}->{match_re} = $regex;
9bbc4e17 280
c1089794
DM
281 die "$errprefix path match error - regex and fixed items\n"
282 if defined($path_lookup->{folders});
283
284 $path_lookup = $path_lookup->{regex};
9bbc4e17 285
c1089794 286 } else {
9bbc4e17 287 $path_lookup->{folders}->{$name} = {} if !defined($path_lookup->{folders}->{$name});
c1089794
DM
288
289 die "$errprefix path match error - regex and fixed items\n"
290 if defined($path_lookup->{regex});
291
292 $path_lookup = $path_lookup->{folders}->{$name};
e143e9d8
DM
293 }
294 }
295
9bbc4e17 296 die "$errprefix duplicate method definition\n"
c1089794
DM
297 if defined($path_lookup->{$method});
298
1dad1ca5
DM
299 if ($method eq 'SUBCLASS') {
300 foreach my $m (qw(GET PUT POST DELETE)) {
301 die "$errprefix duplicate method definition SUBCLASS and $m\n" if $path_lookup->{$m};
302 }
303 }
c1089794
DM
304 $path_lookup->{$method} = $info;
305
e143e9d8
DM
306 $info->{match_re} = $match_re;
307 $info->{match_name} = $match_name;
308
309 $method_by_name->{$self} = {} if !defined($method_by_name->{$self});
310
311 if ($info->{name}) {
c1089794 312 die "$errprefix method name already defined\n"
e143e9d8
DM
313 if defined($method_by_name->{$self}->{$info->{name}});
314
315 $method_by_name->{$self}->{$info->{name}} = $info;
316 }
317
318 push @{$method_registry->{$self}}, $info;
319}
320
f1fb34a0
DM
321sub DESTROY {}; # avoid problems with autoload
322
e143e9d8
DM
323sub AUTOLOAD {
324 my ($this) = @_;
325
326 # also see "man perldiag"
9bbc4e17 327
e143e9d8
DM
328 my $sub = $AUTOLOAD;
329 (my $method = $sub) =~ s/.*:://;
330
e143e9d8
DM
331 my $info = $this->map_method_by_name($method);
332
28932ade
TL
333 {
334 no strict 'refs'; ## no critic (ProhibitNoStrict)
335 *{$sub} = sub {
336 my $self = shift;
337 return $self->handle($info, @_);
338 };
339 }
e143e9d8
DM
340 goto &$AUTOLOAD;
341}
342
343sub method_attributes {
344 my ($self) = @_;
345
346 return $method_registry->{$self};
347}
348
349sub map_method_by_name {
350 my ($self, $name) = @_;
351
352 my $info = $method_by_name->{$self}->{$name};
353 die "no such method '${self}::$name'\n" if !$info;
354
355 return $info;
356}
357
c1089794 358sub map_path_to_methods {
39313ced 359 my ($class, $stack, $uri_param, $pathmatchref) = @_;
e143e9d8 360
c1089794 361 my $path_lookup = $method_path_lookup->{$class};
e143e9d8 362
39313ced
DM
363 # Note: $pathmatchref can be used to obtain path including
364 # uri patterns like '/cluster/firewall/groups/{group}'.
365 # Used by pvesh to display help
366 if (defined($pathmatchref)) {
367 $$pathmatchref = '' if !$$pathmatchref;
368 }
369
844a246d 370 while (defined(my $comp = shift @$stack)) {
c1089794
DM
371 return undef if !$path_lookup; # not registerd?
372 if ($path_lookup->{regex}) {
373 my $name = $path_lookup->{regex}->{match_name};
374 my $regex = $path_lookup->{regex}->{match_re};
e143e9d8 375
c1089794
DM
376 return undef if $comp !~ m/^($regex)$/;
377 $uri_param->{$name} = $1;
378 $path_lookup = $path_lookup->{regex};
39313ced 379 $$pathmatchref .= '/{' . $name . '}' if defined($pathmatchref);
c1089794
DM
380 } elsif ($path_lookup->{folders}) {
381 $path_lookup = $path_lookup->{folders}->{$comp};
39313ced 382 $$pathmatchref .= '/' . $comp if defined($pathmatchref);
e143e9d8 383 } else {
c1089794 384 die "internal error";
e143e9d8 385 }
9bbc4e17 386
c1089794 387 return undef if !$path_lookup;
e143e9d8 388
c1089794
DM
389 if (my $info = $path_lookup->{SUBCLASS}) {
390 $class = $info->{subclass};
e143e9d8 391
c1089794 392 my $fd = $info->{fragmentDelimiter};
e143e9d8 393
c1089794
DM
394 if (defined($fd)) {
395 # we only support the empty string '' (match whole URI)
9bbc4e17 396 die "unsupported fragmentDelimiter '$fd'"
c1089794 397 if $fd ne '';
e143e9d8 398
c1089794
DM
399 $stack = [ join ('/', @$stack) ] if scalar(@$stack) > 1;
400 }
401 $path_lookup = $method_path_lookup->{$class};
e143e9d8 402 }
c1089794 403 }
e143e9d8 404
c1089794 405 return undef if !$path_lookup;
e143e9d8 406
c1089794
DM
407 return ($class, $path_lookup);
408}
e143e9d8 409
c1089794 410sub find_handler {
39313ced 411 my ($class, $method, $path, $uri_param, $pathmatchref) = @_;
e143e9d8 412
c1089794 413 my $stack = [ grep { length($_) > 0 } split('\/+' , $path)]; # skip empty fragments
e143e9d8 414
c1089794
DM
415 my ($handler_class, $path_info);
416 eval {
39313ced 417 ($handler_class, $path_info) = $class->map_path_to_methods($stack, $uri_param, $pathmatchref);
c1089794
DM
418 };
419 my $err = $@;
420 syslog('err', $err) if $err;
e143e9d8 421
c1089794 422 return undef if !($handler_class && $path_info);
e143e9d8 423
c1089794 424 my $method_info = $path_info->{$method};
e143e9d8 425
c1089794 426 return undef if !$method_info;
e143e9d8 427
c1089794 428 return ($handler_class, $method_info);
e143e9d8
DM
429}
430
07f136d6
DC
431my sub untaint_recursive : prototype($) {
432 use feature 'current_sub';
433
434 my ($param) = @_;
435
436 my $ref = ref($param);
437 if ($ref eq 'HASH') {
438 $param->{$_} = __SUB__->($param->{$_}) for keys $param->%*;
439 } elsif ($ref eq 'ARRAY') {
440 for (my $i = 0; $i < scalar($param->@*); $i++) {
441 $param->[$i] = __SUB__->($param->[$i]);
442 }
443 } else {
444 if (defined($param)) {
445 my ($newval) = $param =~ /^(.*)$/s;
446 $param = $newval;
447 }
448 }
449
450 return $param;
451};
452
453# convert arrays to strings where we expect a '-list' format and convert scalar
454# values to arrays when we expect an array (because of www-form-urlencoded)
455#
456# only on the top level, since www-form-urlencoded cannot be nested anyway
457#
458# FIXME: change gui/api calls to not rely on this during 8.x, mark the
459# behaviour deprecated with 9.x, and remove it with 10.x
460my $normalize_legacy_param_formats = sub {
461 my ($param, $schema) = @_;
462
463 return $param if !$schema->{properties};
464 return $param if (ref($param) // '') ne 'HASH';
465
466 for my $key (keys $schema->{properties}->%*) {
467 if (my $value = $param->{$key}) {
468 my $type = $schema->{properties}->{$key}->{type} // '';
469 my $format = $schema->{properties}->{$key}->{format} // '';
470 my $ref = ref($value);
471 if ($ref && $ref eq 'ARRAY' && $type eq 'string' && $format =~ m/-list$/) {
472 $param->{$key} = join(',', $value->@*);
473 } elsif (!$ref && $type eq 'array') {
474 $param->{$key} = [$value];
475 }
476 }
477 }
478
479 return $param;
480};
481
e143e9d8 482sub handle {
1b44e6fe 483 my ($self, $info, $param, $result_verification) = @_;
e143e9d8
DM
484
485 my $func = $info->{code};
486
487 if (!($info->{name} && $func)) {
4a6f8872 488 raise("Method lookup failed ('$info->{name}')\n", code => HTTP_INTERNAL_SERVER_ERROR);
e143e9d8
DM
489 }
490
491 if (my $schema = $info->{parameters}) {
492 # warn "validate ". Dumper($param}) . "\n" . Dumper($schema);
07f136d6 493 $param = $normalize_legacy_param_formats->($param, $schema);
e143e9d8
DM
494 PVE::JSONSchema::validate($param, $schema);
495 # untaint data (already validated)
07f136d6 496 $param = untaint_recursive($param);
e143e9d8
DM
497 }
498
1b44e6fe 499 my $result = $func->($param); # the actual API code execution call
e143e9d8 500
1b44e6fe
TL
501 if ($result_verification && (my $schema = $info->{returns})) {
502 # return validation is rather lose-lose, as it can require quite a bit of time and lead to
503 # false-positive errors, any HTTP API handler should avoid enabling it by default.
9030a7d7 504 PVE::JSONSchema::validate($result, $schema, "Result verification failed\n");
e143e9d8 505 }
e143e9d8
DM
506 return $result;
507}
508
8fa050cd 509# format option, display type and description
921f91d1
DM
510# $name: option name
511# $display_name: for example "-$name" of "<$name>", pass undef to use "$name:"
8fa050cd 512# $phash: json schema property hash
b0e5e9fa 513# $format: 'asciidoc', 'short', 'long' or 'full'
abc1afd8 514# $style: 'config', 'config-sub', 'arg' or 'fixed'
dfc6cff8 515# $mapdef: parameter mapping ({ desc => XXX, func => sub {...} })
8fa050cd 516my $get_property_description = sub {
4842b651 517 my ($name, $style, $phash, $format, $mapdef) = @_;
8fa050cd
DM
518
519 my $res = '';
520
521 $format = 'asciidoc' if !defined($format);
522
523 my $descr = $phash->{description} || "no description available";
524
32f8e0c7
DM
525 if ($phash->{verbose_description} &&
526 ($style eq 'config' || $style eq 'config-sub')) {
527 $descr = $phash->{verbose_description};
528 }
529
8fa050cd
DM
530 chomp $descr;
531
77096a6f 532 my $type_text = PVE::JSONSchema::schema_get_type_text($phash, $style);
8fa050cd 533
dfc6cff8
DM
534 if ($mapdef && $phash->{type} eq 'string') {
535 $type_text = $mapdef->{desc};
4845032a
FG
536 }
537
8fa050cd
DM
538 if ($format eq 'asciidoc') {
539
df79b428 540 if ($style eq 'config') {
921f91d1 541 $res .= "`$name`: ";
32f8e0c7
DM
542 } elsif ($style eq 'config-sub') {
543 $res .= "`$name`=";
df79b428 544 } elsif ($style eq 'arg') {
a4f181a3 545 $res .= "`--$name` ";
df79b428 546 } elsif ($style eq 'fixed') {
abc1afd8 547 $res .= "`<$name>`: ";
df79b428
DM
548 } else {
549 die "unknown style '$style'";
8fa050cd
DM
550 }
551
77096a6f 552 $res .= "`$type_text` " if $type_text;
8fa050cd
DM
553
554 if (defined(my $dv = $phash->{default})) {
25d9bda9 555 $res .= "('default =' `$dv`)";
8fa050cd 556 }
a736ac99 557
32f8e0c7
DM
558 if ($style eq 'config-sub') {
559 $res .= ";;\n\n";
560 } else {
561 $res .= "::\n\n";
562 }
563
564 my $wdescr = $descr;
a736ac99
DM
565 chomp $wdescr;
566 $wdescr =~ s/^$/+/mg;
567
6c19a10d
FE
568 $wdescr =~ s/{/\\{/g;
569 $wdescr =~ s/}/\\}/g;
570
a736ac99 571 $res .= $wdescr . "\n";
8fa050cd
DM
572
573 if (my $req = $phash->{requires}) {
574 my $tmp .= ref($req) ? join(', ', @$req) : $req;
575 $res .= "+\nNOTE: Requires option(s): `$tmp`\n";
576 }
577 $res .= "\n";
578
b0e5e9fa 579 } elsif ($format eq 'short' || $format eq 'long' || $format eq 'full') {
8fa050cd
DM
580
581 my $defaulttxt = '';
582 if (defined(my $dv = $phash->{default})) {
583 $defaulttxt = " (default=$dv)";
584 }
585
df79b428
DM
586 my $display_name;
587 if ($style eq 'config') {
588 $display_name = "$name:";
589 } elsif ($style eq 'arg') {
590 $display_name = "-$name";
591 } elsif ($style eq 'fixed') {
592 $display_name = "<$name>";
593 } else {
594 die "unknown style '$style'";
595 }
8fa050cd 596
cc6792c7 597 my $tmp = sprintf " %-10s %s%s\n", $display_name, "$type_text", "$defaulttxt";
8fa050cd
DM
598 my $indend = " ";
599
600 $res .= Text::Wrap::wrap('', $indend, ($tmp));
8fa050cd
DM
601 $res .= Text::Wrap::wrap($indend, $indend, ($descr)) . "\n\n";
602
603 if (my $req = $phash->{requires}) {
604 my $tmp = "Requires option(s): ";
605 $tmp .= ref($req) ? join(', ', @$req) : $req;
606 $res .= Text::Wrap::wrap($indend, $indend, ($tmp)). "\n\n";
607 }
608
609 } else {
610 die "unknown format '$format'";
611 }
612
613 return $res;
614};
615
dfc6cff8
DM
616# translate parameter mapping definition
617# $mapping_array is a array which can contain:
618# strings ... in that case we assume it is a parameter name, and
619# we want to load that parameter from a file
620# [ param_name, func, desc] ... allows you to specify a arbitrary
621# mapping func for any param
622#
623# Returns: a hash indexed by parameter_name,
624# i.e. { param_name => { func => .., desc => ... } }
625my $compute_param_mapping_hash = sub {
626 my ($mapping_array) = @_;
627
628 my $res = {};
629
630 return $res if !defined($mapping_array);
631
632 foreach my $item (@$mapping_array) {
c7171ff2 633 my ($name, $func, $desc, $interactive);
dfc6cff8 634 if (ref($item) eq 'ARRAY') {
c7171ff2 635 ($name, $func, $desc, $interactive) = @$item;
a1c7edda
DC
636 } elsif (ref($item) eq 'HASH') {
637 # just use the hash
638 $res->{$item->{name}} = $item;
639 next;
dfc6cff8
DM
640 } else {
641 $name = $item;
642 $func = sub { return PVE::Tools::file_get_contents($_[0]) };
643 }
644 $desc //= '<filepath>';
c7171ff2 645 $res->{$name} = { desc => $desc, func => $func, interactive => $interactive };
dfc6cff8
DM
646 }
647
648 return $res;
649};
650
e143e9d8
DM
651# generate usage information for command line tools
652#
c03b7509 653# $info ... method info
e143e9d8 654# $prefix ... usually something like "$exename $cmd" ('pvesm add')
9bbc4e17 655# $arg_param ... list of parameters we want to get as ordered arguments
1d21344c 656# on the command line (or single parameter name for lists)
e143e9d8
DM
657# $fixed_param ... do not generate and info about those parameters
658# $format:
b0e5e9fa
DM
659# 'long' ... default (text, list all options)
660# 'short' ... command line only (text, one line)
661# 'full' ... text, include description
fe3f1fde 662# 'asciidoc' ... generate asciidoc for man pages (like 'full')
9da27228
DM
663# $param_cb ... mapping for string parameters to file path parameters
664# $formatter_properties ... additional property definitions (passed to output formatter)
c03b7509 665sub getopt_usage {
9da27228 666 my ($info, $prefix, $arg_param, $fixed_param, $format, $param_cb, $formatter_properties) = @_;
e143e9d8
DM
667
668 $format = 'long' if !$format;
669
e143e9d8 670 my $schema = $info->{parameters};
c03b7509 671 my $name = $info->{name};
772038d4
TL
672 my $prop = {};
673 if ($schema->{properties}) {
674 $prop = { %{$schema->{properties}} }; # copy
675 }
62c66c47
DM
676
677 my $has_output_format_option = $formatter_properties->{'output-format'} ? 1 : 0;
678
679 if ($formatter_properties) {
680 foreach my $key (keys %$formatter_properties) {
681 if (!$standard_output_options->{$key}) {
682 $prop->{$key} = $formatter_properties->{$key};
683 }
5c59d8f9
DM
684 }
685 }
686
687 # also remove $standard_output_options from $prop (pvesh, pveclient)
688 if ($prop->{'output-format'}) {
689 $has_output_format_option = 1;
690 foreach my $key (keys %$prop) {
691 if ($standard_output_options->{$key}) {
692 delete $prop->{$key};
693 }
62c66c47
DM
694 }
695 }
e143e9d8
DM
696
697 my $out = '';
698
699 my $arg_hash = {};
700
701 my $args = '';
1d21344c
DM
702
703 $arg_param = [ $arg_param ] if $arg_param && !ref($arg_param);
704
e143e9d8
DM
705 foreach my $p (@$arg_param) {
706 next if !$prop->{$p}; # just to be sure
1d21344c 707 my $pd = $prop->{$p};
e143e9d8
DM
708
709 $arg_hash->{$p} = 1;
710 $args .= " " if $args;
1d21344c 711 if ($pd->{format} && $pd->{format} =~ m/-list/) {
7f0f0610 712 $args .= "{<$p>}";
1d21344c
DM
713 } else {
714 $args .= $pd->{optional} ? "[<$p>]" : "<$p>";
715 }
e143e9d8
DM
716 }
717
e143e9d8
DM
718 my $argdescr = '';
719 foreach my $k (@$arg_param) {
720 next if defined($fixed_param->{$k}); # just to be sure
721 next if !$prop->{$k}; # just to be sure
4842b651 722 $argdescr .= $get_property_description->($k, 'fixed', $prop->{$k}, $format);
e143e9d8
DM
723 }
724
725 my $idx_param = {}; # -vlan\d+ -scsi\d+
726
727 my $opts = '';
94258346
DC
728
729 my $type_specific_opts = {};
730
e143e9d8
DM
731 foreach my $k (sort keys %$prop) {
732 next if $arg_hash->{$k};
733 next if defined($fixed_param->{$k});
734
77096a6f 735 my $type_text = $prop->{$k}->{type} || 'string';
e143e9d8 736
94258346
DC
737 if ($prop->{$k}->{oneOf}) {
738 $type_text = 'multiple';
739 }
740
968bcf45 741 my $param_map = {};
4842b651 742
968bcf45
TL
743 if (defined($param_cb)) {
744 my $mapping = $param_cb->($name);
745 $param_map = $compute_param_mapping_hash->($mapping);
746 next if $k eq 'password' && $param_map->{$k} && !$prop->{$k}->{optional};
4842b651 747 }
e143e9d8
DM
748
749 my $base = $k;
750 if ($k =~ m/^([a-z]+)(\d+)$/) {
b546f33e 751 my ($name, $idx) = ($1, $2);
e143e9d8 752 next if $idx_param->{$name};
b54ad320 753 if ($idx == 0 && defined($prop->{"${name}1"})) {
f922b8a8
FG
754 $idx_param->{$name} = 1;
755 $base = "${name}[n]";
756 }
e143e9d8
DM
757 }
758
94258346
DC
759 my $is_optional = $prop->{$k}->{optional} // 0;
760
761 if (my $type_property = $prop->{$k}->{'type-property'}) {
762 # save type specific descriptions for later
763 my $type_schema = $prop->{$type_property};
764 if ($prop->{$k}->{oneOf}) {
765 # it's optional if there are less options than types
766 $is_optional = 1 if scalar($type_schema->{enum}->@*) > scalar($prop->{$k}->{oneOf}->@*);
767 for my $alternative ($prop->{$k}->{oneOf}->@*) {
768 # it's optional if at least one variant is optional
769 $is_optional = 1 if $alternative->{optional};
770 for my $type ($alternative->{'instance-types'}->@*) {
771 my $key = "${type_property}=${type}";
772 $type_specific_opts->{$key} //= "";
773 $type_specific_opts->{$key}
774 .= $get_property_description->($base, 'arg', $alternative, $format, $param_map->{$k});
775 }
776 }
777 } elsif (my $types = $prop->{$k}->{'instance-types'}) {
778 # it's optional if not all types has that option
779 $is_optional = 1 if scalar($type_schema->{enum}->@*) > scalar($types->@*);
780 for my $type ($types->@*) {
781 my $key = "${type_property}=${type}";
782 $type_specific_opts->{$key} //= "";
783 $type_specific_opts->{$key}
784 .= $get_property_description->($base, 'arg', $prop->{$k}, $format, $param_map->{$k});
785 }
786 }
787 } elsif ($prop->{$k}->{oneOf}) {
788 my $res = [];
789 for my $alternative ($prop->{$k}->{oneOf}->@*) {
790 # it's optional if at least one variant is optional
791 $is_optional = 1 if $alternative->{optional};
792 push $res->@*, $get_property_description->($base, 'arg', $alternative, $format, $param_map->{$k});
793 }
794 if ($format eq 'asciidoc') {
795 $opts .= join("\n\nor\n\n", $res->@*);
796 } else {
797 $opts .= join(" or\n\n", $res->@*);
798 }
799 } else {
800 $opts .= $get_property_description->($base, 'arg', $prop->{$k}, $format, $param_map->{$k});
801 }
dfc6cff8 802
94258346 803 if (!$is_optional) {
e143e9d8 804 $args .= " " if $args;
a4f181a3 805 $args .= "--$base <$type_text>"
e143e9d8 806 }
9bbc4e17 807 }
e143e9d8 808
fe3f1fde
DM
809 if ($format eq 'asciidoc') {
810 $out .= "*${prefix}*";
811 $out .= " `$args`" if $args;
62c66c47
DM
812 $out .= " `[OPTIONS]`" if $opts;
813 $out .= " `[FORMAT_OPTIONS]`" if $has_output_format_option;
814 $out .= "\n";
fe3f1fde
DM
815 } else {
816 $out .= "USAGE: " if $format ne 'short';
817 $out .= "$prefix $args";
62c66c47
DM
818 $out .= " [OPTIONS]" if $opts;
819 $out .= " [FORMAT_OPTIONS]" if $has_output_format_option;
820 $out .= "\n";
fe3f1fde 821 }
e143e9d8
DM
822
823 return $out if $format eq 'short';
824
fe3f1fde
DM
825 if ($info->{description}) {
826 if ($format eq 'asciidoc') {
827 my $desc = Text::Wrap::wrap('', '', ($info->{description}));
828 $out .= "\n$desc\n\n";
829 } elsif ($format eq 'full') {
830 my $desc = Text::Wrap::wrap(' ', ' ', ($info->{description}));
94258346 831 $out .= "\n$desc\n\n";
fe3f1fde 832 }
e143e9d8
DM
833 }
834
835 $out .= $argdescr if $argdescr;
836
837 $out .= $opts if $opts;
838
94258346
DC
839 if (scalar(keys $type_specific_opts->%*)) {
840 if ($format eq 'asciidoc') {
841 $out .= "\n\n\n`Conditional options:`\n\n";
842 } else {
843 $out .= " Conditional options:\n\n";
844 }
845 }
846
847 for my $type_opts (sort keys $type_specific_opts->%*) {
848 if ($format eq 'asciidoc') {
849 $out .= "`[$type_opts]` ;;\n\n";
850 } else {
851 $out .= " [$type_opts]\n\n";
852 }
853 $out .= $type_specific_opts->{$type_opts};
854 }
855
e143e9d8
DM
856 return $out;
857}
858
c03b7509 859sub usage_str {
9da27228 860 my ($self, $name, $prefix, $arg_param, $fixed_param, $format, $param_cb, $formatter_properties) = @_;
c03b7509
DM
861
862 my $info = $self->map_method_by_name($name);
863
9da27228 864 return getopt_usage($info, $prefix, $arg_param, $fixed_param, $format, $param_cb, $formatter_properties);
c03b7509
DM
865}
866
8fa050cd
DM
867# generate docs from JSON schema properties
868sub dump_properties {
df79b428 869 my ($prop, $format, $style, $filterFn) = @_;
8fa050cd
DM
870
871 my $raw = '';
872
df79b428 873 $style //= 'config';
9bbc4e17 874
8fa050cd
DM
875 my $idx_param = {}; # -vlan\d+ -scsi\d+
876
877 foreach my $k (sort keys %$prop) {
878 my $phash = $prop->{$k};
879
880 next if defined($filterFn) && &$filterFn($k, $phash);
32f8e0c7 881 next if $phash->{alias};
8fa050cd
DM
882
883 my $base = $k;
884 if ($k =~ m/^([a-z]+)(\d+)$/) {
b546f33e 885 my ($name, $idx) = ($1, $2);
8fa050cd 886 next if $idx_param->{$name};
b54ad320 887 if ($idx == 0 && defined($prop->{"${name}1"})) {
f922b8a8
FG
888 $idx_param->{$name} = 1;
889 $base = "${name}[n]";
890 }
8fa050cd
DM
891 }
892
94258346
DC
893 if ($phash->{oneOf}) {
894 for my $alternative ($phash->{oneOf}->@*) {
895 $raw .= $get_property_description->($base, $style, $alternative, $format);
896 }
897 } else {
898 $raw .= $get_property_description->($base, $style, $phash, $format);
899 }
900
32f8e0c7
DM
901
902 next if $style ne 'config';
903
904 my $prop_fmt = $phash->{format};
905 next if !$prop_fmt;
906
907 if (ref($prop_fmt) ne 'HASH') {
908 $prop_fmt = PVE::JSONSchema::get_format($prop_fmt);
909 }
910
911 next if !(ref($prop_fmt) && (ref($prop_fmt) eq 'HASH'));
912
913 $raw .= dump_properties($prop_fmt, $format, 'config-sub')
9bbc4e17 914
8fa050cd 915 }
b0e5e9fa 916
8fa050cd
DM
917 return $raw;
918}
919
408976c6 920my $replace_file_names_with_contents = sub {
968bcf45 921 my ($param, $param_map) = @_;
408976c6 922
968bcf45 923 while (my ($k, $d) = each %$param_map) {
c7171ff2 924 next if $d->{interactive}; # handled by the JSONSchema's get_options code
dfc6cff8
DM
925 $param->{$k} = $d->{func}->($param->{$k})
926 if defined($param->{$k});
408976c6
FG
927 }
928
929 return $param;
930};
931
4cbcd138
DM
932sub add_standard_output_properties {
933 my ($propdef, $list) = @_;
0adee985 934
4cbcd138 935 $propdef //= {};
0adee985 936
4cbcd138
DM
937 $list //= [ keys %$standard_output_options ];
938
939 my $res = { %$propdef }; # copy
940
941 foreach my $opt (@$list) {
942 die "no such standard output option '$opt'\n" if !defined($standard_output_options->{$opt});
943 die "detected overwriten standard CLI parameter '$opt'\n" if defined($res->{$opt});
944 $res->{$opt} = $standard_output_options->{$opt};
945 }
946
947 return $res;
948}
949
950sub extract_standard_output_properties {
951 my ($data) = @_;
952
953 my $options = {};
954 foreach my $opt (keys %$standard_output_options) {
955 $options->{$opt} = delete $data->{$opt} if defined($data->{$opt});
956 }
957
958 return $options;
959}
0adee985 960
e143e9d8 961sub cli_handler {
9da27228 962 my ($self, $prefix, $name, $args, $arg_param, $fixed_param, $param_cb, $formatter_properties) = @_;
e143e9d8
DM
963
964 my $info = $self->map_method_by_name($name);
e143e9d8 965 my $res;
9da27228
DM
966 my $fmt_param = {};
967
e143e9d8 968 eval {
968bcf45
TL
969 my $param_map = {};
970 $param_map = $compute_param_mapping_hash->($param_cb->($name)) if $param_cb;
9da27228
DM
971 my $schema = { %{$info->{parameters}} }; # copy
972 $schema->{properties} = { %{$schema->{properties}}, %$formatter_properties } if $formatter_properties;
973 my $param = PVE::JSONSchema::get_options($schema, $args, $arg_param, $fixed_param, $param_map);
974
975 if ($formatter_properties) {
976 foreach my $opt (keys %$formatter_properties) {
977 $fmt_param->{$opt} = delete $param->{$opt} if defined($param->{$opt});
978 }
979 }
dfc6cff8 980
968bcf45
TL
981 if (defined($param_map)) {
982 $replace_file_names_with_contents->($param, $param_map);
dfc6cff8
DM
983 }
984
1b44e6fe 985 $res = $self->handle($info, $param, 1);
e143e9d8
DM
986 };
987 if (my $err = $@) {
988 my $ec = ref($err);
989
990 die $err if !$ec || $ec ne "PVE::Exception" || !$err->is_param_exc();
9bbc4e17 991
9da27228 992 $err->{usage} = $self->usage_str($name, $prefix, $arg_param, $fixed_param, 'short', $param_cb, $formatter_properties);
e143e9d8
DM
993
994 die $err;
995 }
996
9da27228 997 return wantarray ? ($res, $fmt_param) : $res;
e143e9d8
DM
998}
999
1000# utility methods
1001# note: this modifies the original hash by adding the id property
1002sub hash_to_array {
1003 my ($hash, $idprop) = @_;
1004
1005 my $res = [];
1006 return $res if !$hash;
1007
1008 foreach my $k (keys %$hash) {
1009 $hash->{$k}->{$idprop} = $k;
1010 push @$res, $hash->{$k};
1011 }
1012
1013 return $res;
1014}
1015
10161;