From: Dietmar Maurer Date: Mon, 18 Jun 2018 06:24:17 +0000 (+0200) Subject: Helpers.pm: new helper find_method_info() X-Git-Url: https://git.proxmox.com/?p=pve-client.git;a=commitdiff_plain;h=520f543ef857f504e30780f4d4484acc9e92fccd Helpers.pm: new helper find_method_info() To lookup method info with extracted uri parameters. --- diff --git a/PVE/APIClient/Helpers.pm b/PVE/APIClient/Helpers.pm index c4bc2c8..dcec3dc 100644 --- a/PVE/APIClient/Helpers.pm +++ b/PVE/APIClient/Helpers.pm @@ -108,26 +108,47 @@ sub get_api_definition { 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 { @@ -188,26 +209,38 @@ sub complete_api_path { $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; @@ -219,7 +252,7 @@ sub extract_path_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); } }; diff --git a/pveclient b/pveclient index 34e2aab..9177db6 100755 --- a/pveclient +++ b/pveclient @@ -36,8 +36,8 @@ sub call_api_method { 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); @@ -84,8 +84,12 @@ my $path_returns = { type => 'null' }; # dynamically update schema definition for direct API call # like: pveclient api -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}; }