From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <a.lauterer@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 16C3790E50
 for <pve-devel@lists.proxmox.com>; Mon, 19 Dec 2022 15:46:38 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id F162F29462
 for <pve-devel@lists.proxmox.com>; Mon, 19 Dec 2022 15:46:37 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pve-devel@lists.proxmox.com>; Mon, 19 Dec 2022 15:46:35 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id BC5AF44D09
 for <pve-devel@lists.proxmox.com>; Mon, 19 Dec 2022 15:46:35 +0100 (CET)
From: Aaron Lauterer <a.lauterer@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Mon, 19 Dec 2022 15:46:34 +0100
Message-Id: <20221219144634.2413269-1-a.lauterer@proxmox.com>
X-Mailer: git-send-email 2.30.2
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.042 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pve-devel] [RFC manager] pveceph: add osd details command
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Mon, 19 Dec 2022 14:46:38 -0000

To provide similar output on the CLI as is possible in the GUI/API
regaring OSD details.

By default (output-format=text) a more concise output is shown. Using
json or yaml as output format will print all the available data.

The 'verbose' flag causes json-pretty output to be used.

The functionality is split between the actual function and the output
formatter as not all options/parameters are available in each.

Signed-off-by: Aaron Lauterer <a.lauterer@proxmox.com>
---
Depends on the OSD Details patches which are not yet applied [0].

RFC because in order to check against the output format and the verbose
flag, the code ended up split between the actual cmdline method and
the formatting function.

The verbose flag is only available in the formatter and the verbose in
the method itself.

If there would be a nicer way to achieve a similar result, I would be
happy to get hints.

[0] https://lists.proxmox.com/pipermail/pve-devel/2022-December/055154.html

 PVE/CLI/pveceph.pm | 66 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)

diff --git a/PVE/CLI/pveceph.pm b/PVE/CLI/pveceph.pm
index afcc67e0..e08cc344 100755
--- a/PVE/CLI/pveceph.pm
+++ b/PVE/CLI/pveceph.pm
@@ -352,6 +352,71 @@ __PACKAGE__->register_method ({
 	return $rpcenv->fork_worker('cephdestroyfs', $fs_name,  $user, $worker);
     }});
 
+__PACKAGE__->register_method ({
+    name => 'osddetails',
+    path => 'osddetails',
+    method => 'GET',
+    description => "Get OSD details.",
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    node => get_standard_option('pve-node'),
+	    osdid => {
+		description => "ID of the OSD",
+		type => 'string',
+	    },
+	    verbose => {
+		description => "Print verbose information, same as json-pretty output format.",
+		type => 'boolean',
+		default => 0,
+		optional => 1,
+	    },
+	},
+    },
+    returns => { type => 'object' },
+    code => sub {
+	my ($param) = @_;
+	PVE::Ceph::Tools::check_ceph_inited();
+	my $res = PVE::API2::Ceph::OSD->osddetails({
+		osdid => $param->{osdid},
+		node => $param->{node},
+	    });
+
+	for my $dev (@{ $res->{devices} }) {
+	    $dev->{"lv-info"} = PVE::API2::Ceph::OSD->osdvolume({
+		    osdid => $param->{osdid},
+		    node => $param->{node},
+		    type => $dev->{device},
+		});
+	}
+	$res->{verbose} = 1 if $param->{verbose};
+	return $res;
+    }});
+
+my $format_osddetails = sub {
+    my ($data, $schema, $options) = @_;
+    $options->{"output-format"} //= "text";
+
+    if ($data->{verbose}) {
+	$options->{"output-format"} = "json-pretty";
+	delete $data->{verbose};
+    }
+
+    if ($options->{"output-format"} eq "text") {
+	for my $dev (@{ $data->{devices} }) {
+	    my $str = "Disk: $dev->{physical_device},"
+		." Type: $dev->{type},"
+		." LV Size: $dev->{'lv-info'}->{lv_size},"
+		." LV Creation Time: $dev->{'lv-info'}->{creation_time}";
+
+	    $data->{osd}->{$dev->{device}} = $str;
+	}
+	PVE::CLIFormatter::print_api_result($data->{osd}, $schema, undef, $options);
+    } else {
+	PVE::CLIFormatter::print_api_result($data, $schema, undef, $options);
+    }
+};
+
 our $cmddef = {
     init => [ 'PVE::API2::Ceph', 'init', [], { node => $nodename } ],
     pool => {
@@ -392,6 +457,7 @@ our $cmddef = {
     osd => {
 	create => [ 'PVE::API2::Ceph::OSD', 'createosd', ['dev'], { node => $nodename }, $upid_exit],
 	destroy => [ 'PVE::API2::Ceph::OSD', 'destroyosd', ['osdid'], { node => $nodename }, $upid_exit],
+	details => [ __PACKAGE__, 'osddetails', ['osdid'], { node => $nodename }, $format_osddetails, $PVE::RESTHandler::standard_output_options],
     },
     createosd => { alias => 'osd create' },
     destroyosd => { alias => 'osd destroy' },
-- 
2.30.2