]> git.proxmox.com Git - pve-client.git/commitdiff
Helpers.pm: new helper find_method_info()
authorDietmar Maurer <dietmar@proxmox.com>
Mon, 18 Jun 2018 06:24:17 +0000 (08:24 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Mon, 18 Jun 2018 07:51:36 +0000 (09:51 +0200)
To lookup method info with extracted uri parameters.

PVE/APIClient/Helpers.pm
pveclient

index c4bc2c80493d9e92304b9eb6cbc759f8f3f47cfc..dcec3dc922f592b56562ce2f16438ebfa084124e 100644 (file)
@@ -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);
        }
     };
 
index 34e2aab8b1b14028e534bc0024cf8420ad529fa1..9177db68160fc31d10a0284a59ebf7f18b027b99 100755 (executable)
--- 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 <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};
 }