From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 529939DCA6 for ; Tue, 6 Jun 2023 10:39:17 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 36A10316E8 for ; Tue, 6 Jun 2023 10:39:17 +0200 (CEST) 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)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Tue, 6 Jun 2023 10:39:16 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id A41C248C5C for ; Tue, 6 Jun 2023 10:39:15 +0200 (CEST) From: Dominik Csapak To: pve-devel@lists.proxmox.com Date: Tue, 6 Jun 2023 10:39:09 +0200 Message-Id: <20230606083914.1400960-3-d.csapak@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230606083914.1400960-1-d.csapak@proxmox.com> References: <20230606083914.1400960-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.015 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy 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 T_SCC_BODY_TEXT_LINE -0.01 - Subject: [pve-devel] [PATCH common v2 2/3] section config: implement array support X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 06 Jun 2023 08:39:17 -0000 enables section configs in the style of: ---- type: id property value property value2 property value3 ---- can be combined with property strings the provided create and update schema just pass through the array type to the api, so the api call must always contain the complete array also adds a test case for such array fields Signed-off-by: Dominik Csapak --- changes from v1: * remove unnecessary code move src/PVE/SectionConfig.pm | 42 ++++++++++++++++++++++++++++++++----- test/section_config_test.pl | 26 +++++++++++++++++++++++ 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/PVE/SectionConfig.pm b/src/PVE/SectionConfig.pm index f36cede..97492db 100644 --- a/src/PVE/SectionConfig.pm +++ b/src/PVE/SectionConfig.pm @@ -254,7 +254,15 @@ sub check_value { if (!$skipSchemaCheck) { my $errors = {}; - PVE::JSONSchema::check_prop($value, $schema, '', $errors); + + my $checkschema = $schema; + + if ($ct eq 'array') { + die "no item schema for array" if !defined($schema->{items}); + $checkschema = $schema->{items}; + } + + PVE::JSONSchema::check_prop($value, $checkschema, '', $errors); if (scalar(keys %$errors)) { die "$errors->{$key}\n" if $errors->{$key}; die "$errors->{_root}\n" if $errors->{_root}; @@ -311,6 +319,15 @@ sub parse_config { } }; + my $is_array = sub { + my ($type, $key) = @_; + + my $schema = $pdata->{propertyList}->{$key}; + die "unknown property type\n" if !$schema; + + return $schema->{type} eq 'array'; + }; + my $errors = []; while (@lines) { my $line = $nextline->(); @@ -352,11 +369,19 @@ sub parse_config { my ($k, $v) = ($1, $3); eval { - die "duplicate attribute\n" if defined($config->{$k}); - if (!$unknown) { - $v = $plugin->check_value($type, $k, $v, $sectionId); + if ($is_array->($type, $k)) { + if (!$unknown) { + $v = $plugin->check_value($type, $k, $v, $sectionId); + } + $config->{$k} = [] if !defined($config->{$k}); + push $config->{$k}->@*, $v; + } else { + die "duplicate attribute\n" if defined($config->{$k}); + if (!$unknown) { + $v = $plugin->check_value($type, $k, $v, $sectionId); + } + $config->{$k} = $v; } - $config->{$k} = $v; }; if (my $err = $@) { warn "$errprefix (section '$sectionId') - unable to parse value of '$k': $err"; @@ -448,6 +473,13 @@ my $format_config_line = sub { if ($ct eq 'boolean') { return "\t$key " . ($value ? 1 : 0) . "\n" if defined($value); + } elsif ($ct eq 'array') { + die "property '$key' is not an array" if ref($value) ne 'ARRAY'; + my $result = ''; + for my $line ($value->@*) { + $result .= "\t$key $line\n" if $value ne ''; + } + return $result; } else { return "\t$key $value\n" if "$value" ne ''; } diff --git a/test/section_config_test.pl b/test/section_config_test.pl index 22a9643..02242bc 100755 --- a/test/section_config_test.pl +++ b/test/section_config_test.pl @@ -105,6 +105,25 @@ sub properties { minimum => 3, maximum => 9, }, + arrayfield => { + description => "Array Field with property string", + type => 'array', + items => { + type => 'string', + description => 'a property string', + format => { + subfield1 => { + type => 'string', + description => 'first subfield' + }, + subfield2 => { + type => 'integer', + minimum => 0, + optional => 1, + }, + }, + }, + }, }; } @@ -113,6 +132,7 @@ sub options { common => { optional => 1 }, field2 => {}, another => {}, + arrayfield => { optional => 1 }, }; } @@ -190,6 +210,10 @@ my $with_unknown_data = { type => 'two', field2 => 5, another => 'even more text', + arrayfield => [ + 'subfield1=test,subfield2=2', + 'subfield1=test2', + ], }, invalid => { type => 'bad', @@ -214,6 +238,8 @@ bad: invalid two: t3 field2 5 another even more text + arrayfield subfield1=test,subfield2=2 + arrayfield subfield1=test2 EOF Conf->expect_fail('unknown-forbidden', $with_unknown_data, $with_unknown_text); -- 2.30.2