]> git.proxmox.com Git - pve-client.git/blobdiff - PVE/APIClient/Helpers.pm
Add update-pve-common make target to move code to PVE/APIClient.
[pve-client.git] / PVE / APIClient / Helpers.pm
index 8bf11bb56bff41a03e8b21ae38c06baa2be5f664..1ea8a5e25ea34ddfd9a47d7132e7125550a4af2c 100644 (file)
@@ -3,7 +3,7 @@ package PVE::APIClient::Helpers;
 use strict;
 use warnings;
 
-use Data::Dumper;
+use Storable;
 use JSON;
 use PVE::APIClient::Exception qw(raise);
 use Encode::Locale;
@@ -13,7 +13,14 @@ use HTTP::Status qw(:constants);
 my $pve_api_definition;
 my $pve_api_path_hash;
 
-my $pve_api_definition_fn = "/usr/share/pve-client/pve-api-definition.js";
+my $pve_api_definition_fn = "/usr/share/pve-client/pve-api-definition.dat";
+
+my $method_map = {
+    create => 'POST',
+    set => 'PUT',
+    get => 'GET',
+    delete => 'DELETE',
+};
 
 my $build_pve_api_path_hash;
 $build_pve_api_path_hash = sub {
@@ -39,16 +46,12 @@ $build_pve_api_path_hash = sub {
 sub get_api_definition {
 
     if (!defined($pve_api_definition)) {
-       local $/;
        open(my $fh, '<',  $pve_api_definition_fn) ||
            die "unable to open '$pve_api_definition_fn' - $!\n";
-       my $json_text = <$fh>;
-       $pve_api_definition = decode_json($json_text);
-
+       $pve_api_definition = Storable::fd_retrieve($fh);
        $build_pve_api_path_hash->($pve_api_definition);
     }
 
-
     return $pve_api_definition;
 }
 
@@ -135,16 +138,88 @@ sub complete_api_path {
        $info = $pve_api_path_hash->{"/$dir"};
     }
 
+    my $res = [];
     if ($info) {
        if (my $children = $info->{children}) {
            foreach my $c (@$children) {
                if ($c->{path} =~ m!\Q$dir/$rest!) {
-                   print "$c->{path}\n";
-                   print "$c->{path}/\n"if $c->{children};
+                   push @$res, $c->{path};
+                   push @$res, "$c->{path}/" 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 $info;
+
+    my $test_path_properties = sub {
+       my ($args) = @_;
+
+       return if scalar(@$args) < 5;
+       return if $args->[1] ne 'api';
+
+       my $path = $args->[4];
+       if (my $method = $method_map->{$args->[2]}) {
+           $info = lookup_api_method($path, $method, 1);
+       }
+    };
+
+    if (defined(my $cmd = $ARGV[0])) {
+       if ($cmd eq 'api') {
+           $test_path_properties->([$0, @ARGV]);
+       } elsif ($cmd eq 'bashcomplete') {
+           my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT});
+           my $args = PVE::APIClient::Tools::split_args($cmdline);
+           $test_path_properties->($args);
+       }
+    }
+
+    return $info;
+}
+
+sub get_vmid_resource {
+    my ($conn, $vmid) = @_;
+
+    my $resources = $conn->get('api2/json/cluster/resources', {type => 'vm'});
+
+    my $resource;
+    for my $tmp (@$resources) {
+       if ($tmp->{vmid} eq $vmid) {
+           $resource = $tmp;
+           last;
+       }
+    }
+
+    if (!defined($resource)) {
+       die "\"$vmid\" not found";
+    }
+
+    return $resource;
+}
+
+sub poll_task {
+    my ($conn, $node, $upid) = @_;
+
+    my $path = "api2/json/nodes/$node/tasks/$upid/status";
+
+    my $task_status;
+    while(1) {
+       $task_status = $conn->get($path, {});
+
+       if ($task_status->{status} eq "stopped") {
+           last;
+       }
+
+       sleep(10);
+    }
+
+    return $task_status->{exitstatus};
 }
 
 1;