public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Fiona Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com, pbs-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH proxmox 1/1] section config: parse additional properties when schema allows it
Date: Fri, 21 Oct 2022 15:02:44 +0200	[thread overview]
Message-ID: <20221021130252.176316-2-f.ebner@proxmox.com> (raw)
In-Reply-To: <20221021130252.176316-1-f.ebner@proxmox.com>

Additional properties will be parsed according to the default string
schema.

This is relevant for use cases when the full schema is not known for
some reason or another. In particular this allows support for parsing
older/newer versions of configuration files. One example of this is
the proposed proxmox-mail-forward helper binary, which currently
doesn't have access to the PBS API types for dependency reasons and
is only interested in the email field for the root user. If it can
only use a minimal schema with additional_properties set to true, it
will be robust against changes.

Writing already works, because the ObjectSchema's verify_json()
already handles additional_properties correctly and
format_section_content() handles them like all other properties
(method doesn't depend on the schema).

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
 proxmox-section-config/src/lib.rs | 79 ++++++++++++++++++++++++++++++-
 1 file changed, 78 insertions(+), 1 deletion(-)

diff --git a/proxmox-section-config/src/lib.rs b/proxmox-section-config/src/lib.rs
index 154f56e..080f23c 100644
--- a/proxmox-section-config/src/lib.rs
+++ b/proxmox-section-config/src/lib.rs
@@ -31,6 +31,9 @@ use proxmox_lang::try_block;
 use proxmox_schema::format::{dump_properties, wrap_text, ParameterDisplayStyle};
 use proxmox_schema::*;
 
+/// Used for additional properties when the schema allows them.
+const ADDITIONAL_PROPERTY_SCHEMA: Schema = StringSchema::new("Additional property").schema();
+
 /// Associates a section type name with a `Schema`.
 pub struct SectionConfigPlugin {
     type_name: String,
@@ -446,7 +449,10 @@ impl SectionConfig {
                                         (true, items)
                                     }
                                     Some((_optional, ref prop_schema)) => (false, prop_schema),
-                                    None => bail!("unknown property '{}'", key),
+                                    None => match plugin.properties.additional_properties() {
+                                        true => (false, &&ADDITIONAL_PROPERTY_SCHEMA),
+                                        false => bail!("unknown property '{}'", key),
+                                    },
                                 };
 
                                 let value = match prop_schema.parse_simple_value(&value) {
@@ -884,6 +890,77 @@ lvmthin: local-lvm2
     assert_eq!(raw, created);
 }
 
+#[test]
+fn test_section_config_with_additional_properties() {
+    let filename = "user.cfg";
+
+    const ID_SCHEMA: Schema = StringSchema::new("default id schema.")
+        .min_length(3)
+        .schema();
+    let mut config = SectionConfig::new(&ID_SCHEMA);
+    let mut config_with_additional = SectionConfig::new(&ID_SCHEMA);
+
+    const PROPERTIES: [(&str, bool, &proxmox_schema::Schema); 2] = [
+        (
+            "email",
+            false,
+            &StringSchema::new("The e-mail of the user").schema(),
+        ),
+        (
+            "userid",
+            true,
+            &StringSchema::new("The id of the user (name@realm).")
+                .min_length(3)
+                .schema(),
+        ),
+    ];
+
+    const USER_PROPERTIES: ObjectSchema = ObjectSchema {
+        description: "user properties",
+        properties: &PROPERTIES,
+        additional_properties: false,
+        default_key: None,
+    };
+
+    const USER_PROPERTIES_WITH_ADDTIONAL: ObjectSchema = ObjectSchema {
+        description: "user properties with additional",
+        properties: &PROPERTIES,
+        additional_properties: true,
+        default_key: None,
+    };
+
+    let plugin = SectionConfigPlugin::new(
+        "user".to_string(),
+        Some("userid".to_string()),
+        &USER_PROPERTIES,
+    );
+    config.register_plugin(plugin);
+
+    let plugin = SectionConfigPlugin::new(
+        "user".to_string(),
+        Some("userid".to_string()),
+        &USER_PROPERTIES_WITH_ADDTIONAL,
+    );
+    config_with_additional.register_plugin(plugin);
+
+    let raw = r"
+
+user: root@pam
+        email root@example.com
+        shinynewoption somevalue
+";
+
+    let res = config_with_additional.parse(filename, raw);
+    println!("RES: {:?}", res);
+    let written = config_with_additional.write(filename, &res.unwrap());
+    println!("CONFIG:\n{}", written.unwrap());
+
+    assert!(config.parse(filename, raw).is_err());
+    // SectionConfigData doesn't have Clone and it would only be needed here currently.
+    let res = config_with_additional.parse(filename, raw);
+    assert!(config.write(filename, &res.unwrap()).is_err());
+}
+
 /// Generate ReST Documentaion for ``SectionConfig``
 pub fn dump_section_config(config: &SectionConfig) -> String {
     let mut res = String::new();
-- 
2.30.2





  reply	other threads:[~2022-10-21 13:03 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-21 13:02 [pve-devel] [PATCH-SERIES proxmox{, -mail-forward, -backup}/pve-manager] add proxmox-mail-forward helper binary Fiona Ebner
2022-10-21 13:02 ` Fiona Ebner [this message]
2022-10-24 11:47   ` [pve-devel] applied: [PATCH proxmox 1/1] section config: parse additional properties when schema allows it Wolfgang Bumiller
2022-10-21 13:02 ` [pve-devel] [PATCH proxmox-mail-forward 1/3] initial commit Fiona Ebner
2022-11-10 10:46   ` [pve-devel] applied: " Wolfgang Bumiller
2022-10-21 13:02 ` [pve-devel] [PATCH proxmox-mail-forward 2/3] add Debian packaging Fiona Ebner
2022-10-21 13:02 ` [pve-devel] [PATCH proxmox-mail-forward 3/3] d/postinst: register binary in .forward Fiona Ebner
2022-10-21 13:02 ` [pve-devel] [PATCH proxmox-backup 1/1] fix #4287: d/control: recommend proxmox-mail-forward Fiona Ebner
2022-11-10 10:49   ` [pve-devel] applied: " Wolfgang Bumiller
2022-10-21 13:02 ` [pve-devel] [PATCH manager 1/4] d/control: depend on proxmox-mail-forward Fiona Ebner
2022-10-21 13:02 ` [pve-devel] [PATCH manager 2/4] d/postinst: replace pvemailforward with proxmox-mail-forward Fiona Ebner
2022-10-21 13:02 ` [pve-devel] [PATCH manager 3/4] remove pvemailforward binary Fiona Ebner
2022-10-21 13:02 ` [pve-devel] [PATCH manager 4/4] d/control: drop ${shlibs:Depends} for pve-manager Fiona Ebner
2022-11-10 11:11   ` Thomas Lamprecht
2022-11-10 10:58 ` [pve-devel] applied-series: [PATCH-SERIES proxmox{, -mail-forward, -backup}/pve-manager] add proxmox-mail-forward helper binary Wolfgang Bumiller

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=20221021130252.176316-2-f.ebner@proxmox.com \
    --to=f.ebner@proxmox.com \
    --cc=pbs-devel@lists.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