1 package PVE
::RESTHandler
;
4 no strict
'refs'; # our autoload requires this
7 use PVE
::Exception
qw(raise raise_param_exc);
10 use HTTP
::Status
qw(:constants :is status_message);
14 my $method_registry = {};
15 my $method_by_name = {};
16 my $method_path_lookup = {};
18 our $AUTOLOAD; # it's a package global
20 our $standard_output_options = {
21 'output-format' => PVE
::JSONSchema
::get_standard_option
('pve-output-format'),
23 description
=> "Do not show column headers (for 'text' format).",
29 description
=> "Do not draw borders (for 'text' format).",
35 description
=> "Suppress printing results.",
40 description
=> "Call output rendering functions to produce human readable text.",
47 sub api_clone_schema
{
51 my $ref = ref($schema);
52 die "not a HASH reference" if !($ref && $ref eq 'HASH');
54 foreach my $k (keys %$schema) {
55 my $d = $schema->{$k};
56 if ($k ne 'properties') {
57 $res->{$k} = ref($d) ? clone
($d) : $d;
60 # convert indexed parameters like -net\d+ to -net[n]
61 foreach my $p (keys %$d) {
63 if ($p =~ m/^([a-z]+)(\d+)$/) {
64 my ($name, $idx) = ($1, $2);
65 if ($idx == 0 && defined($d->{"${name}1"})) {
67 } elsif ($idx > 0 && defined($d->{"${name}0"})) {
68 next; # only handle once for -xx0, but only if -xx0 exists
71 my $tmp = ref($pd) ? clone
($pd) : $pd;
72 # NOTE: add typetext property for more complex types, to
73 # make the web api viewer code simpler
74 if (!(defined($tmp->{enum
}) || defined($tmp->{pattern
}))) {
75 my $typetext = PVE
::JSONSchema
::schema_get_type_text
($tmp);
76 if ($tmp->{type
} && ($tmp->{type
} ne $typetext)) {
77 $tmp->{typetext
} = $typetext;
80 $res->{$k}->{$p} = $tmp;
88 my ($tree, $index, $class, $prefix, $raw_dump) = @_;
90 $prefix = '' if !$prefix;
92 my $ma = $method_registry->{$class};
94 foreach my $info (@$ma) {
96 my $path = "$prefix/$info->{path}";
99 if ($info->{subclass
}) {
100 api_dump_full
($tree, $index, $info->{subclass
}, $path, $raw_dump);
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;
115 foreach my $dir (split('/', $path)) {
118 $res = (grep { $_->{text
} eq $dir } @$treedir)[0];
120 $res->{children
} = [] if !$res->{children
};
121 $treedir = $res->{children
};
128 push @$treedir, $res;
129 $treedir = $res->{children
};
135 foreach my $k (keys %$info) {
136 next if $k eq 'code' || $k eq "match_name" || $k eq "match_re" ||
144 if ($k eq 'parameters') {
145 $data->{$k} = api_clone_schema
($d);
147 $data->{$k} = ref($d) ? clone
($d) : $d;
151 $res->{info
}->{$info->{method}} = $data;
157 sub api_dump_cleanup_tree
{
160 foreach my $rec (@$tree) {
161 delete $rec->{children
} if $rec->{children
} && !scalar(@{$rec->{children
}});
162 if ($rec->{children
}) {
164 api_dump_cleanup_tree
($rec->{children
});
172 # api_dump_remove_refs: prepare API tree for use with to_json($tree)
173 sub api_dump_remove_refs
{
176 my $class = ref($tree);
177 return $tree if !$class;
179 if ($class eq 'ARRAY') {
181 foreach my $el (@$tree) {
182 push @$res, api_dump_remove_refs
($el);
185 } elsif ($class eq 'HASH') {
187 foreach my $k (keys %$tree) {
188 if (my $itemclass = ref($tree->{$k})) {
189 if ($itemclass eq 'CODE') {
190 next if $k eq 'completion';
192 $res->{$k} = api_dump_remove_refs
($tree->{$k});
194 $res->{$k} = $tree->{$k};
198 } elsif ($class eq 'Regexp') {
199 return "$tree"; # return string representation
201 die "unknown class '$class'\n";
206 my ($class, $prefix, $raw_dump) = @_;
211 api_dump_full
($tree, $index, $class, $prefix, $raw_dump);
212 api_dump_cleanup_tree
($tree);
216 sub validate_method_schemas
{
218 foreach my $class (keys %$method_registry) {
219 my $ma = $method_registry->{$class};
221 foreach my $info (@$ma) {
222 PVE
::JSONSchema
::validate_method_info
($info);
227 sub register_method
{
228 my ($self, $info) = @_;
236 if ($info->{subclass
}) {
237 $errprefix = "register subclass $info->{subclass} at ${self}/$info->{path} -";
238 $method = 'SUBCLASS';
240 $errprefix = "register method ${self}/$info->{path} -";
241 $info->{method} = 'GET' if !$info->{method};
242 $method = $info->{method};
245 $method_path_lookup->{$self} = {} if !defined($method_path_lookup->{$self});
246 my $path_lookup = $method_path_lookup->{$self};
248 die "$errprefix no path" if !defined($info->{path
});
250 foreach my $comp (split(/\/+/, $info->{path
})) {
251 die "$errprefix path compoment has zero length\n" if $comp eq '';
253 if ($comp =~ m/^\{(\w+)(:(.*))?\}$/) {
255 $regex = $3 ?
$3 : '\S+';
256 push @$match_re, $regex;
257 push @$match_name, $name;
260 push @$match_re, $name;
261 push @$match_name, undef;
265 $path_lookup->{regex
} = {} if !defined($path_lookup->{regex
});
267 my $old_name = $path_lookup->{regex
}->{match_name
};
268 die "$errprefix found changed regex match name\n"
269 if defined($old_name) && ($old_name ne $name);
270 my $old_re = $path_lookup->{regex
}->{match_re
};
271 die "$errprefix found changed regex\n"
272 if defined($old_re) && ($old_re ne $regex);
273 $path_lookup->{regex
}->{match_name
} = $name;
274 $path_lookup->{regex
}->{match_re
} = $regex;
276 die "$errprefix path match error - regex and fixed items\n"
277 if defined($path_lookup->{folders
});
279 $path_lookup = $path_lookup->{regex
};
282 $path_lookup->{folders
}->{$name} = {} if !defined($path_lookup->{folders
}->{$name});
284 die "$errprefix path match error - regex and fixed items\n"
285 if defined($path_lookup->{regex
});
287 $path_lookup = $path_lookup->{folders
}->{$name};
291 die "$errprefix duplicate method definition\n"
292 if defined($path_lookup->{$method});
294 if ($method eq 'SUBCLASS') {
295 foreach my $m (qw(GET PUT POST DELETE)) {
296 die "$errprefix duplicate method definition SUBCLASS and $m\n" if $path_lookup->{$m};
299 $path_lookup->{$method} = $info;
301 $info->{match_re
} = $match_re;
302 $info->{match_name
} = $match_name;
304 $method_by_name->{$self} = {} if !defined($method_by_name->{$self});
307 die "$errprefix method name already defined\n"
308 if defined($method_by_name->{$self}->{$info->{name
}});
310 $method_by_name->{$self}->{$info->{name
}} = $info;
313 push @{$method_registry->{$self}}, $info;
316 sub DESTROY
{}; # avoid problems with autoload
321 # also see "man perldiag"
324 (my $method = $sub) =~ s/.*:://;
326 my $info = $this->map_method_by_name($method);
330 return $self->handle($info, @_);
335 sub method_attributes
{
338 return $method_registry->{$self};
341 sub map_method_by_name
{
342 my ($self, $name) = @_;
344 my $info = $method_by_name->{$self}->{$name};
345 die "no such method '${self}::$name'\n" if !$info;
350 sub map_path_to_methods
{
351 my ($class, $stack, $uri_param, $pathmatchref) = @_;
353 my $path_lookup = $method_path_lookup->{$class};
355 # Note: $pathmatchref can be used to obtain path including
356 # uri patterns like '/cluster/firewall/groups/{group}'.
357 # Used by pvesh to display help
358 if (defined($pathmatchref)) {
359 $$pathmatchref = '' if !$$pathmatchref;
362 while (defined(my $comp = shift @$stack)) {
363 return undef if !$path_lookup; # not registerd?
364 if ($path_lookup->{regex
}) {
365 my $name = $path_lookup->{regex
}->{match_name
};
366 my $regex = $path_lookup->{regex
}->{match_re
};
368 return undef if $comp !~ m/^($regex)$/;
369 $uri_param->{$name} = $1;
370 $path_lookup = $path_lookup->{regex
};
371 $$pathmatchref .= '/{' . $name . '}' if defined($pathmatchref);
372 } elsif ($path_lookup->{folders
}) {
373 $path_lookup = $path_lookup->{folders
}->{$comp};
374 $$pathmatchref .= '/' . $comp if defined($pathmatchref);
376 die "internal error";
379 return undef if !$path_lookup;
381 if (my $info = $path_lookup->{SUBCLASS
}) {
382 $class = $info->{subclass
};
384 my $fd = $info->{fragmentDelimiter
};
387 # we only support the empty string '' (match whole URI)
388 die "unsupported fragmentDelimiter '$fd'"
391 $stack = [ join ('/', @$stack) ] if scalar(@$stack) > 1;
393 $path_lookup = $method_path_lookup->{$class};
397 return undef if !$path_lookup;
399 return ($class, $path_lookup);
403 my ($class, $method, $path, $uri_param, $pathmatchref) = @_;
405 my $stack = [ grep { length($_) > 0 } split('\/+' , $path)]; # skip empty fragments
407 my ($handler_class, $path_info);
409 ($handler_class, $path_info) = $class->map_path_to_methods($stack, $uri_param, $pathmatchref);
412 syslog
('err', $err) if $err;
414 return undef if !($handler_class && $path_info);
416 my $method_info = $path_info->{$method};
418 return undef if !$method_info;
420 return ($handler_class, $method_info);
424 my ($self, $info, $param) = @_;
426 my $func = $info->{code
};
428 if (!($info->{name
} && $func)) {
429 raise
("Method lookup failed ('$info->{name}')\n",
430 code
=> HTTP_INTERNAL_SERVER_ERROR
);
433 if (my $schema = $info->{parameters
}) {
434 # warn "validate ". Dumper($param}) . "\n" . Dumper($schema);
435 PVE
::JSONSchema
::validate
($param, $schema);
436 # untaint data (already validated)
437 my $extra = delete $param->{'extra-args'};
438 while (my ($key, $val) = each %$param) {
439 ($param->{$key}) = $val =~ /^(.*)$/s;
441 $param->{'extra-args'} = [map { /^(.*)$/ } @$extra] if $extra;
444 my $result = &$func($param);
446 # todo: this is only to be safe - disable?
447 if (my $schema = $info->{returns
}) {
448 PVE
::JSONSchema
::validate
($result, $schema, "Result verification failed\n");
454 # format option, display type and description
456 # $display_name: for example "-$name" of "<$name>", pass undef to use "$name:"
457 # $phash: json schema property hash
458 # $format: 'asciidoc', 'short', 'long' or 'full'
459 # $style: 'config', 'config-sub', 'arg' or 'fixed'
460 # $mapdef: parameter mapping ({ desc => XXX, func => sub {...} })
461 my $get_property_description = sub {
462 my ($name, $style, $phash, $format, $mapdef) = @_;
466 $format = 'asciidoc' if !defined($format);
468 my $descr = $phash->{description
} || "no description available";
470 if ($phash->{verbose_description
} &&
471 ($style eq 'config' || $style eq 'config-sub')) {
472 $descr = $phash->{verbose_description
};
477 my $type_text = PVE
::JSONSchema
::schema_get_type_text
($phash, $style);
479 if ($mapdef && $phash->{type
} eq 'string') {
480 $type_text = $mapdef->{desc
};
483 if ($format eq 'asciidoc') {
485 if ($style eq 'config') {
487 } elsif ($style eq 'config-sub') {
489 } elsif ($style eq 'arg') {
490 $res .= "`--$name` ";
491 } elsif ($style eq 'fixed') {
492 $res .= "`<$name>`: ";
494 die "unknown style '$style'";
497 $res .= "`$type_text` " if $type_text;
499 if (defined(my $dv = $phash->{default})) {
500 $res .= "('default =' `$dv`)";
503 if ($style eq 'config-sub') {
511 $wdescr =~ s/^$/+/mg;
513 $res .= $wdescr . "\n";
515 if (my $req = $phash->{requires
}) {
516 my $tmp .= ref($req) ?
join(', ', @$req) : $req;
517 $res .= "+\nNOTE: Requires option(s): `$tmp`\n";
521 } elsif ($format eq 'short' || $format eq 'long' || $format eq 'full') {
524 if (defined(my $dv = $phash->{default})) {
525 $defaulttxt = " (default=$dv)";
529 if ($style eq 'config') {
530 $display_name = "$name:";
531 } elsif ($style eq 'arg') {
532 $display_name = "-$name";
533 } elsif ($style eq 'fixed') {
534 $display_name = "<$name>";
536 die "unknown style '$style'";
539 my $tmp = sprintf " %-10s %s$defaulttxt\n", $display_name, "$type_text";
542 $res .= Text
::Wrap
::wrap
('', $indend, ($tmp));
544 $res .= Text
::Wrap
::wrap
($indend, $indend, ($descr)) . "\n\n";
546 if (my $req = $phash->{requires
}) {
547 my $tmp = "Requires option(s): ";
548 $tmp .= ref($req) ?
join(', ', @$req) : $req;
549 $res .= Text
::Wrap
::wrap
($indend, $indend, ($tmp)). "\n\n";
553 die "unknown format '$format'";
559 # translate parameter mapping definition
560 # $mapping_array is a array which can contain:
561 # strings ... in that case we assume it is a parameter name, and
562 # we want to load that parameter from a file
563 # [ param_name, func, desc] ... allows you to specify a arbitrary
564 # mapping func for any param
566 # Returns: a hash indexed by parameter_name,
567 # i.e. { param_name => { func => .., desc => ... } }
568 my $compute_param_mapping_hash = sub {
569 my ($mapping_array) = @_;
573 return $res if !defined($mapping_array);
575 foreach my $item (@$mapping_array) {
576 my ($name, $func, $desc, $interactive);
577 if (ref($item) eq 'ARRAY') {
578 ($name, $func, $desc, $interactive) = @$item;
579 } elsif (ref($item) eq 'HASH') {
581 $res->{$item->{name
}} = $item;
585 $func = sub { return PVE
::Tools
::file_get_contents
($_[0]) };
587 $desc //= '<filepath>';
588 $res->{$name} = { desc
=> $desc, func
=> $func, interactive
=> $interactive };
594 # generate usage information for command line tools
596 # $info ... method info
597 # $prefix ... usually something like "$exename $cmd" ('pvesm add')
598 # $arg_param ... list of parameters we want to get as ordered arguments
599 # on the command line (or single parameter name for lists)
600 # $fixed_param ... do not generate and info about those parameters
602 # 'long' ... default (text, list all options)
603 # 'short' ... command line only (text, one line)
604 # 'full' ... text, include description
605 # 'asciidoc' ... generate asciidoc for man pages (like 'full')
606 # $param_cb ... mapping for string parameters to file path parameters
607 # $formatter_properties ... additional property definitions (passed to output formatter)
609 my ($info, $prefix, $arg_param, $fixed_param, $format, $param_cb, $formatter_properties) = @_;
611 $format = 'long' if !$format;
613 my $schema = $info->{parameters
};
614 my $name = $info->{name
};
615 my $prop = { %{$schema->{properties
}} }; # copy
617 my $has_output_format_option = $formatter_properties->{'output-format'} ?
1 : 0;
619 if ($formatter_properties) {
620 foreach my $key (keys %$formatter_properties) {
621 if (!$standard_output_options->{$key}) {
622 $prop->{$key} = $formatter_properties->{$key};
627 # also remove $standard_output_options from $prop (pvesh, pveclient)
628 if ($prop->{'output-format'}) {
629 $has_output_format_option = 1;
630 foreach my $key (keys %$prop) {
631 if ($standard_output_options->{$key}) {
632 delete $prop->{$key};
643 $arg_param = [ $arg_param ] if $arg_param && !ref($arg_param);
645 foreach my $p (@$arg_param) {
646 next if !$prop->{$p}; # just to be sure
647 my $pd = $prop->{$p};
650 $args .= " " if $args;
651 if ($pd->{format
} && $pd->{format
} =~ m/-list/) {
654 $args .= $pd->{optional
} ?
"[<$p>]" : "<$p>";
659 foreach my $k (@$arg_param) {
660 next if defined($fixed_param->{$k}); # just to be sure
661 next if !$prop->{$k}; # just to be sure
662 $argdescr .= $get_property_description->($k, 'fixed', $prop->{$k}, $format);
665 my $idx_param = {}; # -vlan\d+ -scsi\d+
668 foreach my $k (sort keys %$prop) {
669 next if $arg_hash->{$k};
670 next if defined($fixed_param->{$k});
672 my $type_text = $prop->{$k}->{type
} || 'string';
676 if (defined($param_cb)) {
677 my $mapping = $param_cb->($name);
678 $param_map = $compute_param_mapping_hash->($mapping);
679 next if $k eq 'password' && $param_map->{$k} && !$prop->{$k}->{optional
};
683 if ($k =~ m/^([a-z]+)(\d+)$/) {
684 my ($name, $idx) = ($1, $2);
685 next if $idx_param->{$name};
686 if ($idx == 0 && defined($prop->{"${name}1"})) {
687 $idx_param->{$name} = 1;
688 $base = "${name}[n]";
693 $opts .= $get_property_description->($base, 'arg', $prop->{$k}, $format, $param_map->{$k});
695 if (!$prop->{$k}->{optional
}) {
696 $args .= " " if $args;
697 $args .= "--$base <$type_text>"
701 if ($format eq 'asciidoc') {
702 $out .= "*${prefix}*";
703 $out .= " `$args`" if $args;
704 $out .= " `[OPTIONS]`" if $opts;
705 $out .= " `[FORMAT_OPTIONS]`" if $has_output_format_option;
708 $out .= "USAGE: " if $format ne 'short';
709 $out .= "$prefix $args";
710 $out .= " [OPTIONS]" if $opts;
711 $out .= " [FORMAT_OPTIONS]" if $has_output_format_option;
715 return $out if $format eq 'short';
717 if ($info->{description
}) {
718 if ($format eq 'asciidoc') {
719 my $desc = Text
::Wrap
::wrap
('', '', ($info->{description
}));
720 $out .= "\n$desc\n\n";
721 } elsif ($format eq 'full') {
722 my $desc = Text
::Wrap
::wrap
(' ', ' ', ($info->{description
}));
723 $out .= "\n$desc\n\n";
727 $out .= $argdescr if $argdescr;
729 $out .= $opts if $opts;
735 my ($self, $name, $prefix, $arg_param, $fixed_param, $format, $param_cb, $formatter_properties) = @_;
737 my $info = $self->map_method_by_name($name);
739 return getopt_usage
($info, $prefix, $arg_param, $fixed_param, $format, $param_cb, $formatter_properties);
742 # generate docs from JSON schema properties
743 sub dump_properties
{
744 my ($prop, $format, $style, $filterFn) = @_;
750 my $idx_param = {}; # -vlan\d+ -scsi\d+
752 foreach my $k (sort keys %$prop) {
753 my $phash = $prop->{$k};
755 next if defined($filterFn) && &$filterFn($k, $phash);
756 next if $phash->{alias
};
759 if ($k =~ m/^([a-z]+)(\d+)$/) {
760 my ($name, $idx) = ($1, $2);
761 next if $idx_param->{$name};
762 if ($idx == 0 && defined($prop->{"${name}1"})) {
763 $idx_param->{$name} = 1;
764 $base = "${name}[n]";
768 $raw .= $get_property_description->($base, $style, $phash, $format);
770 next if $style ne 'config';
772 my $prop_fmt = $phash->{format
};
775 if (ref($prop_fmt) ne 'HASH') {
776 $prop_fmt = PVE
::JSONSchema
::get_format
($prop_fmt);
779 next if !(ref($prop_fmt) && (ref($prop_fmt) eq 'HASH'));
781 $raw .= dump_properties
($prop_fmt, $format, 'config-sub')
788 my $replace_file_names_with_contents = sub {
789 my ($param, $param_map) = @_;
791 while (my ($k, $d) = each %$param_map) {
792 next if $d->{interactive
}; # handled by the JSONSchema's get_options code
793 $param->{$k} = $d->{func
}->($param->{$k})
794 if defined($param->{$k});
800 sub add_standard_output_properties
{
801 my ($propdef, $list) = @_;
805 $list //= [ keys %$standard_output_options ];
807 my $res = { %$propdef }; # copy
809 foreach my $opt (@$list) {
810 die "no such standard output option '$opt'\n" if !defined($standard_output_options->{$opt});
811 die "detected overwriten standard CLI parameter '$opt'\n" if defined($res->{$opt});
812 $res->{$opt} = $standard_output_options->{$opt};
818 sub extract_standard_output_properties
{
822 foreach my $opt (keys %$standard_output_options) {
823 $options->{$opt} = delete $data->{$opt} if defined($data->{$opt});
830 my ($self, $prefix, $name, $args, $arg_param, $fixed_param, $param_cb, $formatter_properties) = @_;
832 my $info = $self->map_method_by_name($name);
838 $param_map = $compute_param_mapping_hash->($param_cb->($name)) if $param_cb;
839 my $schema = { %{$info->{parameters
}} }; # copy
840 $schema->{properties
} = { %{$schema->{properties
}}, %$formatter_properties } if $formatter_properties;
841 my $param = PVE
::JSONSchema
::get_options
($schema, $args, $arg_param, $fixed_param, $param_map);
843 if ($formatter_properties) {
844 foreach my $opt (keys %$formatter_properties) {
845 $fmt_param->{$opt} = delete $param->{$opt} if defined($param->{$opt});
849 if (defined($param_map)) {
850 $replace_file_names_with_contents->($param, $param_map);
853 $res = $self->handle($info, $param);
858 die $err if !$ec || $ec ne "PVE::Exception" || !$err->is_param_exc();
860 $err->{usage
} = $self->usage_str($name, $prefix, $arg_param, $fixed_param, 'short', $param_cb, $formatter_properties);
865 return wantarray ?
($res, $fmt_param) : $res;
869 # note: this modifies the original hash by adding the id property
871 my ($hash, $idprop) = @_;
874 return $res if !$hash;
876 foreach my $k (keys %$hash) {
877 $hash->{$k}->{$idprop} = $k;
878 push @$res, $hash->{$k};