[pve-devel] [qemu-server cli cleanup 4/6] qm status: use print_api_result

Dietmar Maurer dietmar at proxmox.com
Thu Aug 9 11:43:50 CEST 2018


Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
 PVE/CLI/qm.pm | 84 +++++++++++++----------------------------------------------
 1 file changed, 18 insertions(+), 66 deletions(-)

diff --git a/PVE/CLI/qm.pm b/PVE/CLI/qm.pm
index f894b8a..0116f21 100755
--- a/PVE/CLI/qm.pm
+++ b/PVE/CLI/qm.pm
@@ -87,32 +87,6 @@ sub run_vnc_proxy {
     exit(0);
 }
 
-sub print_recursive_hash {
-    my ($prefix, $hash, $key) = @_;
-
-    if (ref($hash) eq 'HASH') {
-	if (defined($key)) {
-	    print "$prefix$key:\n";
-	}
-	foreach my $itemkey (keys %$hash) {
-	    print_recursive_hash("\t$prefix", $hash->{$itemkey}, $itemkey);
-	}
-    } elsif (ref($hash) eq 'ARRAY') {
-	if (defined($key)) {
-	    print "$prefix$key:\n";
-	}
-	foreach my $item (@$hash) {
-	    print_recursive_hash("\t$prefix", $item);
-	}
-    } elsif (!ref($hash) && defined($hash)) {
-	if (defined($key)) {
-	    print "$prefix$key: $hash\n";
-	} else {
-	    print "$prefix$hash\n";
-	}
-    }
-}
-
 __PACKAGE__->register_method ({
     name => 'showcmd',
     path => 'showcmd',
@@ -145,45 +119,6 @@ __PACKAGE__->register_method ({
     }});
 
 __PACKAGE__->register_method ({
-    name => 'status',
-    path => 'status',
-    method => 'GET',
-    description => "Show VM status.",
-    parameters => {
-	additionalProperties => 0,
-	properties => {
-	    vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
-	    verbose => {
-		description => "Verbose output format",
-		type => 'boolean',
-		optional => 1,
-	    }
-	},
-    },
-    returns => { type => 'null'},
-    code => sub {
-	my ($param) = @_;
-
-	# test if VM exists
-	my $conf = PVE::QemuConfig->load_config ($param->{vmid});
-
-	my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid}, 1);
-	my $stat = $vmstatus->{$param->{vmid}};
-	if ($param->{verbose}) {
-	    foreach my $k (sort (keys %$stat)) {
-		next if $k eq 'cpu' || $k eq 'relcpu'; # always 0
-		my $v = $stat->{$k};
-		print_recursive_hash("", $v, $k);
-	    }
-	} else {
-	    my $status = $stat->{qmpstatus} || 'unknown';
-	    print "status: $status\n";
-	}
-
-	return undef;
-    }});
-
-__PACKAGE__->register_method ({
     name => 'vncproxy',
     path => 'vncproxy',
     method => 'PUT',
@@ -832,7 +767,24 @@ our $cmddef = {
 
     showcmd => [ __PACKAGE__, 'showcmd', ['vmid']],
 
-    status => [ __PACKAGE__, 'status', ['vmid']],
+    status => [  "PVE::API2::Qemu", 'vm_status', ['vmid'],  { node => $nodename },
+		sub {
+		    my ($data, $schema, $options) = @_;
+
+		    if ($options->{verbose}) {
+			delete $data->{cpu}; # always 0
+			delete $data->{relcpu}; # always 0
+			PVE::CLIFormatter::print_api_result($data, $schema, undef, $options);
+		    } else {
+			my $status = $data->{qmpstatus} || 'unknown';
+			PVE::CLIFormatter::print_api_result($status, {  type => 'string' }, undef, $options);
+		    }
+		 }, PVE::RESTHandler::add_standard_output_properties({
+		     verbose => {
+			 description => "Verbose output format.",
+			 type => 'boolean',
+			 optional => 1,
+		     }})],
 
     snapshot => [ "PVE::API2::Qemu", 'snapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
 
-- 
2.11.0




More information about the pve-devel mailing list