Add a simple implementation of list
authorRené Jochum <r.jochum@proxmox.com>
Mon, 11 Jun 2018 09:35:27 +0000 (11:35 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Tue, 12 Jun 2018 04:39:35 +0000 (06:39 +0200)
Signed-off-by: René Jochum <r.jochum@proxmox.com>
PVE/APIClient/Commands/help.pm
PVE/APIClient/Commands/list.pm [new file with mode: 0644]
PVE/APIClient/Commands/lxc.pm
pveclient

index 407af5e..81d4d4a 100644 (file)
@@ -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 (file)
index 0000000..aed0a54
--- /dev/null
@@ -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
index b30e09f..66ad704 100644 (file)
@@ -419,29 +419,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;
index 46d902d..6a06a88 100755 (executable)
--- 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',