return $pve_api_definition;
}
-sub lookup_api_method {
- my ($path, $method, $noerr) = @_;
+my $map_path_to_info = sub {
+ my ($child_list, $stack, $uri_param) = @_;
+
+ while (defined(my $comp = shift @$stack)) {
+ foreach my $child (@$child_list) {
+ my $text = $child->{text};
+
+ if ($text eq $comp) {
+ # found
+ } elsif ($text =~ m/^\{(\S+)\}$/) {
+ # found
+ $uri_param->{$1} = $comp;
+ } else {
+ next; # text next child
+ }
+ if ($child->{leaf} || !scalar(@$stack)) {
+ return $child;
+ } else {
+ $child_list = $child->{children};
+ last; # test next path component
+ }
+ }
+ }
+ return undef;
+};
- get_api_definition(); # make sure API data is loaded
+sub find_method_info {
+ my ($path, $method, $uri_param, $noerr) = @_;
- my $info = $pve_api_path_hash->{$path};
+ $uri_param //= {};
- if (!$info) {
- return undef if $noerr;
- die "unable to find API info for path '$path'\n";
- }
+ my $stack = [ grep { length($_) > 0 } split('\/+' , $path)]; # skip empty fragments
- my $data = $info->{info}->{$method};
+ my $child = $map_path_to_info->(get_api_definition(), $stack, $uri_param);
- if (!$data) {
+ if (!($child && $child->{info} && $child->{info}->{$method})) {
return undef if $noerr;
die "unable to find API method '$method' for path '$path'\n";
}
- return $data;
+ return $child->{info}->{$method};
}
sub complete_api_call_options {
$dir = '';
$info = { children => $pve_api_definition };
} else {
- $info = $pve_api_path_hash->{"/$dir"};
+ my $stack = [ grep { length($_) > 0 } split('\/+' , $dir)]; # skip empty fragments
+ $info = $map_path_to_info->($pve_api_definition, $stack, {});
}
my $res = [];
if ($info) {
+ my $prefix = length($dir) ? "/$dir/" : '/';
if (my $children = $info->{children}) {
foreach my $c (@$children) {
- if ($c->{path} =~ m!\Q$dir/$rest!) {
- push @$res, $c->{path};
- push @$res, "$c->{path}/" if $c->{children};
+ my $ctext = $c->{text};
+ if ($ctext =~ m/^\{(\S+)\}$/) {
+ push @$res, "$prefix$ctext";
+ push @$res, "$prefix$ctext/";
+ if (length($rest)) {
+ push @$res, "$prefix$rest";
+ push @$res, "$prefix$rest/";
+ }
+ } elsif ($ctext =~ m/^\Q$rest/) {
+ push @$res, "$prefix$ctext";
+ push @$res, "$prefix$ctext/" if $c->{children};
}
}
}
}
+
return $res;
}
# test for command lines with api calls (or similar bash completion calls):
# example1: pveclient api get remote1 /cluster
sub extract_path_info {
+ my ($uri_param) = @_;
my $info;
my $path = $args->[4];
if (my $method = $method_map->{$args->[2]}) {
- $info = lookup_api_method($path, $method, 1);
+ $info = find_method_info($path, $method, $uri_param, 1);
}
};
my $config = PVE::APIClient::Config->load();
- # test if api path exists
- my $info = PVE::APIClient::Helpers::lookup_api_method($path, $method);
+ my $uri_param = {};
+ my $info = PVE::APIClient::Helpers::find_method_info($path, $method, $uri_param);
my $conn = PVE::APIClient::Config->remote_conn($config, $remote);
# dynamically update schema definition for direct API call
# like: pveclient api <get|set|create|delete|help> <remote> <path>
-if (my $info = PVE::APIClient::Helpers::extract_path_info()) {
- $path_properties = $info->{parameters}->{properties};
+my $uri_param = {};
+if (my $info = PVE::APIClient::Helpers::extract_path_info($uri_param)) {
+ foreach my $key (keys %{$info->{parameters}->{properties}}) {
+ next if defined($uri_param->{$key});
+ $path_properties->{$key} = $info->{parameters}->{properties}->{$key};
+ }
$path_returns = $info->{returns};
}