[pve-devel] [pve-manager 2/5] pvesh: implement 'ls' command

Dietmar Maurer dietmar at proxmox.com
Thu Jul 26 11:04:02 CEST 2018


Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
 bin/pvesh | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 94 insertions(+)

diff --git a/bin/pvesh b/bin/pvesh
index ae5a62c2..190d7158 100755
--- a/bin/pvesh
+++ b/bin/pvesh
@@ -164,6 +164,45 @@ sub dir_info {
     return $res;
 }
 
+sub resource_cap {
+    my ($path) = @_;
+
+    my $res = '';
+
+    my ($handler, $info) = PVE::API2->find_handler('GET', $path);
+    if (!($handler && $info)) {
+	$res .= '--';
+    } else {
+	if (PVE::JSONSchema::method_get_child_link($info)) {
+	    $res .= 'Dr';
+	} else {
+	    $res .= '-r';
+	}
+    }
+
+    ($handler, $info) = PVE::API2->find_handler('PUT', $path);
+    if (!($handler && $info)) {
+	$res .= '-';
+    } else {
+	$res .= 'w';
+    }
+
+    ($handler, $info) = PVE::API2->find_handler('POST', $path);
+    if (!($handler && $info)) {
+	$res .= '-';
+    } else {
+	$res .= 'c';
+    }
+
+    ($handler, $info) = PVE::API2->find_handler('DELETE', $path);
+    if (!($handler && $info)) {
+	$res .= '-';
+    } else {
+	$res .= 'd';
+    }
+
+    return $res;
+}
 
 # dynamically update schema definition
 # like: pvesh <get|set|create|delete|help> <path>
@@ -263,6 +302,60 @@ sub call_api_method {
 }
 
 __PACKAGE__->register_method ({
+    name => 'ls',
+    path => 'ls',
+    method => 'GET',
+    description => "List child objects on <api_path>.",
+    parameters => {
+	additionalProperties => 0,
+	properties => PVE::RESTHandler::add_standard_output_properties($path_properties),
+    },
+    returns => { type => 'null' },
+    code => sub {
+	my ($param) = @_;
+
+	my $path = PVE::Tools::extract_param($param, 'api_path');
+
+	my $stdopts =  PVE::RESTHandler::extract_standard_output_properties($param);
+	PVE::CLIFormatter::query_terminal_options($stdopts);
+
+	my $uri_param = {};
+	my ($handler, $info) = PVE::API2->find_handler('GET', $path, $uri_param);
+	if (!$handler || !$info) {
+	    die "no such resource '$path'\n";
+	}
+
+	my $link = PVE::JSONSchema::method_get_child_link($info);
+	die "resource '$path' does not define child links\n" if !$link;
+
+	my $res;
+
+	my ($node, $remip) = check_proxyto($info, $uri_param);
+	if ($node) {
+	    $res = proxy_handler($node, $remip, $path, 'ls', $param);
+	} else {
+	    foreach my $p (keys %$uri_param) {
+		$param->{$p} = $uri_param->{$p};
+	    }
+
+	    my $data = $handler->handle($info, $param);
+
+	    my $children = extract_children($link, $data);
+
+	    $res = [];
+	    foreach my $c (@$children) {
+		my $item = { name => $c, capabilities => resource_cap("$path/$c")};
+		push @$res, $item;
+	    }
+	}
+
+	my $schema = { type => 'array', items => { type => 'object' }};
+	PVE::CLIFormatter::print_api_result($res, $schema, ['name', 'capabilities'], $stdopts);
+
+	return undef;
+    }});
+
+__PACKAGE__->register_method ({
     name => 'get',
     path => 'get',
     method => 'GET',
@@ -404,6 +497,7 @@ __PACKAGE__->register_method ({
 our $cmddef = {
     usage => [ __PACKAGE__, 'usage', ['api_path']],
     get => [ __PACKAGE__, 'get', ['api_path']],
+    ls => [ __PACKAGE__, 'ls', ['api_path']],
     set => [ __PACKAGE__, 'set', ['api_path']],
     create => [ __PACKAGE__, 'create', ['api_path']],
     delete => [ __PACKAGE__, 'delete', ['api_path']],
-- 
2.11.0




More information about the pve-devel mailing list