]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/JSONSchema.pm
added JSONSchema::print_property_string
[pve-common.git] / src / PVE / JSONSchema.pm
index 5beebd80139a1aacbb8ce6dfc34b9bfa50417eb2..0cb281d9e84ffbb2b25df4aa3e68e30e950b6035 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',
@@ -522,6 +532,74 @@ sub parse_property_string {
     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) = @_;