[pve-devel] [PATCH pve-client] Add 'lxc list'

René Jochum r.jochum at proxmox.com
Fri Jun 8 09:44:57 CEST 2018


---
 PVE/APIClient/Commands/lxc.pm | 82 ++++++++++++++++++++++++++++++++++++++++++-
 PVE/APIClient/Helpers.pm      | 14 ++++++++
 2 files changed, 95 insertions(+), 1 deletion(-)

diff --git a/PVE/APIClient/Commands/lxc.pm b/PVE/APIClient/Commands/lxc.pm
index f0c85f7..0f6f638 100644
--- a/PVE/APIClient/Commands/lxc.pm
+++ b/PVE/APIClient/Commands/lxc.pm
@@ -18,6 +18,7 @@ use PVE::PTY;
 
 use base qw(PVE::CLIHandler);
 use PVE::APIClient::Config;
+use PVE::APIClient::Helpers;
 
 my $CRLF = "\x0D\x0A";
 my $max_payload_size = 128*1024;
@@ -356,14 +357,93 @@ __PACKAGE__->register_method ({
 	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) = @_;
 
-	die "implement me";
+	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' });
+	my $lxcs = [grep({$_->{type} eq "lxc"} @$resources)];
+
+	my $glxcs = [];
+	for my $el (@$lxcs) {
+	    my $node = $el->{node};
+	    my $vmid = $el->{vmid};
+
+	    my $lxc_config = $conn->get("api2/json/nodes/$node/lxc/$vmid/config", {});
+
+	    for my $i (0 .. 9) {
+		if (exists($lxc_config->{"net" . $i})) {
+		    $lxc_config->{"net" . $i . "_raw"} = $lxc_config->{"net" . $i};
+		    $lxc_config->{"net" . $i} =
+			PVE::APIClient::Helpers::parse_key_value_string($lxc_config->{"net" . $i});
+		}
+	    }
+
+	    $lxc_config->{"rootfs_raw"} = $lxc_config->{"rootfs"};
+	    $lxc_config->{"rootfs"} = PVE::APIClient::Helpers::parse_key_value_string($lxc_config->{"rootfs"});
+
+	    $lxc_config->{node} = $node;
+	    $lxc_config->{vmid} = $vmid;
+
+	    $lxc_config->{status} = $el->{status};
+
+	    push @$glxcs, $lxc_config;
+	}
+
+	if (!defined($param->{format})) {
+	    my $headers = ['Node', 'VMID', 'Hostname', 'Rootfs', 'Memory', 'Status', 'IPv4', 'IPv6'];
 
+	    my $data = [];
+	    for my $el (@$glxcs) {
+		my $net4 = "";
+		my $net6 = "";
+
+		for my $i (0..9) {
+		    if (exists($el->{"net" . $i})) {
+			if (exists($el->{"net" . $i}->{ip})) {
+			    $net4 .= "\n" if (length($net4) > 0);
+			    $net4 .= $el->{"net" . $i}->{ip} . " (" . $el->{"net" . $i}->{name} . ")";
+			}
+
+			if (exists($el->{"net" . $i}->{ip6})) {
+			    $net6 .= "\n" if (length($net6) > 0);
+			    $net6 .= $el->{"net" . $i}->{ip6} . " (" . $el->{"net" . $i}->{name} . ")";
+			}
+		    }
+		}
+
+		push(@$data, [
+		    $el->{node},
+		    $el->{vmid},
+		    $el->{hostname},
+		    $el->{rootfs}->{size},
+		    $el->{memory},
+		    $el->{status},
+		    $net4,
+		    $net6
+		])
+	    }
+
+	    printf("%10s %10s %10s %10s %10s %10s %30s %30s\n", @$headers);
+	    for my $row (@$data) {
+		printf("%10s %10s %10s %10s %10s %10s %30s %30s\n", @$row);
+	    }
+	} else {
+	    print JSON::to_json($glxcs, {utf8 => 1, pretty => 1});
+	}
+
+	return undef;
     }});
 
 
diff --git a/PVE/APIClient/Helpers.pm b/PVE/APIClient/Helpers.pm
index e7f2216..6e9643e 100644
--- a/PVE/APIClient/Helpers.pm
+++ b/PVE/APIClient/Helpers.pm
@@ -66,4 +66,18 @@ sub lookup_api_method {
     return $data;
 }
 
+sub parse_key_value_string {
+    my $data = shift;
+
+    my @pairs = split( /,/, $data );
+
+    my $res = {};
+    for my $pair (@pairs) {
+        my ( $key, $value ) = split( /=/, $pair );
+        $res->{$key} = $value;
+    }
+
+    return $res;
+}
+
 1;
-- 
2.11.0




More information about the pve-devel mailing list