public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: "Max R. Carrara" <m.carrara@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [RFC pve-storage master v1 01/12] plugin: meta: add package PVE::Storage::Plugin::Meta
Date: Mon,  8 Sep 2025 20:00:45 +0200	[thread overview]
Message-ID: <20250908180058.530119-2-m.carrara@proxmox.com> (raw)
In-Reply-To: <20250908180058.530119-1-m.carrara@proxmox.com>

This package is used to retrieve general metadata about plugins.

Add this package in order to keep code concerning the retrieval of
storage plugin metadata in one place instead of mixing the code into
`PVE::Storage` and `PVE::Storage::Plugin`.

At the moment, plugin metadata includes the plugin's kind (inbuilt or
custom), its supported content types and formats, and what properties
it declares as sensitive.

Since plugin metadata (such as the returned hash by the `plugindata()`
method, for example) is static, cache the metadata of all plugins
after the first call to either `get_plugin_metadata()` or
`get_plugin_metadata_all()`.

The public subroutines (deep-)copy their returned data to prevent any
accidental modification, as hashrefs aren't supported by the
'use constant' Perl core pragma. This isn't the most optimal way to
do this; as a potential alternative `Readonly` [0] could be used
instead, but I didn't want to pull in another dependency at the
moment.

[0]: https://metacpan.org/pod/Readonly

Signed-off-by: Max R. Carrara <m.carrara@proxmox.com>
---
 src/PVE/Storage/Makefile        |   1 +
 src/PVE/Storage/Plugin/Makefile |  10 ++
 src/PVE/Storage/Plugin/Meta.pm  | 168 ++++++++++++++++++++++++++++++++
 3 files changed, 179 insertions(+)
 create mode 100644 src/PVE/Storage/Plugin/Makefile
 create mode 100644 src/PVE/Storage/Plugin/Meta.pm

diff --git a/src/PVE/Storage/Makefile b/src/PVE/Storage/Makefile
index a67dc25..ca687b6 100644
--- a/src/PVE/Storage/Makefile
+++ b/src/PVE/Storage/Makefile
@@ -19,5 +19,6 @@ SOURCES= \
 .PHONY: install
 install:
 	make -C Common install
+	make -C Plugin install
 	for i in ${SOURCES}; do install -D -m 0644 $$i ${DESTDIR}${PERLDIR}/PVE/Storage/$$i; done
 	make -C LunCmd install
diff --git a/src/PVE/Storage/Plugin/Makefile b/src/PVE/Storage/Plugin/Makefile
new file mode 100644
index 0000000..ca82517
--- /dev/null
+++ b/src/PVE/Storage/Plugin/Makefile
@@ -0,0 +1,10 @@
+SOURCES = Meta.pm		\
+
+
+INSTALL_PATH = ${DESTDIR}${PERLDIR}/PVE/Storage/Plugin
+
+.PHONY: install
+install:
+	set -e && for SOURCE in ${SOURCES}; \
+		do install -D -m 0644 $$SOURCE ${INSTALL_PATH}/$$SOURCE; \
+	done
diff --git a/src/PVE/Storage/Plugin/Meta.pm b/src/PVE/Storage/Plugin/Meta.pm
new file mode 100644
index 0000000..6d0cb51
--- /dev/null
+++ b/src/PVE/Storage/Plugin/Meta.pm
@@ -0,0 +1,168 @@
+package PVE::Storage::Plugin::Meta;
+
+use v5.36;
+
+use Carp qw(croak confess);
+use Storable qw(dclone);
+
+use PVE::Storage;
+use PVE::Storage::Plugin;
+
+use Exporter qw(import);
+
+our @EXPORT_OK = qw(
+    plugin_kinds
+    plugin_content_types
+    plugin_formats
+    get_plugin_metadata
+    get_plugin_metadata_all
+);
+
+=head1 NAME
+
+PVE::Storage::Plugin::Meta - Retrieving Storage Plugin Metadata
+
+=head1 DESCRIPTION
+
+=for comment
+TODO
+
+=cut
+
+my $PLUGIN_KINDS = [
+    'builtin', 'custom',
+];
+
+# Note: 'none' isn't included here since it's an internal marker content type.
+my $PLUGIN_CONTENT_TYPES = [
+    'images', 'rootdir', 'vztmpl', 'iso', 'backup', 'snippets', 'import',
+];
+
+my $PLUGIN_FORMATS = [
+    'raw', 'qcow2', 'vmdk', 'subvol',
+];
+
+my $DEFAULT_PLUGIN_FORMAT = 'raw';
+
+sub plugin_kinds() {
+    return [$PLUGIN_KINDS->@*];
+}
+
+sub plugin_content_types() {
+    return [$PLUGIN_CONTENT_TYPES->@*];
+}
+
+sub plugin_formats() {
+    return [$PLUGIN_FORMATS->@*];
+}
+
+my $plugin_metadata = undef;
+
+my sub assemble_plugin_metadata_content($plugin) {
+    confess '$plugin is undef' if !defined($plugin);
+
+    my $content_metadata = {
+        supported => [],
+        default => [],
+    };
+
+    my $plugindata = $plugin->plugindata();
+
+    return $content_metadata if !defined($plugindata->{content});
+
+    my $supported = $plugindata->{content}->[0];
+    my $default = $plugindata->{content}->[1];
+
+    for my $content_type ($PLUGIN_CONTENT_TYPES->@*) {
+        if (defined($supported->{$content_type})) {
+            push($content_metadata->{supported}->@*, $content_type);
+        }
+
+        if (defined($default->{$content_type})) {
+            push($content_metadata->{default}->@*, $content_type);
+        }
+    }
+
+    return $content_metadata;
+}
+
+my sub assemble_plugin_metadata_format($plugin) {
+    confess '$plugin is undef' if !defined($plugin);
+
+    my $plugindata = $plugin->plugindata();
+
+    if (!defined($plugindata->{format})) {
+        return {
+            supported => [$DEFAULT_PLUGIN_FORMAT],
+            default => $DEFAULT_PLUGIN_FORMAT,
+        };
+    }
+
+    my $format_metadata = {
+        supported => [],
+        default => $plugindata->{format}->[1],
+    };
+
+    my $supported = $plugindata->{format}->[0];
+
+    for my $format ($PLUGIN_FORMATS->@*) {
+        if (defined($supported->{$format})) {
+            push($format_metadata->{supported}->@*, $format);
+        }
+    }
+
+    return $format_metadata;
+}
+
+my sub assemble_plugin_metadata() {
+    return if defined($plugin_metadata);
+
+    $plugin_metadata = {};
+    my $all_types = PVE::Storage::Plugin->lookup_types();
+
+    for my $type ($all_types->@*) {
+        my $plugin = PVE::Storage::Plugin->lookup($type);
+
+        $plugin = "$plugin";
+
+        my $kind = 'builtin';
+        $kind = 'custom' if $plugin =~ m/^PVE::Storage::Custom::/;
+
+        my $metadata = {
+            type => $type,
+            module => $plugin,
+            kind => $kind,
+        };
+
+        $metadata->{content} = assemble_plugin_metadata_content($plugin);
+        $metadata->{format} = assemble_plugin_metadata_format($plugin);
+
+        my $sensitive_properties = $plugin->plugindata()->{'sensitive-properties'} // {};
+
+        $metadata->{'sensitive-properties'} =
+            [grep { $sensitive_properties->{$_} } sort keys $sensitive_properties->%*];
+
+        $plugin_metadata->{$type} = $metadata;
+    }
+
+    return;
+}
+
+sub get_plugin_metadata {
+    my ($plugin_type) = @_;
+
+    croak "\$plugin_type is undef" if !defined($plugin_type);
+
+    assemble_plugin_metadata() if !defined($plugin_metadata);
+
+    return dclone($plugin_metadata->{$plugin_type}) if exists($plugin_metadata->{$plugin_type});
+    return undef;
+}
+
+sub get_plugin_metadata_all {
+    assemble_plugin_metadata() if !defined($plugin_metadata);
+
+    return dclone($plugin_metadata);
+}
+
+1;
-- 
2.47.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


  reply	other threads:[~2025-09-08 18:01 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-08 18:00 [pve-devel] [RFC pve-storage, pve-manager master v1 00/12] GUI Support for Custom Storage Plugins Max R. Carrara
2025-09-08 18:00 ` Max R. Carrara [this message]
2025-09-08 18:00 ` [pve-devel] [RFC pve-storage master v1 02/12] api: Add 'plugins/storage' and 'plugins/storage/{plugin}' paths Max R. Carrara
2025-09-08 18:00 ` [pve-devel] [RFC pve-storage master v1 03/12] plugin: meta: introduce 'short-name' Max R. Carrara
2025-09-08 18:00 ` [pve-devel] [RFC pve-storage master v1 04/12] plugin: views: add package PVE::Storage::Plugin::Views Max R. Carrara
2025-09-08 18:00 ` [pve-devel] [RFC pve-storage master v1 05/12] plugin: add new plugin API method `get_form_view()` Max R. Carrara
2025-09-08 18:00 ` [pve-devel] [RFC pve-storage master v1 06/12] plugin: meta: add metadata regarding views in API Max R. Carrara
2025-09-08 18:00 ` [pve-devel] [RFC pve-storage master v1 07/12] api: views: add paths regarding storage plugin views Max R. Carrara
2025-09-08 18:00 ` [pve-devel] [RFC pve-storage master v1 08/12] plugin: zfspool: add 'short-name' and form view for ZFS pool plugin Max R. Carrara
2025-09-08 18:00 ` [pve-devel] [RFC pve-manager master v1 09/12] api: handle path 'plugins/storage' through its package Max R. Carrara
2025-09-08 18:00 ` [pve-devel] [RFC pve-manager master v1 10/12] ui: storage: add CustomBase.js Max R. Carrara
2025-09-08 18:00 ` [pve-devel] [RFC pve-manager master v1 11/12] ui: storage: support custom storage plugins in Datacenter > Storage Max R. Carrara
2025-09-08 18:00 ` [pve-devel] [RFC pve-manager master v1 12/12] ui: storage: use `Ext.Msg.alert()` instead of throwing an exception Max R. Carrara
2025-09-08 19:23 ` [pve-devel] [RFC pve-storage, pve-manager master v1 00/12] GUI Support for Custom Storage Plugins Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250908180058.530119-2-m.carrara@proxmox.com \
    --to=m.carrara@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal