[pve-devel] [PATCH access-control v2 1/3] domain sync: make options actually required

Dominik Csapak d.csapak at proxmox.com
Thu Apr 23 08:47:17 CEST 2020


we want the api options to be optional, but only as long as there are
default values set in the realm config

since they are all marked as optional (else they would be required in
the api) this check did not work as intended

instead, set the result to the value of:
* the parameter
* the set default in the config
* the api default
in this order

if it is undef after this, raise a parameter exception

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
changes from v1:
* better structure code to see the precedence of parameters

 PVE/API2/Domains.pm | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/PVE/API2/Domains.pm b/PVE/API2/Domains.pm
index b3c3ac4..e139869 100644
--- a/PVE/API2/Domains.pm
+++ b/PVE/API2/Domains.pm
@@ -372,21 +372,18 @@ my $parse_sync_opts = sub {
     my $sync_opts_fmt = PVE::JSONSchema::get_format('realm-sync-options');
 
     my $res = {};
+    my $defaults = {};
     if (defined(my $cfg_opts = $realmconfig->{'sync-defaults-options'})) {
-	$res = PVE::JSONSchema::parse_property_string($sync_opts_fmt, $cfg_opts);
+	$defaults = PVE::JSONSchema::parse_property_string($sync_opts_fmt, $cfg_opts);
     }
 
     for my $opt (sort keys %$sync_opts_fmt) {
 	my $fmt = $sync_opts_fmt->{$opt};
 
-	if (exists $param->{$opt}) {
-	    $res->{$opt} = $param->{$opt};
-	} elsif (!exists $res->{$opt}) {
-	    raise_param_exc({
-		"$opt" => 'Not passed as parameter and not defined in realm default sync options.'
-	    }) if !$fmt->{optional};
-	    $res->{$opt} = $fmt->{default} if exists $fmt->{default};
-	}
+	$res->{$opt} = $param->{$opt} // $defaults->{$opt} // $fmt->{default};
+	raise_param_exc({
+	    "$opt" => 'Not passed as parameter and not defined in realm default sync options.'
+	}) if !defined($res->{$opt});
     }
     return $res;
 };
-- 
2.20.1





More information about the pve-devel mailing list