]> git.proxmox.com Git - pve-client.git/blobdiff - PVE/APIClient/Helpers.pm
Handle errors on tasks correctly with a die,
[pve-client.git] / PVE / APIClient / Helpers.pm
index dcec3dc922f592b56562ce2f16438ebfa084124e..5c2e4cb661353b2284497cf0023b754a4f52963d 100644 (file)
@@ -7,13 +7,13 @@ use Storable;
 use JSON;
 use File::Path qw(make_path);
 
+use PVE::APIClient::JSONSchema;
 use PVE::APIClient::Exception qw(raise);
 use Encode::Locale;
 use Encode;
 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.dat";
 
@@ -24,27 +24,6 @@ my $method_map = {
     delete => 'DELETE',
 };
 
-my $build_pve_api_path_hash;
-$build_pve_api_path_hash = sub {
-    my ($tree) = @_;
-
-    my $class = ref($tree);
-    return $tree if !$class;
-
-    if ($class eq 'ARRAY') {
-       foreach my $el (@$tree) {
-           $build_pve_api_path_hash->($el);
-       }
-    } elsif ($class eq 'HASH') {
-       if (defined($tree->{leaf}) && defined(my $path = $tree->{path})) {
-           $pve_api_path_hash->{$path} = $tree;
-       }
-       foreach my $k (keys %$tree) {
-           $build_pve_api_path_hash->($tree->{$k});
-       }
-    }
-};
-
 my $default_output_format = 'text';
 my $client_output_format =  $default_output_format;
 
@@ -96,13 +75,57 @@ sub print_result {
     }
 }
 
+
+my $__real_remove_formats; $__real_remove_formats = sub {
+    my ($properties) = @_;
+
+    foreach my $pname (keys %$properties) {
+       if (my $d = $properties->{$pname}) {
+           if (defined(my $format = $d->{format})) {
+               if (ref($format)) {
+                   $__real_remove_formats->($format);
+               } elsif (!PVE::APIClient::JSONSchema::get_format($format)) {
+                   # simply remove unknown format definitions
+                   delete $d->{format};
+               }
+           }
+       }
+    }
+};
+
+my $remove_unknown_formats; $remove_unknown_formats = sub {
+    my ($tree) = @_;
+
+    my $class = ref($tree);
+    return if !$class;
+
+    if ($class eq 'ARRAY') {
+       foreach my $el (@$tree) {
+          $remove_unknown_formats->($el);
+       }
+    } elsif ($class eq 'HASH') {
+       if (my $info = $tree->{info}) {
+           for my $method (qw(GET PUT PUSH DELETE)) {
+               next if !$info->{$method};
+               my $properties = $info->{$method}->{parameters}->{properties};
+               $__real_remove_formats->($properties) if $properties;
+           }
+       }
+       if ($tree->{children}) {
+           $remove_unknown_formats->($tree->{children});
+       }
+    }
+    return;
+};
+
 sub get_api_definition {
 
     if (!defined($pve_api_definition)) {
        open(my $fh, '<',  $pve_api_definition_fn) ||
            die "unable to open '$pve_api_definition_fn' - $!\n";
        $pve_api_definition = Storable::fd_retrieve($fh);
-       $build_pve_api_path_hash->($pve_api_definition);
+
+       $remove_unknown_formats->($pve_api_definition);
     }
 
     return $pve_api_definition;
@@ -195,6 +218,14 @@ sub complete_api_call_options {
     &$print_result(@option_list);
 }
 
+sub merge_api_definition_properties {
+    my ($path, $method, $properties) = @_;
+
+    my $info = PVE::APIClient::Helpers::find_method_info($path, $method);
+
+    return { %{$info->{parameters}->{properties}}, %$properties };
+}
+
 sub complete_api_path {
     my ($text) = @_;
 
@@ -290,19 +321,42 @@ sub get_vmid_resource {
 }
 
 sub poll_task {
-    my ($conn, $node, $upid) = @_;
+    my ($conn, $node, $upid, $quiet) = @_;
 
     my $path = "api2/json/nodes/$node/tasks/$upid/status";
 
     my $task_status;
+    my $last_line = 0;
     while(1) {
+       if (!$quiet) {
+           my $path = "api2/json/nodes/$node/tasks/$upid/log";
+           my $task_log = $conn->get($path, {start => $last_line});
+
+           my $printme = '';
+           for my $li (@$task_log) {
+               if ($li->{t} eq 'no content') {
+                   next;
+               }
+               $printme .= $li->{t} . "\n";
+               $last_line = $li->{n};
+           }
+
+           if ($printme ne '') {
+               print $printme;
+           }
+       }
+
        $task_status = $conn->get($path, {});
 
        if ($task_status->{status} eq "stopped") {
            last;
        }
 
-       sleep(10);
+       sleep(2);
+    }
+
+    if ($task_status->{exitstatus} ne "OK") {
+       die $task_status->{exitstatus};
     }
 
     return $task_status->{exitstatus};