[pve-devel] [PATCH pve-client v3] Add a simple implementation of list

René Jochum r.jochum at proxmox.com
Mon Jun 11 11:35:27 CEST 2018


Signed-off-by: René Jochum <r.jochum at proxmox.com>
---
This removes all the logic from previous patches where I quieried /config from each VM/LXC.


 PVE/APIClient/Commands/help.pm |  2 ++
 PVE/APIClient/Commands/list.pm | 58 ++++++++++++++++++++++++++++++++++++++++++
 PVE/APIClient/Commands/lxc.pm  | 21 ---------------
 pveclient                      |  2 ++
 4 files changed, 62 insertions(+), 21 deletions(-)
 create mode 100644 PVE/APIClient/Commands/list.pm

diff --git a/PVE/APIClient/Commands/help.pm b/PVE/APIClient/Commands/help.pm
index 407af5e..81d4d4a 100644
--- a/PVE/APIClient/Commands/help.pm
+++ b/PVE/APIClient/Commands/help.pm
@@ -4,6 +4,7 @@ use strict;
 use warnings;
 
 use PVE::APIClient::Commands::help;
+use PVE::APIClient::Commands::list;
 use PVE::APIClient::Commands::lxc;
 use PVE::APIClient::Commands::remote;
 
@@ -55,6 +56,7 @@ __PACKAGE__->register_method ({
 	};
 
 	$assemble_usage_string->('help', $PVE::APIClient::Commands::help::cmddef);
+	$assemble_usage_string->('list', $PVE::APIClient::Commands::list::cmddef);
 	$assemble_usage_string->('lxc', $PVE::APIClient::Commands::lxc::cmddef);
 	$assemble_usage_string->('remote', $PVE::APIClient::Commands::remote::cmddef);
 
diff --git a/PVE/APIClient/Commands/list.pm b/PVE/APIClient/Commands/list.pm
new file mode 100644
index 0000000..aed0a54
--- /dev/null
+++ b/PVE/APIClient/Commands/list.pm
@@ -0,0 +1,58 @@
+package PVE::APIClient::Commands::list;
+
+use strict;
+use warnings;
+use JSON;
+
+use PVE::JSONSchema qw(get_standard_option);
+
+use base qw(PVE::CLIHandler);
+
+__PACKAGE__->register_method ({
+    name => 'list',
+    path => 'list',
+    method => 'GET',
+    description => "List containers.",
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    remote => get_standard_option('pveclient-remote-name'),
+	    format => {
+		type => 'string',
+		description => 'Output format',
+		enum => [ 'table', 'json' ],
+		optional => 1,
+		default => 'table',
+	    }
+	},
+    },
+    returns => { type => 'null'},
+    code => sub {
+	my ($param) = @_;
+
+	my $config = PVE::APIClient::Config->load();
+	my $conn = PVE::APIClient::Config->remote_conn($config, $param->{remote});
+	my $resources = $conn->get('api2/json/cluster/resources', { type => 'vm' });
+
+	if (!defined($param->{format}) or $param->{format} eq 'table') {
+	    my $headers = ['Node', 'VMID', 'Type', 'Name', 'Status'];
+	    my $data = [];
+	    for my $el (@$resources) {
+		push(@$data, [$el->{node}, $el->{vmid}, $el->{type}, $el->{name}, $el->{status}]);
+	    }
+
+	    printf("%10s %10s %10s %10s %10s\n", @$headers);
+	    for my $row (@$data) {
+		printf("%10s %10s %10s %10s %10s\n", @$row);
+	    }
+	} else {
+	    print JSON::to_json($resources, {utf8 => 1, pretty => 1});
+	}
+
+	return undef;
+    }});
+
+
+our $cmddef = [ __PACKAGE__, 'list', ['remote']];
+
+1;
\ No newline at end of file
diff --git a/PVE/APIClient/Commands/lxc.pm b/PVE/APIClient/Commands/lxc.pm
index c60017b..9e163f7 100644
--- a/PVE/APIClient/Commands/lxc.pm
+++ b/PVE/APIClient/Commands/lxc.pm
@@ -390,29 +390,8 @@ __PACKAGE__->register_method ({
 	return undef;
     }});
 
-__PACKAGE__->register_method ({
-    name => 'list',
-    path => 'list',
-    method => 'GET',
-    description => "List containers.",
-    parameters => {
-	additionalProperties => 0,
-	properties => {
-	    remote => get_standard_option('pveclient-remote-name'),
-	},
-    },
-    returns => { type => 'null'},
-    code => sub {
-	my ($param) = @_;
-
-	die "implement me";
-
-    }});
-
-
 our $cmddef = {
     enter => [ __PACKAGE__, 'enter', ['remote', 'vmid']],
-    list => [ __PACKAGE__, 'list', ['remote']],
 };
 
 1;
diff --git a/pveclient b/pveclient
index 46d902d..6a06a88 100755
--- a/pveclient
+++ b/pveclient
@@ -13,6 +13,7 @@ use PVE::CLIHandler;
 use PVE::APIClient::LWP;
 use PVE::APIClient::Helpers;
 use PVE::APIClient::Commands::remote;
+use PVE::APIClient::Commands::list;
 use PVE::APIClient::Commands::lxc;
 use PVE::APIClient::Commands::help;
 
@@ -31,6 +32,7 @@ sub call_method {
 }
 
 my $cli_class_handlers = {
+    list => 'PVE::APIClient::Commands::list',
     lxc => 'PVE::APIClient::Commands::lxc',
     remote => 'PVE::APIClient::Commands::remote',
     help => 'PVE::APIClient::Commands::help',
-- 
2.11.0




More information about the pve-devel mailing list