[pve-devel] [PATCH pve-client v3] Add "storage status" and "storage list"
Dietmar Maurer
dietmar at proxmox.com
Thu Jun 21 12:27:36 CEST 2018
comments inline
> On June 21, 2018 at 10:17 AM René Jochum <r.jochum at proxmox.com> wrote:
>
>
> Signed-off-by: René Jochum <r.jochum at proxmox.com>
> ---
> PVE/APIClient/Commands/storage.pm | 155
> ++++++++++++++++++++++++++++++++++++++
> pveclient | 2 +
> 2 files changed, 157 insertions(+)
> create mode 100644 PVE/APIClient/Commands/storage.pm
>
> diff --git a/PVE/APIClient/Commands/storage.pm
> b/PVE/APIClient/Commands/storage.pm
> new file mode 100644
> index 0000000..1e914f5
> --- /dev/null
> +++ b/PVE/APIClient/Commands/storage.pm
> @@ -0,0 +1,155 @@
> +package PVE::APIClient::Commands::storage;
> +
> +use strict;
> +use warnings;
> +use JSON;
> +
> +use PVE::APIClient::JSONSchema qw(get_standard_option);
> +
> +use PVE::APIClient::Config;
> +use PVE::APIClient::CLIHandler;
> +
> +use base qw(PVE::APIClient::CLIHandler);
> +
> +my $print_content = sub {
> + my ($list) = @_;
> +
> + my $maxlenname = 0;
> + foreach my $info (@$list) {
> +
> + my $volid = $info->{volid};
> + my $sidlen = length ($volid);
> + $maxlenname = $sidlen if $sidlen > $maxlenname;
> + }
> +
> + foreach my $info (@$list) {
> + next if !$info->{vmid};
> + my $volid = $info->{volid};
> +
> + printf "%-${maxlenname}s %5s %10d %d\n", $volid,
> + $info->{format}, $info->{size}, $info->{vmid};
> + }
> +
> + foreach my $info (sort { $a->{format} cmp $b->{format} } @$list) {
> + next if $info->{vmid};
> + my $volid = $info->{volid};
> +
> + printf "%-${maxlenname}s %5s %10d\n", $volid,
> + $info->{format}, $info->{size};
> + }
> +};
> +
> +my $print_status = sub {
> + my $res = shift;
> +
> + my $maxlen = 0;
> + foreach my $res (@$res) {
> + my $storeid = $res->{storage};
> + $maxlen = length ($storeid) if length ($storeid) > $maxlen;
> + }
> + $maxlen+=1;
> +
> + printf "%-${maxlen}s %10s %10s %15s %15s %15s %8s\n", 'Name', 'Type',
> + 'Status', 'Total', 'Used', 'Available', '%';
> +
> + foreach my $res (sort { $a->{storage} cmp $b->{storage} } @$res) {
> + my $storeid = $res->{storage};
> +
> + my $active = $res->{active} ? 'active' : 'inactive';
> + my ($per, $per_fmt) = (0, '% 7.2f%%');
> + $per = ($res->{used}*100)/$res->{total} if $res->{total} > 0;
> +
> + if (!$res->{enabled}) {
> + $per = 'N/A';
> + $per_fmt = '% 8s';
> + $active = 'disabled';
> + }
> +
> + printf "%-${maxlen}s %10s %10s %15d %15d %15d $per_fmt\n", $storeid,
> + $res->{type}, $active, $res->{total}/1024, $res->{used}/1024,
> + $res->{avail}/1024, $per;
> + }
> +};
Can't we use the new print_api_list() code from Stoiko? If not, can we modify
it
so that we can reuse it?
> +
> +__PACKAGE__->register_method ({
> + name => 'list',
> + path => 'list',
> + method => 'GET',
> + description => "Get status for all datastores.",
> + parameters => {
> + additionalProperties => 0,
> + properties => {
> + remote => get_standard_option('pveclient-remote-name'),
> + storage => get_standard_option('pve-storage-id'),
> + format => get_standard_option('pveclient-output-format'),
> + node => {
> + description => "The cluster node name.",
> + type => 'string', format => 'pve-node',
> + optional => 1,
> + },
Please use get_standard_option(...)
> + },
> + },
> + returns => { type => 'null'},
> + code => sub {
> + my ($param) = @_;
> +
> + my $config = PVE::APIClient::Config->load();
> + my $conn = PVE::APIClient::Config->remote_conn($config, $param->{remote});
> +
> + my $node = $param->{node} // 'localhost';
> + my $storage = $param->{storage};
> +
> + my $resources = $conn->get("api2/json/nodes/$node/storage/$storage/content",
> {});
> +
> + if (!defined($param->{format}) or $param->{format} eq 'text') {
> + $print_content->($resources);
> + } else {
> + print JSON::to_json($resources, {utf8 => 1, pretty => 1});
> + }
> +
> + return undef;
> + }});
> +
> +__PACKAGE__->register_method ({
> + name => 'status',
> + path => 'status',
> + method => 'GET',
> + description => "List storage content.",
> + parameters => {
> + additionalProperties => 0,
> + properties => {
> + remote => get_standard_option('pveclient-remote-name'),
> + format => get_standard_option('pveclient-output-format'),
> + node => {
> + description => "The cluster node name.",
> + type => 'string', format => 'pve-node',
> + optional => 1,
> + },
> + },
> + },
> + returns => { type => 'null'},
> + code => sub {
> + my ($param) = @_;
> +
> + my $config = PVE::APIClient::Config->load();
> + my $conn = PVE::APIClient::Config->remote_conn($config, $param->{remote});
> +
> + my $node = $param->{node} // 'localhost';
> +
> + my $resources = $conn->get("api2/json/nodes/$node/storage", {});
> +
> + if (!defined($param->{format}) or $param->{format} eq 'text') {
> + $print_status->($resources);
> + } else {
> + print JSON::to_json($resources, {utf8 => 1, pretty => 1});
> + }
> +
> + return undef;
> + }});
> +
> +our $cmddef = {
> + list => [ __PACKAGE__, 'list', ['remote', 'storage']],
> + status => [ __PACKAGE__, 'status', ['remote']],
> +};
> +
> +1;
> diff --git a/pveclient b/pveclient
> index 2995fd5..b905526 100755
> --- a/pveclient
> +++ b/pveclient
> @@ -16,6 +16,7 @@ use PVE::APIClient::Helpers;
> use PVE::APIClient::Config;
> use PVE::APIClient::Commands::config;
> use PVE::APIClient::Commands::remote;
> +use PVE::APIClient::Commands::storage;
> use PVE::APIClient::Commands::list;
> use PVE::APIClient::Commands::lxc;
> use PVE::APIClient::Commands::GuestStatus;
> @@ -181,6 +182,7 @@ our $cmddef = {
> list => $PVE::APIClient::Commands::list::cmddef,
> lxc => $PVE::APIClient::Commands::lxc::cmddef,
> remote => $PVE::APIClient::Commands::remote::cmddef,
> + storage => $PVE::APIClient::Commands::storage::cmddef,
>
> resume => [ 'PVE::APIClient::Commands::GuestStatus', 'resume', ['remote',
> 'vmid']],
> shutdown => [ 'PVE::APIClient::Commands::GuestStatus', 'shutdown',
> ['remote', 'vmid']],
> --
> 2.11.0
>
> _______________________________________________
> pve-devel mailing list
> pve-devel at pve.proxmox.com
> https://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel
More information about the pve-devel
mailing list