]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/SectionConfig.pm
Add AbstractConfig base class
[pve-common.git] / src / PVE / SectionConfig.pm
index 5fc479eb7b446d90ec228fa9e493cd7d1ccbfdee..6f85b228b81ec82c77d64fbbbc8f0197e5274e01 100644 (file)
@@ -26,6 +26,9 @@ sub register {
     my $type = $class->type();
     my $pdata = $class->private();
 
+    die "duplicate plugin registration (type = $type)"
+       if defined($pdata->{plugins}->{$type});
+
     my $plugindata = $class->plugindata();
     $pdata->{plugindata}->{$type} = $plugindata;
     $pdata->{plugins}->{$type} = $class;
@@ -52,11 +55,51 @@ sub createSchema {
 
     my $pdata = $class->private();
     my $propertyList = $pdata->{propertyList};
+    my $plugins = $pdata->{plugins};
+
+    my $props = {};
+
+    my $copy_property = sub {
+       my ($src) = @_;
+
+       my $res = {};
+       foreach my $k (keys %$src) {
+           $res->{$k} = $src->{$k};
+       }
+
+       return $res;
+    };
+
+    foreach my $p (keys %$propertyList) {
+       if (!$propertyList->{$p}->{optional}) {
+           $props->{$p} = $propertyList->{$p};
+           next;
+       }
+
+       my $required = 1;
+
+       my $copts = $class->options();
+       $required = 0 if defined($copts->{$p}) && $copts->{$p}->{optional};
+
+       foreach my $t (keys %$plugins) {
+           my $opts = $pdata->{options}->{$t} || {};
+           $required = 0 if !defined($opts->{$p}) || $opts->{$p}->{optional};
+       }
+
+       if ($required) {
+           # make a copy, because we modify the optional property
+           my $res = &$copy_property($propertyList->{$p});
+           $res->{optional} = 0;
+           $props->{$p} = $res;
+       } else {
+           $props->{$p} = $propertyList->{$p};
+       }
+    }
 
     return {
        type => "object",
        additionalProperties => 0,
-       properties => $propertyList,
+       properties => $props,
     };
 }
 
@@ -75,13 +118,20 @@ sub updateSchema {
            $props->{$p} = $propertyList->{$p};
            next;
        }
+
+       my $modifyable = 0;
+
+       my $copts = $class->options();
+       $modifyable = 1 if defined($copts->{$p}) && !$copts->{$p}->{fixed};
+
        foreach my $t (keys %$plugins) {
-           my $opts = $pdata->{options}->{$t};
+           my $opts = $pdata->{options}->{$t} || {};
            next if !defined($opts->{$p});
-           if (!$opts->{$p}->{fixed}) {
-               $props->{$p} = $propertyList->{$p};
-           }
+           $modifyable = 1 if !$opts->{$p}->{fixed};
        }
+       next if !$modifyable;
+
+       $props->{$p} = $propertyList->{$p};
     }
 
     $props->{digest} = get_standard_option('pve-config-digest');
@@ -346,6 +396,9 @@ my $format_config_line = sub {
 
     my $ct = $schema->{type};
 
+    die "property '$key' contains a line feed\n"
+       if ($key =~ m/[\n\r]/) || ($value =~ m/[\n\r]/);
+
     if ($ct eq 'boolean') {
        return $value ? "\t$key\n" : '';
     } else {