[pve-devel] applied (partly): [qemu-server] qm: try to use new PVE::CLIFormatter::print_api_result

Thomas Lamprecht t.lamprecht at proxmox.com
Wed Jul 11 10:22:04 CEST 2018


On 7/10/18 1:59 PM, Dietmar Maurer wrote:
> Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
> ---
>  PVE/API2/Qemu.pm | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++----
>  PVE/CLI/qm.pm    | 60 ++++++++++++++++-----------------------------
>  2 files changed, 91 insertions(+), 44 deletions(-)
> 

Applied the API changes, i.e., the addition of more return-schema definitions.
Omitted the CLI stuff for now as we decided to still wait with using the new
CLIFormatter stuff...

> diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
> index c15c71f..655f514 100644
> --- a/PVE/API2/Qemu.pm
> +++ b/PVE/API2/Qemu.pm
> @@ -365,7 +365,41 @@ __PACKAGE__->register_method({
>  	type => 'array',
>  	items => {
>  	    type => "object",
> -	    properties => {},
> +	    properties => {
> +		vmid => get_standard_option('pve-vmid'),
> +		status => {
> +		    description => "Qemu process status.",
> +		    type => 'string',
> +		    enum => ['stopped', 'running'],
> +		},
> +		maxmem => {
> +		    description => "Maximum memory in bytes.",
> +		    type => 'integer',
> +		    optional => 1,
> +		    renderer => 'bytes',
> +		},
> +		maxdisk => {
> +		    description => "Root disk size in bytes.",
> +		    type => 'integer',
> +		    optional => 1,
> +		    renderer => 'bytes',
> +		},
> +		name => {
> +		    description => "VM name.",
> +		    type => 'string',
> +		    optional => 1,
> +		},
> +		qmpstatus => {
> +		    description => "Qemu QMP agent status.",
> +		    type => 'string',
> +		    optional => 1,
> +		},
> +		pid => {
> +		    description => "PID of running qemu process.",
> +		    type => 'integer',
> +		    optional => 1,
> +		},
> +	    },
>  	},
>  	links => [ { rel => 'child', href => "{vmid}" } ],
>      },
> @@ -828,13 +862,14 @@ __PACKAGE__->register_method({
>  	},
>      },
>      returns => {
> +	description => "The current VM configuration.",
>  	type => "object",
> -	properties => {
> +	properties => PVE::QemuServer::json_config_properties({
>  	    digest => {
>  		type => 'string',
>  		description => 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
>  	    }
> -	},
> +	}),
>      },
>      code => sub {
>  	my ($param) = @_;
> @@ -3319,7 +3354,32 @@ __PACKAGE__->register_method({
>  	type => 'array',
>  	items => {
>  	    type => "object",
> -	    properties => {},
> +	    properties => {
> +		name => {
> +		    description => "Snapshot identifier. Value 'current' identifies the current VM.",
> +		    type => 'string',
> +		},
> +		vmstate => {
> +		    description => "Snapshot includes RAM.",
> +		    type => 'boolean',
> +		    optional => 1,
> +		},
> +		description => {
> +		    description => "Snapshot description.",
> +		    type => 'string',
> +		},
> +		snaptime => {
> +		    description => "Snapshot creation time",
> +		    type => 'integer',
> +		    renderer => 'timestamp',
> +		    optional => 1,
> +		},
> +		parent => {
> +		    description => "Parent snapshot identifier.",
> +		    type => 'string',
> +		    optional => 1,
> +		},
> +	    },
>  	},
>  	links => [ { rel => 'child', href => "{name}" } ],
>      },
> @@ -3347,7 +3407,12 @@ __PACKAGE__->register_method({
>  	}
>  
>  	my $running = PVE::QemuServer::check_running($vmid, 1) ? 1 : 0;
> -	my $current = { name => 'current', digest => $conf->{digest}, running => $running };
> +	my $current = {
> +	    name => 'current',
> +	    digest => $conf->{digest},
> +	    running => $running,
> +	    description => "You are here!",
> +	};
>  	$current->{parent} = $conf->{parent} if $conf->{parent};
>  
>  	push @$res, $current;
> diff --git a/PVE/CLI/qm.pm b/PVE/CLI/qm.pm
> index 48fbc5f..491d671 100755
> --- a/PVE/CLI/qm.pm
> +++ b/PVE/CLI/qm.pm
> @@ -682,22 +682,9 @@ sub param_mapping {
>  our $cmddef = {
>      list => [ "PVE::API2::Qemu", 'vmlist', [],
>  	     { node => $nodename }, sub {
> -		 my $vmlist = shift;
> -
> -		 exit 0 if (!scalar(@$vmlist));
> -
> -		 printf "%10s %-20s %-10s %-10s %12s %-10s\n",
> -		 qw(VMID NAME STATUS MEM(MB) BOOTDISK(GB) PID);
> -
> -		 foreach my $rec (sort { $a->{vmid} <=> $b->{vmid} } @$vmlist) {
> -		     printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid}, $rec->{name},
> -		     $rec->{qmpstatus} || $rec->{status},
> -		     ($rec->{maxmem} || 0)/(1024*1024),
> -		     ($rec->{maxdisk} || 0)/(1024*1024*1024),
> -		     $rec->{pid}||0;
> -		 }
> -
> -
> +		 my ($data, $schema, $options) = @_;
> +		 my $proplist = ['vmid', 'name', 'status', 'qmpstatus', 'maxmem', 'maxdisk', 'pid'];
> +		 PVE::CLIFormatter::print_api_result($data, $schema, $proplist, $options);
>  	      } ],
>  
>      create => [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node => $nodename }, $upid_exit ],
> @@ -718,20 +705,15 @@ our $cmddef = {
>  
>      config => [ "PVE::API2::Qemu", 'vm_config', ['vmid'],
>  		{ node => $nodename }, sub {
> -		    my $config = shift;
> -		    foreach my $k (sort (keys %$config)) {
> -			next if $k eq 'digest';
> -			my $v = $config->{$k};
> -			if ($k eq 'description') {
> -			    $v = PVE::Tools::encode_text($v);
> -			}
> -			print "$k: $v\n";
> -		    }
> +		    my ($data, $schema, $options) = @_;
> +		    PVE::CLIFormatter::print_api_result($data, $schema, undef, $options);
>  		}],
>  
>      pending => [ "PVE::API2::Qemu", 'vm_pending', ['vmid'],
>  		{ node => $nodename }, sub {
> -		    my $data = shift;
> +		    my ($data, $schema, $options) = @_;
> +
> +		    my $table = [];
>  		    foreach my $item (sort { $a->{key} cmp $b->{key}} @$data) {
>  			my $k = $item->{key};
>  			next if $k eq 'digest';
> @@ -743,17 +725,21 @@ our $cmddef = {
>  			}
>  			if (defined($v)) {
>  			    if ($item->{delete}) {
> -				print "del $k: $v\n";
> +				push @$table, { status => 'del', key => $k, value => $v };
>  			    } elsif (defined($p)) {
> -				print "cur $k: $v\n";
> -				print "new $k: $p\n";
> +				push @$table, { status => 'cur', key => $k, value => $v };
> +				push @$table, { status => 'new', key => $k, value => $p };
>  			    } else {
> -				print "cur $k: $v\n";
> +				push @$table, { status => 'cur', key => $k, value => $v };
>  			    }
>  			} elsif (defined($p)) {
> -			    print "new $k: $p\n";
> +			    push @$table, { status => 'new', key => $k, value => $p };
>  			}
>  		    }
> +
> +		    my $proplist = ['status', 'key', 'value'];
> +		    my $tableschema = { type => 'array', items => { type => 'object' }};
> +		    PVE::CLIFormatter::print_api_result($table, $tableschema, $proplist, $options);
>  		}],
>  
>      showcmd => [ __PACKAGE__, 'showcmd', ['vmid']],
> @@ -765,14 +751,10 @@ our $cmddef = {
>      delsnapshot => [ "PVE::API2::Qemu", 'delsnapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
>  
>      listsnapshot => [ "PVE::API2::Qemu", 'snapshot_list', ['vmid'], { node => $nodename },
> -		    sub {
> -			my $res = shift;
> -			foreach my $e (@$res) {
> -			    my $headline = $e->{description} || 'no-description';
> -			    $headline =~ s/\n.*//sg;
> -			    my $parent = $e->{parent} // 'no-parent';
> -			    printf("%-20s %-20s %s\n", $e->{name}, $parent, $headline);
> -			}
> +		      sub {
> +			  my ($data, $schema, $options) = @_;
> +			  my $proplist = ['name', 'snaptime', 'vmstate', 'parent', 'description'];
> +			  PVE::CLIFormatter::print_api_result($data, $schema, $proplist, $options);
>  		    }],
>  
>      rollback => [ "PVE::API2::Qemu", 'rollback', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
> 





More information about the pve-devel mailing list