[pve-devel] [pve-manager] pvesh: implement 'ls' command
Dietmar Maurer
dietmar at proxmox.com
Wed Jul 25 11:38:44 CEST 2018
Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
As requested by Thomas.
NOTE: needs previous patches for pve-common which are not applied so far
bin/pvesh | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 93 insertions(+)
diff --git a/bin/pvesh b/bin/pvesh
index 303fe0ef..ea582d8b 100755
--- a/bin/pvesh
+++ b/bin/pvesh
@@ -165,6 +165,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>
@@ -261,6 +300,59 @@ 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 $data;
+ my ($node, $remip) = check_proxyto($info, $uri_param);
+ if ($node) {
+ $data = proxy_handler($node, $remip, $path, 'ls', $param, $stdopts->{quiet});
+ } else {
+ foreach my $p (keys %$uri_param) {
+ $param->{$p} = $uri_param->{$p};
+ }
+
+ $data = $handler->handle($info, $param);
+ }
+
+ my $children = extract_children($link, $data);
+
+ my $res = [];
+ foreach my $c (@$children) {
+ my $item = { name => $c, cap => resource_cap("$path/$c")};
+ push @$res, $item;
+ }
+
+ my $schema = { type => 'array', items => { type => 'object' }};
+ PVE::CLIFormatter::print_api_result($res, $schema, ['name', 'cap'], $stdopts);
+
+ return undef;
+ }});
+
+__PACKAGE__->register_method ({
name => 'get',
path => 'get',
method => 'GET',
@@ -402,6 +494,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