ISCSI `map` parameter being optional allows iSCSI storage mappings to be created with a non-existing map list. This can make sense since once `map` is specified, it needs values for `node`, `target` and `portals`.
Signed-off-by: Mira Limbeck <[email protected]> --- src/PVE/API2/Storage/Makefile | 2 +- src/PVE/API2/Storage/Mapping.pm | 213 ++++++++++++++++++++++++++++++ src/PVE/Storage/Mapping/ISCSI.pm | 2 +- src/PVE/Storage/Mapping/Plugin.pm | 14 ++ 4 files changed, 229 insertions(+), 2 deletions(-) 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..e90345d --- /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}->{iscsi}; + 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/ISCSI.pm b/src/PVE/Storage/Mapping/ISCSI.pm index 34d00c5..cec2d02 100644 --- a/src/PVE/Storage/Mapping/ISCSI.pm +++ b/src/PVE/Storage/Mapping/ISCSI.pm @@ -43,7 +43,7 @@ sub properties { sub options { return { description => { optional => 1 }, - map => {}, + map => { optional => 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.39.5 _______________________________________________ pve-devel mailing list [email protected] https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel
