]> git.proxmox.com Git - pve-common.git/blame - src/PVE/RESTHandler.pm
runtest.pl: use lib '.' - because newer perl does not have that by default
[pve-common.git] / src / PVE / RESTHandler.pm
CommitLineData
e143e9d8
DM
1package PVE::RESTHandler;
2
3use strict;
4no strict 'refs'; # our autoload requires this
e143e9d8
DM
5use warnings;
6use PVE::SafeSyslog;
7use PVE::Exception qw(raise raise_param_exc);
8use PVE::JSONSchema;
e143e9d8
DM
9use HTTP::Status qw(:constants :is status_message);
10use Text::Wrap;
4be9b5f7 11use Clone qw(clone);
e143e9d8
DM
12
13my $method_registry = {};
14my $method_by_name = {};
c1089794 15my $method_path_lookup = {};
e143e9d8
DM
16
17our $AUTOLOAD; # it's a package global
18
19sub 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 36 my ($name, $idx) = ($1, $2);
b54ad320 37 if ($idx == 0 && defined($d->{"${name}1"})) {
b546f33e
DM
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 }
534d4270
DM
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;
e143e9d8
DM
53 }
54 }
55
56 return $res;
57}
58
59sub 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
4be9b5f7 117 $data->{$k} = ref($d) ? clone($d) : $d;
e143e9d8
DM
118 }
119 }
120 $res->{info}->{$info->{method}} = $data;
121 };
122 }
123 }
124};
125
126sub 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
141sub 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
152sub 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
163sub register_method {
164 my ($self, $info) = @_;
165
166 my $match_re = [];
167 my $match_name = [];
168
c1089794
DM
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
e143e9d8 186 foreach my $comp (split(/\/+/, $info->{path})) {
c1089794
DM
187 die "$errprefix path compoment has zero length\n" if $comp eq '';
188 my ($name, $regex);
e143e9d8 189 if ($comp =~ m/^\{(\w+)(:(.*))?\}$/) {
c1089794
DM
190 $name = $1;
191 $regex = $3 ? $3 : '\S+';
192 push @$match_re, $regex;
193 push @$match_name, $name;
e143e9d8 194 } else {
c1089794
DM
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};
e143e9d8
DM
224 }
225 }
226
c1089794
DM
227 die "$errprefix duplicate method definition\n"
228 if defined($path_lookup->{$method});
229
1dad1ca5
DM
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 }
c1089794
DM
235 $path_lookup->{$method} = $info;
236
e143e9d8
DM
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}) {
c1089794 243 die "$errprefix method name already defined\n"
e143e9d8
DM
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
f1fb34a0
DM
252sub DESTROY {}; # avoid problems with autoload
253
e143e9d8
DM
254sub AUTOLOAD {
255 my ($this) = @_;
256
257 # also see "man perldiag"
258
259 my $sub = $AUTOLOAD;
260 (my $method = $sub) =~ s/.*:://;
261
e143e9d8
DM
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
271sub method_attributes {
272 my ($self) = @_;
273
274 return $method_registry->{$self};
275}
276
277sub 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
c1089794 286sub map_path_to_methods {
39313ced 287 my ($class, $stack, $uri_param, $pathmatchref) = @_;
e143e9d8 288
c1089794 289 my $path_lookup = $method_path_lookup->{$class};
e143e9d8 290
39313ced
DM
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
844a246d 298 while (defined(my $comp = shift @$stack)) {
c1089794
DM
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};
e143e9d8 303
c1089794
DM
304 return undef if $comp !~ m/^($regex)$/;
305 $uri_param->{$name} = $1;
306 $path_lookup = $path_lookup->{regex};
39313ced 307 $$pathmatchref .= '/{' . $name . '}' if defined($pathmatchref);
c1089794
DM
308 } elsif ($path_lookup->{folders}) {
309 $path_lookup = $path_lookup->{folders}->{$comp};
39313ced 310 $$pathmatchref .= '/' . $comp if defined($pathmatchref);
e143e9d8 311 } else {
c1089794 312 die "internal error";
e143e9d8 313 }
c1089794
DM
314
315 return undef if !$path_lookup;
e143e9d8 316
c1089794
DM
317 if (my $info = $path_lookup->{SUBCLASS}) {
318 $class = $info->{subclass};
e143e9d8 319
c1089794 320 my $fd = $info->{fragmentDelimiter};
e143e9d8 321
c1089794
DM
322 if (defined($fd)) {
323 # we only support the empty string '' (match whole URI)
324 die "unsupported fragmentDelimiter '$fd'"
325 if $fd ne '';
e143e9d8 326
c1089794
DM
327 $stack = [ join ('/', @$stack) ] if scalar(@$stack) > 1;
328 }
329 $path_lookup = $method_path_lookup->{$class};
e143e9d8 330 }
c1089794 331 }
e143e9d8 332
c1089794 333 return undef if !$path_lookup;
e143e9d8 334
c1089794
DM
335 return ($class, $path_lookup);
336}
e143e9d8 337
c1089794 338sub find_handler {
39313ced 339 my ($class, $method, $path, $uri_param, $pathmatchref) = @_;
e143e9d8 340
c1089794 341 my $stack = [ grep { length($_) > 0 } split('\/+' , $path)]; # skip empty fragments
e143e9d8 342
c1089794
DM
343 my ($handler_class, $path_info);
344 eval {
39313ced 345 ($handler_class, $path_info) = $class->map_path_to_methods($stack, $uri_param, $pathmatchref);
c1089794
DM
346 };
347 my $err = $@;
348 syslog('err', $err) if $err;
e143e9d8 349
c1089794 350 return undef if !($handler_class && $path_info);
e143e9d8 351
c1089794 352 my $method_info = $path_info->{$method};
e143e9d8 353
c1089794 354 return undef if !$method_info;
e143e9d8 355
c1089794 356 return ($handler_class, $method_info);
e143e9d8
DM
357}
358
359sub 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)
5851be88 373 my $extra = delete $param->{'extra-args'};
e143e9d8
DM
374 while (my ($key, $val) = each %$param) {
375 ($param->{$key}) = $val =~ /^(.*)$/s;
376 }
5851be88 377 $param->{'extra-args'} = [map { /^(.*)$/ } @$extra] if $extra;
e143e9d8
DM
378 }
379
380 my $result = &$func($param);
381
382 # todo: this is only to be safe - disable?
383 if (my $schema = $info->{returns}) {
9030a7d7 384 PVE::JSONSchema::validate($result, $schema, "Result verification failed\n");
e143e9d8
DM
385 }
386
387 return $result;
388}
389
8fa050cd 390# format option, display type and description
921f91d1
DM
391# $name: option name
392# $display_name: for example "-$name" of "<$name>", pass undef to use "$name:"
8fa050cd 393# $phash: json schema property hash
b0e5e9fa 394# $format: 'asciidoc', 'short', 'long' or 'full'
abc1afd8 395# $style: 'config', 'config-sub', 'arg' or 'fixed'
8fa050cd 396my $get_property_description = sub {
4845032a 397 my ($name, $style, $phash, $format, $hidepw, $fileparams) = @_;
8fa050cd
DM
398
399 my $res = '';
400
401 $format = 'asciidoc' if !defined($format);
402
403 my $descr = $phash->{description} || "no description available";
404
32f8e0c7
DM
405 if ($phash->{verbose_description} &&
406 ($style eq 'config' || $style eq 'config-sub')) {
407 $descr = $phash->{verbose_description};
408 }
409
8fa050cd
DM
410 chomp $descr;
411
abc1afd8 412 my $type = PVE::JSONSchema::schema_get_type_text($phash, $style);
8fa050cd 413
921f91d1 414 if ($hidepw && $name eq 'password') {
8fa050cd
DM
415 $type = '';
416 }
417
4845032a
FG
418 if ($fileparams && $type eq 'string') {
419 foreach my $elem (@$fileparams) {
420 if ($name eq $elem) {
25d9bda9 421 $type = '<filepath>';
4845032a
FG
422 last;
423 }
424 }
425 }
426
8fa050cd
DM
427 if ($format eq 'asciidoc') {
428
df79b428 429 if ($style eq 'config') {
921f91d1 430 $res .= "`$name`: ";
32f8e0c7
DM
431 } elsif ($style eq 'config-sub') {
432 $res .= "`$name`=";
df79b428
DM
433 } elsif ($style eq 'arg') {
434 $res .= "`-$name` ";
435 } elsif ($style eq 'fixed') {
abc1afd8 436 $res .= "`<$name>`: ";
df79b428
DM
437 } else {
438 die "unknown style '$style'";
8fa050cd
DM
439 }
440
441 $res .= "`$type` " if $type;
442
443 if (defined(my $dv = $phash->{default})) {
25d9bda9 444 $res .= "('default =' `$dv`)";
8fa050cd 445 }
a736ac99 446
32f8e0c7
DM
447 if ($style eq 'config-sub') {
448 $res .= ";;\n\n";
449 } else {
450 $res .= "::\n\n";
451 }
452
453 my $wdescr = $descr;
a736ac99
DM
454 chomp $wdescr;
455 $wdescr =~ s/^$/+/mg;
456
457 $res .= $wdescr . "\n";
8fa050cd
DM
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
b0e5e9fa 465 } elsif ($format eq 'short' || $format eq 'long' || $format eq 'full') {
8fa050cd
DM
466
467 my $defaulttxt = '';
468 if (defined(my $dv = $phash->{default})) {
469 $defaulttxt = " (default=$dv)";
470 }
471
df79b428
DM
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 }
8fa050cd
DM
482
483 my $tmp = sprintf " %-10s %s$defaulttxt\n", $display_name, "$type";
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
e143e9d8
DM
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')
1d21344c
DM
507# $arg_param ... list of parameters we want to get as ordered arguments
508# on the command line (or single parameter name for lists)
e143e9d8
DM
509# $fixed_param ... do not generate and info about those parameters
510# $format:
b0e5e9fa
DM
511# 'long' ... default (text, list all options)
512# 'short' ... command line only (text, one line)
513# 'full' ... text, include description
fe3f1fde 514# 'asciidoc' ... generate asciidoc for man pages (like 'full')
e143e9d8 515# $hidepw ... hide password option (use this if you provide a read passwork callback)
4845032a 516# $stringfilemap ... mapping for string parameters to file path parameters
e143e9d8 517sub usage_str {
4845032a 518 my ($self, $name, $prefix, $arg_param, $fixed_param, $format, $hidepw, $stringfilemap) = @_;
e143e9d8
DM
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 = '';
1d21344c
DM
531
532 $arg_param = [ $arg_param ] if $arg_param && !ref($arg_param);
533
e143e9d8
DM
534 foreach my $p (@$arg_param) {
535 next if !$prop->{$p}; # just to be sure
1d21344c 536 my $pd = $prop->{$p};
e143e9d8
DM
537
538 $arg_hash->{$p} = 1;
539 $args .= " " if $args;
1d21344c 540 if ($pd->{format} && $pd->{format} =~ m/-list/) {
7f0f0610 541 $args .= "{<$p>}";
1d21344c
DM
542 } else {
543 $args .= $pd->{optional} ? "[<$p>]" : "<$p>";
544 }
e143e9d8
DM
545 }
546
e143e9d8
DM
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
305068a7 551 $argdescr .= &$get_property_description($k, 'fixed', $prop->{$k}, $format, 0);
e143e9d8
DM
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 = $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+)$/) {
b546f33e 567 my ($name, $idx) = ($1, $2);
e143e9d8 568 next if $idx_param->{$name};
b54ad320 569 if ($idx == 0 && defined($prop->{"${name}1"})) {
f922b8a8
FG
570 $idx_param->{$name} = 1;
571 $base = "${name}[n]";
572 }
e143e9d8
DM
573 }
574
6577ea45 575 my $mapping = defined($stringfilemap) ? &$stringfilemap($name) : undef;
305068a7 576 $opts .= &$get_property_description($base, 'arg', $prop->{$k}, $format,
6577ea45 577 $hidepw, $mapping);
e143e9d8
DM
578
579 if (!$prop->{$k}->{optional}) {
580 $args .= " " if $args;
581 $args .= "-$base <$type>"
582 }
583 }
584
fe3f1fde
DM
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 }
e143e9d8
DM
594
595 return $out if $format eq 'short';
596
fe3f1fde
DM
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 }
e143e9d8
DM
605 }
606
607 $out .= $argdescr if $argdescr;
608
609 $out .= $opts if $opts;
610
611 return $out;
612}
613
8fa050cd
DM
614# generate docs from JSON schema properties
615sub dump_properties {
df79b428 616 my ($prop, $format, $style, $filterFn) = @_;
8fa050cd
DM
617
618 my $raw = '';
619
df79b428
DM
620 $style //= 'config';
621
8fa050cd
DM
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);
32f8e0c7 628 next if $phash->{alias};
8fa050cd
DM
629
630 my $base = $k;
631 if ($k =~ m/^([a-z]+)(\d+)$/) {
b546f33e 632 my ($name, $idx) = ($1, $2);
8fa050cd 633 next if $idx_param->{$name};
b54ad320 634 if ($idx == 0 && defined($prop->{"${name}1"})) {
f922b8a8
FG
635 $idx_param->{$name} = 1;
636 $base = "${name}[n]";
637 }
8fa050cd
DM
638 }
639
df79b428 640 $raw .= &$get_property_description($base, $style, $phash, $format, 0);
32f8e0c7
DM
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
8fa050cd 655 }
b0e5e9fa 656
8fa050cd
DM
657 return $raw;
658}
659
408976c6
FG
660my $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
e143e9d8 673sub cli_handler {
408976c6 674 my ($self, $prefix, $name, $args, $arg_param, $fixed_param, $pwcallback, $stringfilemap) = @_;
e143e9d8
DM
675
676 my $info = $self->map_method_by_name($name);
677
e143e9d8
DM
678 my $res;
679 eval {
0ce82909 680 my $param = PVE::JSONSchema::get_options($info->{parameters}, $args, $arg_param, $fixed_param, $pwcallback);
408976c6
FG
681 &$replace_file_names_with_contents($param, &$stringfilemap($name))
682 if defined($stringfilemap);
e143e9d8
DM
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
4845032a 690 $err->{usage} = $self->usage_str($name, $prefix, $arg_param, $fixed_param, 'short', $pwcallback, $stringfilemap);
e143e9d8
DM
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
700sub 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
7141;