]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/JSONSchema.pm
parse_property_string: add newline to error strings
[pve-common.git] / src / PVE / JSONSchema.pm
index 5beebd80139a1aacbb8ce6dfc34b9bfa50417eb2..e84c661cbd01e19182dd604d33c5b6024d2b62c9 100644 (file)
@@ -341,6 +341,16 @@ sub pve_verify_address {
     return $addr;
 }
 
+register_format('disk-size', \&pve_verify_disk_size);
+sub pve_verify_disk_size {
+    my ($size, $noerr) = @_;
+    if (!defined(parse_size($size))) {
+       return undef if $noerr;
+       die "value does not look like a valid disk size: $size\n";
+    }
+    return $size;
+}
+
 register_standard_option('spice-proxy', {
     description => "SPICE proxy server. This can be used by the client to specify the proxy server. All nodes in a cluster runs 'spiceproxy', so it is up to the client to choose one. By default, we return the node where the VM is currently running. As resonable setting is to use same node you use to connect to the API (This is window.location.hostname for the JS GUI).",
     type => 'string', format => 'address',
@@ -488,16 +498,16 @@ sub parse_property_string {
 
        if ($part =~ /^([^=]+)=(.+)$/) {
            my ($k, $v) = ($1, $2);
-           die "duplicate key in comma-separated list property: $k" if defined($res->{$k});
+           die "duplicate key in comma-separated list property: $k\n" if defined($res->{$k});
            my $schema = $format->{$k};
-           die "invalid key in comma-separated list property: $k" if !$schema;
+           die "invalid key in comma-separated list property: $k\n" if !$schema;
            if ($schema->{type} && $schema->{type} eq 'boolean') {
                $v = 1 if $v =~ m/^(1|on|yes|true)$/i;
                $v = 0 if $v =~ m/^(0|off|no|false)$/i;
            }
            $res->{$k} = $v;
        } elsif ($part !~ /=/) {
-           die "duplicate key in comma-separated list property: $default_key" if $default_key;
+           die "duplicate key in comma-separated list property: $default_key\n" if $default_key;
            foreach my $key (keys %$format) {
                if ($format->{$key}->{default_key}) {
                    $default_key = $key;
@@ -505,23 +515,91 @@ sub parse_property_string {
                        $res->{$default_key} = $part;
                        last;
                    }
-                   die "duplicate key in comma-separated list property: $default_key";
+                   die "duplicate key in comma-separated list property: $default_key\n";
                }
            }
        } else {
-           die "missing key in comma-separated list property";
+           die "missing key in comma-separated list property\n";
        }
     }
 
     my $errors = {};
     check_object($path, $format, $res, undef, $errors);
     if (scalar(%$errors)) {
-       raise "format error", errors => $errors;
+       raise "format error\n", errors => $errors;
     }
 
     return $res;
 }
 
+sub print_property_string {
+    my ($data, $format, $skip, $path) = @_;
+
+    if (ref($format) ne 'HASH') {
+       my $schema = $format_list->{$format};
+       die "not a valid format: $format" if !$schema;
+       $format = $schema;
+    }
+
+    my $errors = {};
+    check_object($path, $format, $data, undef, $errors);
+    if (scalar(%$errors)) {
+       raise "format error", errors => $errors;
+    }
+
+    my $default_key;
+    my %skipped = map { $_ => 1 } @$skip;
+    my %allowed;
+    my %required; # this is a set, all present keys are required regardless of value
+    foreach my $key (keys %$format) {
+       $allowed{$key} = 1;
+       if (!$format->{$key}->{optional} && !$skipped{$key}) {
+           $required{$key} = 1;
+       }
+
+       # Skip default keys
+       if ($format->{$key}->{default_key}) {
+           if ($default_key) {
+               warn "multiple default keys in schema ($default_key, $key)";
+           } else {
+               $default_key = $key;
+               $skipped{$key} = 1;
+           }
+       }
+    }
+
+    my ($text, $comma);
+    if ($default_key) {
+       $text = "$data->{$default_key}";
+       $comma = ',';
+    } else {
+       $text = '';
+       $comma = '';
+    }
+
+    foreach my $key (sort keys %$data) {
+       die "invalid key: $key" if !$allowed{$key};
+       delete $required{$key};
+       next if $skipped{$key};
+
+       my $type = $format->{$key}->{type};
+       my $value = $data->{$key};
+       $text .= $comma;
+       $comma = ',';
+       if ($type eq 'disk-size') {
+           $text .= "$key=" . format_size($value);
+       } else {
+           $text .= "$key=$value";
+       }
+    }
+
+    if (my $missing = join(',', keys %required)) {
+       die "missing properties: $missing";
+    }
+
+    return $text;
+}
+
 sub add_error {
     my ($errors, $path, $msg) = @_;