all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Mira Limbeck <m.limbeck@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH v2 storage 10/15] api: add mapping support
Date: Thu, 30 Apr 2026 19:27:08 +0200	[thread overview]
Message-ID: <20260430173220.441001-11-m.limbeck@proxmox.com> (raw)
In-Reply-To: <20260430173220.441001-1-m.limbeck@proxmox.com>

introduces the necessary API to create, update and delete storage
mappings

Signed-off-by: Mira Limbeck <m.limbeck@proxmox.com>
---
v2:
 - now generic over mapping plugin

 src/PVE/API2/Storage/Makefile     |   2 +-
 src/PVE/API2/Storage/Mapping.pm   | 213 ++++++++++++++++++++++++++++++
 src/PVE/Storage/Mapping/Plugin.pm |  14 ++
 3 files changed, 228 insertions(+), 1 deletion(-)
 create mode 100644 src/PVE/API2/Storage/Mapping.pm

diff --git a/src/PVE/API2/Storage/Makefile b/src/PVE/API2/Storage/Makefile
index 1705080..3792cec 100644
--- a/src/PVE/API2/Storage/Makefile
+++ b/src/PVE/API2/Storage/Makefile
@@ -1,5 +1,5 @@
 
-SOURCES= Content.pm Status.pm Config.pm PruneBackups.pm Scan.pm FileRestore.pm
+SOURCES= Content.pm Status.pm Config.pm PruneBackups.pm Scan.pm FileRestore.pm Mapping.pm
 
 .PHONY: install
 install:
diff --git a/src/PVE/API2/Storage/Mapping.pm b/src/PVE/API2/Storage/Mapping.pm
new file mode 100644
index 0000000..91483cd
--- /dev/null
+++ b/src/PVE/API2/Storage/Mapping.pm
@@ -0,0 +1,213 @@
+package PVE::API2::Storage::Mapping;
+
+use strict;
+use warnings;
+
+use PVE::RESTHandler;
+use PVE::Tools qw(extract_param);
+
+use base qw(PVE::RESTHandler);
+
+my $storage_mapping_type_enum = PVE::Storage::Mapping::Plugin->lookup_types();
+
+__PACKAGE__->register_method({
+    name => 'index',
+    path => '',
+    method => 'GET',
+    description => 'List all storage mappings.',
+    permissions => {
+        user => 'all',
+    },
+    parameters => {
+        additionalProperties => 0,
+        properties => {
+            type => {
+                description => "List only storage of specific type.",
+                type => 'string',
+                enum => $storage_mapping_type_enum,
+                optional => 1,
+            },
+        },
+    },
+    returns => {
+        type => 'array',
+        items => {
+            type => 'object',
+            properties => {
+                id => {
+                    type => 'string',
+                    format => 'pve-storage-id',
+                },
+                type => {
+                    type => 'string',
+                },
+                map => {
+                    type => 'string',
+                },
+            },
+        },
+    },
+    code => sub {
+        my ($param) = @_;
+
+        my $rpcenv = PVE::RPCEnvironment::get();
+        my $authuser = $rpcenv->get_user();
+
+        my $cfg = PVE::Storage::Mapping::Plugin::config();
+
+        my $mapping_privs = ['Mapping.Audit', 'Mapping.Modify', 'Mapping.Use'];
+
+        my $res = [];
+        foreach my $id (keys $cfg->{ids}->%*) {
+            my $type = $cfg->{ids}->{$id}->{type};
+
+            next if defined($param->{type}) && $type ne $param->{type};
+            next
+                if !$rpcenv->check_any($authuser, "/mapping/storage/$type/$id", $mapping_privs, 1);
+
+            my $map = $cfg->{ids}->{$id}->{map};
+
+            push @$res,
+                {
+                    id => $id,
+                    type => $type,
+                    map => $map,
+                };
+        }
+
+        return $res;
+    },
+});
+
+__PACKAGE__->register_method({
+    name => 'create',
+    path => '',
+    method => 'POST',
+    description => 'Create a new storage mapping.',
+    permissions => {
+        check => ['perm', '/mapping/storage/', ['Mapping.Modify']],
+    },
+    parameters => PVE::Storage::Mapping::Plugin->createSchema(),
+    returns => {
+        type => 'null',
+    },
+    code => sub {
+        my ($param) = @_;
+
+        my $id = extract_param($param, 'id');
+        my $type = extract_param($param, 'type');
+
+        my $plugin = PVE::Storage::Mapping::Plugin->lookup($type);
+        my $opts = $plugin->check_config($id, $param, 1, 1);
+
+        PVE::Storage::Mapping::Plugin::lock_storage_mapping_config(
+            sub {
+                my $cfg = PVE::Storage::Mapping::Plugin::config();
+
+                die "storage mapping ID '$id' already defined\n" if defined($cfg->{ids}->{$id});
+
+                $cfg->{ids}->{$id} = $opts;
+
+                PVE::Storage::Mapping::Plugin::write_storage_mapping_config($cfg);
+            },
+            "create storage mapping failed",
+        );
+
+        return;
+    },
+});
+
+__PACKAGE__->register_method({
+    name => 'update',
+    path => '{id}',
+    method => 'PUT',
+    description => 'Update a storage mapping.',
+    permissions => {
+        check => ['perm', '/mapping/storage/{id}', ['Mapping.Modify']],
+    },
+    parameters => PVE::Storage::Mapping::Plugin->updateSchema(),
+    returns => {
+        type => 'null',
+    },
+    code => sub {
+        my ($param) = @_;
+
+        my $digest = extract_param($param, 'digest');
+        my $delete = extract_param($param, 'delete');
+        my $id = extract_param($param, 'id');
+        my $type = extract_param($param, 'type');
+
+        if ($delete) {
+            $delete = [PVE::Tools::split_list($delete)];
+        }
+
+        my $plugin = PVE::Storage::Mapping::Plugin->lookup($type);
+        my $opts = $plugin->check_config($id, $param, 1, 1);
+
+        PVE::Storage::Mapping::Plugin::lock_storage_mapping_config(
+            sub {
+                my $cfg = PVE::Storage::Mapping::Plugin::config();
+
+                PVE::Tools::assert_if_modified($cfg->{digest}, $digest) if defined($digest);
+
+                die "storage mapping ID '$id' does not exist\n" if !defined($cfg->{ids}->{$id});
+
+                my $data = $cfg->{ids}->{$id};
+                my $options = $plugin->private()->{options}->{$type};
+                PVE::SectionConfig::delete_from_config($data, $options, $opts, $delete);
+                foreach my $key (keys $opts->%*) {
+                    $data->{$key} = $opts->{$key};
+                }
+
+                PVE::Storage::Mapping::Plugin::write_storage_mapping_config($cfg);
+            },
+            "update storage mapping failed",
+        );
+
+        return;
+    },
+});
+
+__PACKAGE__->register_method({
+    name => 'delete',
+    path => '{id}',
+    method => 'DELETE',
+    description => 'Remove a storage mapping.',
+    permissions => {
+        check => ['perm', '/mapping/storage', ['Mapping.Modify']],
+    },
+    parameters => {
+        additionalProperties => 0,
+        properties => {
+            id => {
+                type => 'string',
+                format => 'pve-storage-id',
+            },
+        },
+    },
+    returns => {
+        type => 'null',
+    },
+    code => sub {
+        my ($param) = @_;
+
+        my $id = $param->{id};
+
+        PVE::Storage::Mapping::Plugin::lock_storage_mapping_config(
+            sub {
+                my $cfg = PVE::Storage::Mapping::Plugin::config();
+
+                if ($cfg->{ids}->{$id}) {
+                    delete $cfg->{ids}->{$id};
+                }
+
+                PVE::Storage::Mapping::Plugin::write_storage_mapping_config($cfg);
+            },
+            "delete storage mapping failed",
+        );
+
+        return;
+    },
+});
+
+1;
diff --git a/src/PVE/Storage/Mapping/Plugin.pm b/src/PVE/Storage/Mapping/Plugin.pm
index 2da2e26..a0d3198 100644
--- a/src/PVE/Storage/Mapping/Plugin.pm
+++ b/src/PVE/Storage/Mapping/Plugin.pm
@@ -71,4 +71,18 @@ sub get_map_format {
     die "implement in subclass\n";
 }
 
+sub lock_storage_mapping_config {
+    my ($code, $errmsg) = @_;
+
+    cfs_lock_file($FILENAME, undef, $code);
+    if (my $err = $@) {
+        $errmsg ? die "$errmsg: $err" : die $err;
+    }
+}
+
+sub write_storage_mapping_config {
+    my ($cfg) = @_;
+
+    cfs_write_file($FILENAME, $cfg);
+}
 1;
-- 
2.47.3




  parent reply	other threads:[~2026-04-30 17:33 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-30 17:26 [PATCH v2 cluster/storage/manager 00/15] storage mapping Mira Limbeck
2026-04-30 17:26 ` [PATCH v2 cluster 01/15] mapping: add storage.cfg Mira Limbeck
2026-04-30 17:27 ` [PATCH v2 storage 02/15] mapping: add base plugin Mira Limbeck
2026-04-30 17:35   ` Mira Limbeck
2026-04-30 17:27 ` [PATCH v2 storage 03/15] mapping: add iSCSI plugin Mira Limbeck
2026-04-30 17:27 ` [PATCH v2 storage 04/15] iscsi: introduce mapping support Mira Limbeck
2026-04-30 17:27 ` [PATCH v2 storage 05/15] iscsi: add helper to get local config Mira Limbeck
2026-04-30 17:27 ` [PATCH v2 storage 06/15] iscsi: change functions to handle mappings Mira Limbeck
2026-04-30 17:27 ` [PATCH v2 storage 07/15] iscsi: introduce helper to update discovery db Mira Limbeck
2026-04-30 17:27 ` [PATCH v2 storage 08/15] iscsi: rework to update discovery db and simplify login Mira Limbeck
2026-04-30 17:27 ` [PATCH v2 storage 09/15] iscsi: remove stale sessions in non-mapping case Mira Limbeck
2026-04-30 17:27 ` Mira Limbeck [this message]
2026-04-30 17:27 ` [PATCH v2 storage 11/15] mapping: iscsi: add discovery-portal config option Mira Limbeck
2026-04-30 17:27 ` [PATCH v2 storage 12/15] iscsi: add support for non-persistent discovery Mira Limbeck
2026-04-30 17:38   ` Mira Limbeck
2026-04-30 17:27 ` [PATCH v2 storage 13/15] api: add non-persistent iscsi discovery option Mira Limbeck
2026-04-30 17:27 ` [POC v2 storage 14/15] mapping: add zfspool plugin Mira Limbeck
2026-04-30 17:27 ` [PATCH v2 manager 15/15] api: mapping: add storage mapping path Mira Limbeck

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=20260430173220.441001-11-m.limbeck@proxmox.com \
    --to=m.limbeck@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal