]> git.proxmox.com Git - pve-client.git/blobdiff - PVE/APIClient/Helpers.pm
fully implement api get/set/create/delete
[pve-client.git] / PVE / APIClient / Helpers.pm
index b83829b39f8216a901d31693e4f04bbca0089a16..d8a7fba0e50ca1069919293b3d3727f51c786765 100644 (file)
@@ -43,6 +43,23 @@ $build_pve_api_path_hash = sub {
     }
 };
 
+my $default_output_format = 'table';
+my $client_output_format =  $default_output_format;
+
+sub set_output_format {
+    my ($format) = @_;
+
+    if (!defined($format)) {
+       $client_output_format =  $default_output_format;
+    } else {
+       $client_output_format =  $format;
+    }
+}
+
+sub get_output_format {
+    return $client_output_format;
+}
+
 sub get_api_definition {
 
     if (!defined($pve_api_definition)) {
@@ -175,7 +192,7 @@ sub extract_path_info {
            $test_path_properties->([$0, @ARGV]);
        } elsif ($cmd eq 'bashcomplete') {
            my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT});
-           my $args = PVE::Tools::split_args($cmdline);
+           my $args = PVE::APIClient::Tools::split_args($cmdline);
            $test_path_properties->($args);
        }
     }
@@ -183,4 +200,43 @@ sub extract_path_info {
     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;