]>
git.proxmox.com Git - pve-common.git/blob - src/PVE/JSONSchema.pm
1 package PVE
:: JSONSchema
;
5 use Storable
; # for dclone
7 use Devel
:: Cycle
- quiet
; # todo: remove?
8 use PVE
:: Tools
qw(split_list $IPV6RE $IPV4RE ) ;
9 use PVE
:: Exception
qw(raise) ;
10 use HTTP
:: Status
qw(:constants) ;
11 use Net
:: IP
qw(:PROC) ;
17 register_standard_option
21 # Note: This class implements something similar to JSON schema, but it is not 100% complete.
22 # see: http://tools.ietf.org/html/draft-zyp-json-schema-02
23 # see: http://json-schema.org/
25 # the code is similar to the javascript parser from http://code.google.com/p/jsonschema/
27 my $standard_options = {};
28 sub register_standard_option
{
29 my ( $name, $schema ) = @_ ;
31 die "standard option ' $name ' already registered \n "
32 if $standard_options ->{ $name };
34 $standard_options ->{ $name } = $schema ;
37 sub get_standard_option
{
38 my ( $name, $base ) = @_ ;
40 my $std = $standard_options ->{ $name };
41 die "no such standard option ' $name ' \n " if ! $std ;
43 my $res = $base || {};
45 foreach my $opt ( keys %$std ) {
46 next if defined ( $res ->{ $opt });
47 $res ->{ $opt } = $std ->{ $opt };
53 register_standard_option
( 'pve-vmid' , {
54 description
=> "The (unique) ID of the VM." ,
55 type
=> 'integer' , format
=> 'pve-vmid' ,
59 register_standard_option
( 'pve-node' , {
60 description
=> "The cluster node name." ,
61 type
=> 'string' , format
=> 'pve-node' ,
64 register_standard_option
( 'pve-node-list' , {
65 description
=> "List of cluster node names." ,
66 type
=> 'string' , format
=> 'pve-node-list' ,
69 register_standard_option
( 'pve-iface' , {
70 description
=> "Network interface name." ,
71 type
=> 'string' , format
=> 'pve-iface' ,
72 minLength
=> 2 , maxLength
=> 20 ,
75 register_standard_option
( 'pve-storage-id' , {
76 description
=> "The storage identifier." ,
77 type
=> 'string' , format
=> 'pve-storage-id' ,
80 register_standard_option
( 'pve-config-digest' , {
81 description
=> 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.' ,
84 maxLength
=> 40 , # sha1 hex digest lenght is 40
87 register_standard_option
( 'skiplock' , {
88 description
=> "Ignore locks - only root is allowed to use this option." ,
93 register_standard_option
( 'extra-args' , {
94 description
=> "Extra arguments as array" ,
96 items
=> { type
=> 'string' },
100 my $format_list = {};
102 sub register_format
{
103 my ( $format, $code ) = @_ ;
105 die "JSON schema format ' $format ' already registered \n "
106 if $format_list ->{ $format };
108 $format_list ->{ $format } = $code ;
113 return $format_list ->{ $format };
116 # register some common type for pve
118 register_format
( 'string' , sub {}); # allow format => 'string-list'
120 register_format
( 'urlencoded' , \
& pve_verify_urlencoded
);
121 sub pve_verify_urlencoded
{
122 my ( $text, $noerr ) = @_ ;
123 if ( $text !~ /^[-%a-zA-Z0-9_.!~*'()]*$/ ) {
124 return undef if $noerr ;
125 die "invalid urlencoded string: $text\n " ;
130 register_format
( 'pve-configid' , \
& pve_verify_configid
);
131 sub pve_verify_configid
{
132 my ( $id, $noerr ) = @_ ;
134 if ( $id !~ m/^[a-z][a-z0-9_]+$/i ) {
135 return undef if $noerr ;
136 die "invalid configuration ID ' $id ' \n " ;
141 PVE
:: JSONSchema
:: register_format
( 'pve-storage-id' , \
& parse_storage_id
);
142 sub parse_storage_id
{
143 my ( $storeid, $noerr ) = @_ ;
145 if ( $storeid !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i ) {
146 return undef if $noerr ;
147 die "storage ID ' $storeid ' contains illegal characters \n " ;
153 register_format
( 'pve-vmid' , \
& pve_verify_vmid
);
154 sub pve_verify_vmid
{
155 my ( $vmid, $noerr ) = @_ ;
157 if ( $vmid !~ m/^[1-9][0-9]{2,8}$/ ) {
158 return undef if $noerr ;
159 die "value does not look like a valid VM ID \n " ;
164 register_format
( 'pve-node' , \
& pve_verify_node_name
);
165 sub pve_verify_node_name
{
166 my ( $node, $noerr ) = @_ ;
168 if ( $node !~ m/^([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)$/ ) {
169 return undef if $noerr ;
170 die "value does not look like a valid node name \n " ;
175 register_format
( 'ipv4' , \
& pve_verify_ipv4
);
176 sub pve_verify_ipv4
{
177 my ( $ipv4, $noerr ) = @_ ;
179 if ( $ipv4 !~ m/^(?:$IPV4RE)$/ ) {
180 return undef if $noerr ;
181 die "value does not look like a valid IPv4 address \n " ;
186 register_format
( 'ipv6' , \
& pve_verify_ipv6
);
187 sub pve_verify_ipv6
{
188 my ( $ipv6, $noerr ) = @_ ;
190 if ( $ipv6 !~ m/^(?:$IPV6RE)$/ ) {
191 return undef if $noerr ;
192 die "value does not look like a valid IPv6 address \n " ;
197 register_format
( 'ip' , \
& pve_verify_ip
);
199 my ( $ip, $noerr ) = @_ ;
201 if ( $ip !~ m/^(?:(?:$IPV4RE)|(?:$IPV6RE))$/ ) {
202 return undef if $noerr ;
203 die "value does not look like a valid IP address \n " ;
208 my $ipv4_mask_hash = {
225 '255.255.128.0' => 17 ,
226 '255.255.192.0' => 18 ,
227 '255.255.224.0' => 19 ,
228 '255.255.240.0' => 20 ,
229 '255.255.248.0' => 21 ,
230 '255.255.252.0' => 22 ,
231 '255.255.254.0' => 23 ,
232 '255.255.255.0' => 24 ,
233 '255.255.255.128' => 25 ,
234 '255.255.255.192' => 26 ,
235 '255.255.255.224' => 27 ,
236 '255.255.255.240' => 28 ,
237 '255.255.255.248' => 29 ,
238 '255.255.255.252' => 30 ,
239 '255.255.255.254' => 31 ,
240 '255.255.255.255' => 32 ,
243 register_format
( 'ipv4mask' , \
& pve_verify_ipv4mask
);
244 sub pve_verify_ipv4mask
{
245 my ( $mask, $noerr ) = @_ ;
247 if (! defined ( $ipv4_mask_hash ->{ $mask })) {
248 return undef if $noerr ;
249 die "value does not look like a valid IP netmask \n " ;
254 register_format
( 'CIDRv6' , \
& pve_verify_cidrv6
);
255 sub pve_verify_cidrv6
{
256 my ( $cidr, $noerr ) = @_ ;
258 if ( $cidr =~ m!^(?:$IPV6RE)(?:/(\d+))$! && ( $1 > 7 ) && ( $1 <= 120 )) {
262 return undef if $noerr ;
263 die "value does not look like a valid IPv6 CIDR network \n " ;
266 register_format
( 'CIDRv4' , \
& pve_verify_cidrv4
);
267 sub pve_verify_cidrv4
{
268 my ( $cidr, $noerr ) = @_ ;
270 if ( $cidr =~ m!^(?:$IPV4RE)(?:/(\d+))$! && ( $1 > 7 ) && ( $1 <= 32 )) {
274 return undef if $noerr ;
275 die "value does not look like a valid IPv4 CIDR network \n " ;
278 register_format
( 'CIDR' , \
& pve_verify_cidr
);
279 sub pve_verify_cidr
{
280 my ( $cidr, $noerr ) = @_ ;
282 if (!( pve_verify_cidrv4
( $cidr, 1 ) ||
283 pve_verify_cidrv6
( $cidr, 1 )))
285 return undef if $noerr ;
286 die "value does not look like a valid CIDR network \n " ;
292 register_format
( 'pve-ipv4-config' , \
& pve_verify_ipv4_config
);
293 sub pve_verify_ipv4_config
{
294 my ( $config, $noerr ) = @_ ;
296 return $config if $config =~ /^(?:dhcp|manual)$/ ||
297 pve_verify_cidrv4
( $config, 1 );
298 return undef if $noerr ;
299 die "value does not look like a valid ipv4 network configuration \n " ;
302 register_format
( 'pve-ipv6-config' , \
& pve_verify_ipv6_config
);
303 sub pve_verify_ipv6_config
{
304 my ( $config, $noerr ) = @_ ;
306 return $config if $config =~ /^(?:auto|dhcp|manual)$/ ||
307 pve_verify_cidrv6
( $config, 1 );
308 return undef if $noerr ;
309 die "value does not look like a valid ipv6 network configuration \n " ;
312 register_format
( 'email' , \
& pve_verify_email
);
313 sub pve_verify_email
{
314 my ( $email, $noerr ) = @_ ;
316 # we use same regex as in Utils.js
317 if ( $email !~ /^(\w+)([\-+.][\w]+)*@(\w[\-\w]*\.){1,5}([A-Za-z]){2,63}$/ ) {
318 return undef if $noerr ;
319 die "value does not look like a valid email address \n " ;
324 register_format
( 'dns-name' , \
& pve_verify_dns_name
);
325 sub pve_verify_dns_name
{
326 my ( $name, $noerr ) = @_ ;
328 my $namere = "([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)" ;
330 if ( $name !~ /^(${namere}\.)*${namere}$/ ) {
331 return undef if $noerr ;
332 die "value does not look like a valid DNS name \n " ;
337 # network interface name
338 register_format
( 'pve-iface' , \
& pve_verify_iface
);
339 sub pve_verify_iface
{
340 my ( $id, $noerr ) = @_ ;
342 if ( $id !~ m/^[a-z][a-z0-9_]{1,20}([:\.]\d+)?$/i ) {
343 return undef if $noerr ;
344 die "invalid network interface name ' $id ' \n " ;
349 # general addresses by name or IP
350 register_format
( 'address' , \
& pve_verify_address
);
351 sub pve_verify_address
{
352 my ( $addr, $noerr ) = @_ ;
354 if (!( pve_verify_ip
( $addr, 1 ) ||
355 pve_verify_dns_name
( $addr, 1 )))
357 return undef if $noerr ;
358 die "value does not look like a valid address: $addr\n " ;
363 register_format
( 'disk-size' , \
& pve_verify_disk_size
);
364 sub pve_verify_disk_size
{
365 my ( $size, $noerr ) = @_ ;
366 if (! defined ( parse_size
( $size ))) {
367 return undef if $noerr ;
368 die "value does not look like a valid disk size: $size\n " ;
373 register_standard_option
( 'spice-proxy' , {
374 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)." ,
375 type
=> 'string' , format
=> 'address' ,
378 register_standard_option
( 'remote-viewer-config' , {
379 description
=> "Returned values can be directly passed to the 'remote-viewer' application." ,
380 additionalProperties
=> 1 ,
382 type
=> { type
=> 'string' },
383 password
=> { type
=> 'string' },
384 proxy
=> { type
=> 'string' },
385 host
=> { type
=> 'string' },
386 'tls-port' => { type
=> 'integer' },
390 register_format
( 'pve-startup-order' , \
& pve_verify_startup_order
);
391 sub pve_verify_startup_order
{
392 my ( $value, $noerr ) = @_ ;
394 return $value if pve_parse_startup_order
( $value );
396 return undef if $noerr ;
398 die "unable to parse startup options \n " ;
401 sub pve_parse_startup_order
{
404 return undef if ! $value ;
408 foreach my $p ( split ( /,/ , $value )) {
409 next if $p =~ m/^\s*$/ ;
411 if ( $p =~ m/^(order=)?(\d+)$/ ) {
413 } elsif ( $p =~ m/^up=(\d+)$/ ) {
415 } elsif ( $p =~ m/^down=(\d+)$/ ) {
425 PVE
:: JSONSchema
:: register_standard_option
( 'pve-startup-order' , {
426 description
=> "Startup and shutdown behavior. Order is a non-negative number defining the general startup order. Shutdown in done with reverse ordering. Additionally you can set the 'up' or 'down' delay in seconds, which specifies a delay to wait before the next VM is started or stopped." ,
428 type
=> 'string' , format
=> 'pve-startup-order' ,
429 typetext
=> '[[order=]\d+] [,up=\d+] [,down=\d+] ' ,
433 my ( $format, $value, $path ) = @_ ;
435 return parse_property_string
( $format, $value, $path ) if ref ( $format ) eq 'HASH' ;
436 return if $format eq 'regex' ;
438 if ( $format =~ m/^(.*)-a?list$/ ) {
440 my $code = $format_list ->{ $1 };
442 die "undefined format ' $format ' \n " if ! $code ;
444 # Note: we allow empty lists
445 foreach my $v ( split_list
( $value )) {
449 } elsif ( $format =~ m/^(.*)-opt$/ ) {
451 my $code = $format_list ->{ $1 };
453 die "undefined format ' $format ' \n " if ! $code ;
455 return if ! $value ; # allow empty string
461 my $code = $format_list ->{ $format };
463 die "undefined format ' $format ' \n " if ! $code ;
465 return parse_property_string
( $code, $value, $path ) if ref ( $code ) eq 'HASH' ;
473 return undef if $value !~ m/^(\d+(\.\d+)?)([KMGT])?$/ ;
474 my ( $size, $unit ) = ( $1, $3 );
477 $size = $size * 1024 ;
478 } elsif ( $unit eq 'M' ) {
479 $size = $size * 1024 * 1024 ;
480 } elsif ( $unit eq 'G' ) {
481 $size = $size * 1024 * 1024 * 1024 ;
482 } elsif ( $unit eq 'T' ) {
483 $size = $size * 1024 * 1024 * 1024 * 1024 ;
494 my $kb = int ( $size/1024 );
495 return $size if $kb*1024 != $size ;
497 my $mb = int ( $kb/1024 );
498 return "${kb}K" if $mb*1024 != $kb ;
500 my $gb = int ( $mb/1024 );
501 return "${mb}M" if $gb*1024 != $mb ;
503 my $tb = int ( $gb/1024 );
504 return "${gb}G" if $tb*1024 != $gb ;
509 sub parse_property_string
{
510 my ( $format, $data, $path, $additional_properties ) = @_ ;
512 # In property strings we default to not allowing additional properties
513 $additional_properties = 0 if ! defined ( $additional_properties );
515 # Support named formats here, too:
517 if ( my $desc = $format_list ->{ $format }) {
520 die "unknown format: $format\n " ;
522 } elsif ( ref ( $format ) ne 'HASH' ) {
523 die "unexpected format value of type " . ref ( $format ). " \n " ;
529 foreach my $part ( split ( /,/ , $data )) {
530 next if $part =~ /^\s*$/ ;
532 if ( $part =~ /^([^=]+)=(.+)$/ ) {
533 my ( $k, $v ) = ( $1, $2 );
534 die "duplicate key in comma-separated list property: $k\n " if defined ( $res ->{ $k });
535 my $schema = $format ->{ $k };
536 if ( my $alias = $schema ->{ alias
}) {
537 if ( my $key_alias = $schema ->{ keyAlias
}) {
538 die "key alias ' $key_alias ' is already defined \n " if defined ( $res ->{ $key_alias });
539 $res ->{ $key_alias } = $k ;
542 $schema = $format ->{ $k };
545 die "invalid key in comma-separated list property: $k\n " if ! $schema ;
546 if ( $schema ->{ type
} && $schema ->{ type
} eq 'boolean' ) {
547 $v = 1 if $v =~ m/^(1|on|yes|true)$/i ;
548 $v = 0 if $v =~ m/^(0|off|no|false)$/i ;
551 } elsif ( $part !~ /=/ ) {
552 die "duplicate key in comma-separated list property: $default_key\n " if $default_key ;
553 foreach my $key ( keys %$format ) {
554 if ( $format ->{ $key }->{ default_key
}) {
556 if (! $res ->{ $default_key }) {
557 $res ->{ $default_key } = $part ;
560 die "duplicate key in comma-separated list property: $default_key\n " ;
563 die "value without key, but schema does not define a default key \n " if ! $default_key ;
565 die "missing key in comma-separated list property \n " ;
570 check_object
( $path, $format, $res, $additional_properties, $errors );
571 if ( scalar ( %$errors )) {
572 raise
"format error \n " , errors
=> $errors ;
579 my ( $errors, $path, $msg ) = @_ ;
581 $path = '_root' if ! $path ;
583 if ( $errors ->{ $path }) {
584 $errors ->{ $path } = join ( ' \n ' , $errors ->{ $path }, $msg );
586 $errors ->{ $path } = $msg ;
593 # see 'man perlretut'
594 return $value =~ /^[+-]?(\d+\.\d+|\d+\.|\.\d+|\d+)([eE][+-]?\d+)?$/ ;
600 return $value =~ m/^[+-]?\d+$/ ;
604 my ( $path, $type, $value, $errors ) = @_ ;
608 if (! defined ( $value )) {
609 return 1 if $type eq 'null' ;
613 if ( my $tt = ref ( $type )) {
614 if ( $tt eq 'ARRAY' ) {
615 foreach my $t ( @$type ) {
617 check_type
( $path, $t, $value, $tmperr );
618 return 1 if ! scalar ( %$tmperr );
620 my $ttext = join ( '|' , @$type );
621 add_error
( $errors, $path, "type check (' $ttext ') failed" );
623 } elsif ( $tt eq 'HASH' ) {
625 check_prop
( $value, $type, $path, $tmperr );
626 return 1 if ! scalar ( %$tmperr );
627 add_error
( $errors, $path, "type check failed" );
630 die "internal error - got reference type ' $tt '" ;
635 return 1 if $type eq 'any' ;
637 if ( $type eq 'null' ) {
638 if ( defined ( $value )) {
639 add_error
( $errors, $path, "type check (' $type ') failed - value is not null" );
645 my $vt = ref ( $value );
647 if ( $type eq 'array' ) {
648 if (! $vt || $vt ne 'ARRAY' ) {
649 add_error
( $errors, $path, "type check (' $type ') failed" );
653 } elsif ( $type eq 'object' ) {
654 if (! $vt || $vt ne 'HASH' ) {
655 add_error
( $errors, $path, "type check (' $type ') failed" );
659 } elsif ( $type eq 'coderef' ) {
660 if (! $vt || $vt ne 'CODE' ) {
661 add_error
( $errors, $path, "type check (' $type ') failed" );
665 } elsif ( $type eq 'string' && $vt eq 'Regexp' ) {
666 # qr// regexes can be used as strings and make sense for format=regex
670 add_error
( $errors, $path, "type check (' $type ') failed - got $vt " );
673 if ( $type eq 'string' ) {
674 return 1 ; # nothing to check ?
675 } elsif ( $type eq 'boolean' ) {
676 #if ($value =~ m/^(1|true|yes|on)$/i) {
679 #} elsif ($value =~ m/^(0|false|no|off)$/i) {
680 } elsif ( $value eq '0' ) {
681 return 1 ; # return success (not value)
683 add_error
( $errors, $path, "type check (' $type ') failed - got ' $value '" );
686 } elsif ( $type eq 'integer' ) {
687 if (! is_integer
( $value )) {
688 add_error
( $errors, $path, "type check (' $type ') failed - got ' $value '" );
692 } elsif ( $type eq 'number' ) {
693 if (! is_number
( $value )) {
694 add_error
( $errors, $path, "type check (' $type ') failed - got ' $value '" );
699 return 1 ; # no need to verify unknown types
709 my ( $path, $schema, $value, $additional_properties, $errors ) = @_ ;
711 # print "Check Object " . Dumper($value) . "\nSchema: " . Dumper($schema);
713 my $st = ref ( $schema );
714 if (! $st || $st ne 'HASH' ) {
715 add_error
( $errors, $path, "Invalid schema definition." );
719 my $vt = ref ( $value );
720 if (! $vt || $vt ne 'HASH' ) {
721 add_error
( $errors, $path, "an object is required" );
725 foreach my $k ( keys %$schema ) {
726 check_prop
( $value ->{ $k }, $schema ->{ $k }, $path ?
" $path . $k " : $k, $errors );
729 foreach my $k ( keys %$value ) {
731 my $newpath = $path ?
" $path . $k " : $k ;
733 if ( my $subschema = $schema ->{ $k }) {
734 if ( my $requires = $subschema ->{ requires
}) {
735 if ( ref ( $requires )) {
736 #print "TEST: " . Dumper($value) . "\n", Dumper($requires) ;
737 check_prop
( $value, $requires, $path, $errors );
738 } elsif (! defined ( $value ->{ $requires })) {
739 add_error
( $errors, $path ?
" $path . $requires " : $requires,
740 "missing property - ' $newpath ' requires this property" );
744 next ; # value is already checked above
747 if ( defined ( $additional_properties ) && ! $additional_properties ) {
748 add_error
( $errors, $newpath, "property is not defined in schema " .
749 "and the schema does not allow additional properties" );
752 check_prop
( $value ->{ $k }, $additional_properties, $newpath, $errors )
753 if ref ( $additional_properties );
757 sub check_object_warn
{
758 my ( $path, $schema, $value, $additional_properties ) = @_ ;
760 check_object
( $path, $schema, $value, $additional_properties, $errors );
761 if ( scalar ( %$errors )) {
762 foreach my $k ( keys %$errors ) {
763 warn "parse error: $k : $errors ->{ $k } \n " ;
771 my ( $value, $schema, $path, $errors ) = @_ ;
773 die "internal error - no schema" if ! $schema ;
774 die "internal error" if ! $errors ;
776 #print "check_prop $path\n" if $value;
778 my $st = ref ( $schema );
779 if (! $st || $st ne 'HASH' ) {
780 add_error
( $errors, $path, "Invalid schema definition." );
784 # if it extends another schema, it must pass that schema as well
785 if ( $schema ->{ extends
}) {
786 check_prop
( $value, $schema ->{ extends
}, $path, $errors );
789 if (! defined ( $value )) {
790 return if $schema ->{ type
} && $schema ->{ type
} eq 'null' ;
791 if (! $schema ->{ optional
} && ! $schema ->{ alias
} && ! $schema ->{ group
}) {
792 add_error
( $errors, $path, "property is missing and it is not optional" );
797 return if ! check_type
( $path, $schema ->{ type
}, $value, $errors );
799 if ( $schema ->{ disallow
}) {
801 if ( check_type
( $path, $schema ->{ disallow
}, $value, $tmperr )) {
802 add_error
( $errors, $path, "disallowed value was matched" );
807 if ( my $vt = ref ( $value )) {
809 if ( $vt eq 'ARRAY' ) {
810 if ( $schema ->{ items
}) {
811 my $it = ref ( $schema ->{ items
});
812 if ( $it && $it eq 'ARRAY' ) {
813 #die "implement me $path: $vt " . Dumper($schema) ."\n". Dumper($value);
814 die "not implemented" ;
817 foreach my $el ( @$value ) {
818 check_prop
( $el, $schema ->{ items
}, "${path}[ $ind ]" , $errors );
824 } elsif ( $schema ->{ properties
} || $schema ->{ additionalProperties
}) {
825 check_object
( $path, defined ( $schema ->{ properties
}) ?
$schema ->{ properties
} : {},
826 $value, $schema ->{ additionalProperties
}, $errors );
832 if ( my $format = $schema ->{ format
}) {
833 eval { check_format
( $format, $value, $path ); };
835 add_error
( $errors, $path, "invalid format - $@ " );
840 if ( my $pattern = $schema ->{ pattern
}) {
841 if ( $value !~ m/^$pattern$/ ) {
842 add_error
( $errors, $path, "value does not match the regex pattern" );
847 if ( defined ( my $max = $schema ->{ maxLength
})) {
848 if ( length ( $value ) > $max ) {
849 add_error
( $errors, $path, "value may only be $max characters long" );
854 if ( defined ( my $min = $schema ->{ minLength
})) {
855 if ( length ( $value ) < $min ) {
856 add_error
( $errors, $path, "value must be at least $min characters long" );
861 if ( is_number
( $value )) {
862 if ( defined ( my $max = $schema ->{ maximum
})) {
864 add_error
( $errors, $path, "value must have a maximum value of $max " );
869 if ( defined ( my $min = $schema ->{ minimum
})) {
871 add_error
( $errors, $path, "value must have a minimum value of $min " );
877 if ( my $ea = $schema ->{ enum
}) {
880 foreach my $ev ( @$ea ) {
887 add_error
( $errors, $path, "value ' $value ' does not have a value in the enumeration '" .
888 join ( ", " , @$ea ) . "'" );
895 my ( $instance, $schema, $errmsg ) = @_ ;
898 $errmsg = "Parameter verification failed. \n " if ! $errmsg ;
900 # todo: cycle detection is only needed for debugging, I guess
901 # we can disable that in the final release
902 # todo: is there a better/faster way to detect cycles?
904 find_cycle
( $instance, sub { $cycles = 1 });
906 add_error
( $errors, undef , "data structure contains recursive cycles" );
908 check_prop
( $instance, $schema, '' , $errors );
911 if ( scalar ( %$errors )) {
912 raise
$errmsg, code
=> HTTP_BAD_REQUEST
, errors
=> $errors ;
918 my $schema_valid_types = [ "string" , "object" , "coderef" , "array" , "boolean" , "number" , "integer" , "null" , "any" ];
919 my $default_schema_noref = {
920 description
=> "This is the JSON Schema for JSON Schemas." ,
921 type
=> [ "object" ],
922 additionalProperties
=> 0 ,
925 type
=> [ "string" , "array" ],
926 description
=> "This is a type definition value. This can be a simple type, or a union type" ,
931 enum
=> $schema_valid_types,
933 enum
=> $schema_valid_types,
937 description
=> "This indicates that the instance property in the instance object is not required." ,
943 description
=> "This is a definition for the properties of an object value" ,
949 description
=> "When the value is an array, this indicates the schema to use to validate each item in an array" ,
953 additionalProperties
=> {
954 type
=> [ "boolean" , "object" ],
955 description
=> "This provides a default property definition for all properties that are not explicitly defined in an object type definition." ,
962 description
=> "This indicates the minimum value for the instance property when the type of the instance value is a number." ,
967 description
=> "This indicates the maximum value for the instance property when the type of the instance value is a number." ,
971 description
=> "When the instance value is a string, this indicates minimum length of the string" ,
978 description
=> "When the instance value is a string, this indicates maximum length of the string." ,
984 description
=> "A text representation of the type (used to generate documentation)." ,
989 description
=> "When the instance value is a string, this provides a regular expression that a instance string value should match in order to be valid." ,
996 description
=> "This provides an enumeration of possible values that are valid for the instance property." ,
1001 description
=> "This provides a description of the purpose the instance property. The value can be a string or it can be an object with properties corresponding to various different instance languages (with an optional default property indicating the default description)." ,
1003 verbose_description
=> {
1006 description
=> "This provides a more verbose description." ,
1008 format_description
=> {
1011 description
=> "This provides a shorter (usually just one word) description for a property used to generate descriptions for comma separated list property strings." ,
1016 description
=> "This provides the title of the property" ,
1019 type
=> [ "string" , "object" ],
1021 description
=> "indicates a required property or a schema that must be validated if this property is present" ,
1024 type
=> [ "string" , "object" ],
1026 description
=> "This indicates what format the data is among some predefined formats which may include: \n\n date - a string following the ISO format \n address \n schema - a schema definition object \n person \n page \n html - a string representing HTML" ,
1031 description
=> "Whether this is the default key in a comma separated list property string." ,
1036 description
=> "When a key represents the same property as another it can be an alias to it, causing the parsed datastructure to use the other key to store the current value under." ,
1041 description
=> "Allows to store the current 'key' as value of another property. Only valid if used together with 'alias'." ,
1042 requires
=> 'alias' ,
1047 description
=> "This indicates the default for the instance property."
1051 description
=> "Bash completion function. This function should return a list of possible values." ,
1057 description
=> "This attribute may take the same values as the \" type \" attribute, however if the instance matches the type or if this value is an array and the instance matches any type or schema in the array, then this instance is not valid." ,
1062 description
=> "This indicates the schema extends the given schema. All instances of this schema must be valid to by the extended schema also." ,
1065 # this is from hyper schema
1068 description
=> "This defines the link relations of the instance objects" ,
1075 description
=> "This defines the target URL for the relation and can be parameterized using {propertyName} notation. It should be resolved as a URI-reference relative to the URI that was used to retrieve the instance document" ,
1079 description
=> "This is the name of the link relation" ,
1085 description
=> "For submission links, this defines the method that should be used to access the target resource" ,
1095 my $default_schema = Storable
:: dclone
( $default_schema_noref );
1097 $default_schema ->{ properties
}->{ properties
}->{ additionalProperties
} = $default_schema ;
1098 $default_schema ->{ properties
}->{ additionalProperties
}->{ properties
} = $default_schema ->{ properties
};
1100 $default_schema ->{ properties
}->{ items
}->{ properties
} = $default_schema ->{ properties
};
1101 $default_schema ->{ properties
}->{ items
}->{ additionalProperties
} = 0 ;
1103 $default_schema ->{ properties
}->{ disallow
}->{ properties
} = $default_schema ->{ properties
};
1104 $default_schema ->{ properties
}->{ disallow
}->{ additionalProperties
} = 0 ;
1106 $default_schema ->{ properties
}->{ requires
}->{ properties
} = $default_schema ->{ properties
};
1107 $default_schema ->{ properties
}->{ requires
}->{ additionalProperties
} = 0 ;
1109 $default_schema ->{ properties
}->{ extends
}->{ properties
} = $default_schema ->{ properties
};
1110 $default_schema ->{ properties
}->{ extends
}->{ additionalProperties
} = 0 ;
1112 my $method_schema = {
1114 additionalProperties
=> 0 ,
1117 description
=> "This a description of the method" ,
1122 description
=> "This indicates the name of the function to call." ,
1125 additionalProperties
=> 1 ,
1140 description
=> "The HTTP method name." ,
1141 enum
=> [ 'GET' , 'POST' , 'PUT' , 'DELETE' ],
1146 description
=> "Method needs special privileges - only pvedaemon can execute it" ,
1151 description
=> "A parameter name. If specified, all calls to this method are proxied to the host contained in that parameter." ,
1156 description
=> "Required access permissions. By default only 'root' is allowed to access this method." ,
1158 additionalProperties
=> 0 ,
1161 description
=> "Describe access permissions." ,
1165 description
=> "A simply way to allow access for 'all' authenticated users. Value 'world' is used to allow access without credentials." ,
1167 enum
=> [ 'all' , 'world' ],
1171 description
=> "Array of permission checks (prefix notation)." ,
1178 description
=> "Used internally" ,
1182 description
=> "Used internally" ,
1187 description
=> "path for URL matching (uri template)" ,
1189 fragmentDelimiter
=> {
1191 description
=> "A ways to override the default fragment delimiter '/'. This onyl works on a whole sub-class. You can set this to the empty string to match the whole rest of the URI." ,
1196 description
=> "JSON Schema for parameters." ,
1201 description
=> "JSON Schema for return value." ,
1206 description
=> "method implementaion (code reference)" ,
1211 description
=> "Delegate call to this class (perl class string)." ,
1214 additionalProperties
=> 0 ,
1220 fragmentDelimiter
=> { optional
=> 1 }
1228 sub validate_schema
{
1231 my $errmsg = "internal error - unable to verify schema \n " ;
1232 validate
( $schema, $default_schema, $errmsg );
1235 sub validate_method_info
{
1238 my $errmsg = "internal error - unable to verify method info \n " ;
1239 validate
( $info, $method_schema, $errmsg );
1241 validate_schema
( $info ->{ parameters
}) if $info ->{ parameters
};
1242 validate_schema
( $info ->{ returns
}) if $info ->{ returns
};
1245 # run a self test on load
1246 # make sure we can verify the default schema
1247 validate_schema
( $default_schema_noref );
1248 validate_schema
( $method_schema );
1250 # and now some utility methods (used by pve api)
1251 sub method_get_child_link
{
1254 return undef if ! $info ;
1256 my $schema = $info ->{ returns
};
1257 return undef if ! $schema || ! $schema ->{ type
} || $schema ->{ type
} ne 'array' ;
1259 my $links = $schema ->{ links
};
1260 return undef if ! $links ;
1263 foreach my $lnk ( @$links ) {
1264 if ( $lnk ->{ href
} && $lnk ->{ rel
} && ( $lnk ->{ rel
} eq 'child' )) {
1273 # a way to parse command line parameters, using a
1274 # schema to configure Getopt::Long
1276 my ( $schema, $args, $arg_param, $fixed_param, $pwcallback ) = @_ ;
1278 if (! $schema || ! $schema ->{ properties
}) {
1279 raise
( "too many arguments \n " , code
=> HTTP_BAD_REQUEST
)
1280 if scalar ( @$args ) != 0 ;
1285 if ( $arg_param && ! ref ( $arg_param )) {
1286 my $pd = $schema ->{ properties
}->{ $arg_param };
1287 die "expected list format $pd ->{format}"
1288 if !( $pd && $pd ->{ format
} && $pd ->{ format
} =~ m/-list/ );
1289 $list_param = $arg_param ;
1293 foreach my $prop ( keys %{ $schema ->{ properties
}}) {
1294 my $pd = $schema ->{ properties
}->{ $prop };
1295 next if $list_param && $prop eq $list_param ;
1296 next if defined ( $fixed_param ->{ $prop });
1298 if ( $prop eq 'password' && $pwcallback ) {
1299 # we do not accept plain password on input line, instead
1300 # we turn this into a boolean option and ask for password below
1301 # using $pwcallback() (for security reasons).
1302 push @getopt, " $prop " ;
1303 } elsif ( $pd ->{ type
} eq 'boolean' ) {
1304 push @getopt, " $prop :s" ;
1306 if ( $pd ->{ format
} && $pd ->{ format
} =~ m/-a?list/ ) {
1307 push @getopt, " $prop =s@" ;
1309 push @getopt, " $prop =s" ;
1314 Getopt
:: Long
:: Configure
( 'prefix_pattern=(--|-)' );
1317 raise
( "unable to parse option \n " , code
=> HTTP_BAD_REQUEST
)
1318 if ! Getopt
:: Long
:: GetOptionsFromArray
( $args, $opts, @getopt );
1322 $opts ->{ $list_param } = $args ;
1324 } elsif ( ref ( $arg_param )) {
1325 foreach my $arg_name ( @$arg_param ) {
1326 if ( $opts ->{ 'extra-args' }) {
1327 raise
( "internal error: extra-args must be the last argument \n " , code
=> HTTP_BAD_REQUEST
);
1329 if ( $arg_name eq 'extra-args' ) {
1330 $opts ->{ 'extra-args' } = $args ;
1334 raise
( "not enough arguments \n " , code
=> HTTP_BAD_REQUEST
) if ! @$args ;
1335 $opts ->{ $arg_name } = shift @$args ;
1337 raise
( "too many arguments \n " , code
=> HTTP_BAD_REQUEST
) if @$args ;
1339 raise
( "too many arguments \n " , code
=> HTTP_BAD_REQUEST
)
1340 if scalar ( @$args ) != 0 ;
1344 if ( my $pd = $schema ->{ properties
}->{ password
}) {
1345 if ( $pd ->{ type
} ne 'boolean' && $pwcallback ) {
1346 if ( $opts ->{ password
} || ! $pd ->{ optional
}) {
1347 $opts ->{ password
} = & $pwcallback ();
1352 $opts = PVE
:: Tools
:: decode_utf8_parameters
( $opts );
1354 foreach my $p ( keys %$opts ) {
1355 if ( my $pd = $schema ->{ properties
}->{ $p }) {
1356 if ( $pd ->{ type
} eq 'boolean' ) {
1357 if ( $opts ->{ $p } eq '' ) {
1359 } elsif ( $opts ->{ $p } =~ m/^(1|true|yes|on)$/i ) {
1361 } elsif ( $opts ->{ $p } =~ m/^(0|false|no|off)$/i ) {
1364 raise
( "unable to parse boolean option \n " , code
=> HTTP_BAD_REQUEST
);
1366 } elsif ( $pd ->{ format
}) {
1368 if ( $pd ->{ format
} =~ m/-list/ ) {
1369 # allow --vmid 100 --vmid 101 and --vmid 100,101
1370 # allow --dow mon --dow fri and --dow mon,fri
1371 $opts ->{ $p } = join ( "," , @{ $opts ->{ $p }}) if ref ( $opts ->{ $p }) eq 'ARRAY' ;
1372 } elsif ( $pd ->{ format
} =~ m/-alist/ ) {
1373 # we encode array as \0 separated strings
1374 # Note: CGI.pm also use this encoding
1375 if ( scalar (@{ $opts ->{ $p }}) != 1 ) {
1376 $opts ->{ $p } = join ( "\0" , @{ $opts ->{ $p }});
1378 # st that split_list knows it is \0 terminated
1379 my $v = $opts ->{ $p }->[ 0 ];
1380 $opts ->{ $p } = " $v\0 " ;
1387 foreach my $p ( keys %$fixed_param ) {
1388 $opts ->{ $p } = $fixed_param ->{ $p };
1394 # A way to parse configuration data by giving a json schema
1396 my ( $schema, $filename, $raw ) = @_ ;
1398 # do fast check (avoid validate_schema($schema))
1399 die "got strange schema" if ! $schema ->{ type
} ||
1400 ! $schema ->{ properties
} || $schema ->{ type
} ne 'object' ;
1404 while ( $raw =~ /^\s*(.+?)\s*$/gm ) {
1407 next if $line =~ /^#/ ;
1409 if ( $line =~ m/^(\S+?):\s*(.*)$/ ) {
1412 if ( $schema ->{ properties
}->{ $key } &&
1413 $schema ->{ properties
}->{ $key }->{ type
} eq 'boolean' ) {
1415 $value = 1 if $value =~ m/^(1|on|yes|true)$/i ;
1416 $value = 0 if $value =~ m/^(0|off|no|false)$/i ;
1418 $cfg ->{ $key } = $value ;
1420 warn "ignore config line: $line\n "
1425 check_prop
( $cfg, $schema, '' , $errors );
1427 foreach my $k ( keys %$errors ) {
1428 warn "parse error in ' $filename ' - ' $k ': $errors ->{ $k } \n " ;
1435 # generate simple key/value file
1437 my ( $schema, $filename, $cfg ) = @_ ;
1439 # do fast check (avoid validate_schema($schema))
1440 die "got strange schema" if ! $schema ->{ type
} ||
1441 ! $schema ->{ properties
} || $schema ->{ type
} ne 'object' ;
1443 validate
( $cfg, $schema, "validation error in ' $filename ' \n " );
1447 foreach my $k ( keys %$cfg ) {
1448 $data .= " $k : $cfg ->{ $k } \n " ;
1454 # helpers used to generate our manual pages
1456 my $find_schema_default_key = sub {
1460 my $keyAliasProps = {};
1462 foreach my $key ( keys %$format ) {
1463 my $phash = $format ->{ $key };
1464 if ( $phash ->{ default_key
}) {
1465 die "multiple default keys in schema ( $default_key, $key ) \n "
1466 if defined ( $default_key );
1467 die "default key ' $key ' is an alias - this is not allowed \n "
1468 if defined ( $phash ->{ alias
});
1469 die "default key ' $key ' with keyAlias attribute is not allowed \n "
1470 if $phash ->{ keyAlias
};
1471 $default_key = $key ;
1473 my $key_alias = $phash ->{ keyAlias
};
1474 die "found keyAlias without 'alias definition for ' $key ' \n "
1475 if $key_alias && ! $phash ->{ alias
};
1477 if ( $phash ->{ alias
} && $key_alias ) {
1478 die "inconsistent keyAlias ' $key_alias ' definition"
1479 if defined ( $keyAliasProps ->{ $key_alias }) &&
1480 $keyAliasProps ->{ $key_alias } ne $phash ->{ alias
};
1481 $keyAliasProps ->{ $key_alias } = $phash ->{ alias
};
1485 return wantarray ?
( $default_key, $keyAliasProps ) : $default_key ;
1488 sub generate_typetext
{
1489 my ( $format, $list_enums ) = @_ ;
1491 my ( $default_key, $keyAliasProps ) = & $find_schema_default_key ( $format );
1496 my $add_option_string = sub {
1497 my ( $text, $optional ) = @_ ;
1503 $text = "[ $text ]" if $optional ;
1508 my $format_key_value = sub {
1509 my ( $key, $phash ) = @_ ;
1511 die "internal error" if defined ( $phash ->{ alias
});
1517 if ( my $desc = $phash ->{ format_description
}) {
1518 $typetext .= "< $desc >" ;
1519 } elsif ( my $text = $phash ->{ typetext
}) {
1521 } elsif ( my $enum = $phash ->{ enum
}) {
1522 if ( $list_enums || ( scalar ( @$enum ) <= 3 )) {
1523 $typetext .= '<' . join ( '|' , @$enum ) . '>' ;
1525 $typetext .= '<enum>' ;
1527 } elsif ( $phash ->{ type
} eq 'boolean' ) {
1528 $typetext .= '<1|0>' ;
1529 } elsif ( $phash ->{ type
} eq 'integer' ) {
1530 $typetext .= '<integer>' ;
1531 } elsif ( $phash ->{ type
} eq 'number' ) {
1532 $typetext .= '<number>' ;
1534 die "internal error: neither format_description nor typetext found for option ' $key '" ;
1537 if ( defined ( $default_key ) && ( $default_key eq $key )) {
1538 & $add_option_string ( "[ $keytext =] $typetext " , $phash ->{ optional
});
1540 & $add_option_string ( " $keytext = $typetext " , $phash ->{ optional
});
1546 my $cond_add_key = sub {
1549 return if $done ->{ $key }; # avoid duplicates
1553 my $phash = $format ->{ $key };
1555 return if ! $phash ; # should not happen
1557 return if $phash ->{ alias
};
1559 & $format_key_value ( $key, $phash );
1563 & $cond_add_key ( $default_key ) if defined ( $default_key );
1565 # add required keys first
1566 foreach my $key ( sort keys %$format ) {
1567 my $phash = $format ->{ $key };
1568 & $cond_add_key ( $key ) if $phash && ! $phash ->{ optional
};
1572 foreach my $key ( sort keys %$format ) {
1573 & $cond_add_key ( $key );
1576 foreach my $keyAlias ( sort keys %$keyAliasProps ) {
1577 & $add_option_string ( "< $keyAlias >=< $keyAliasProps ->{ $keyAlias }>" , 1 );
1583 sub print_property_string
{
1584 my ( $data, $format, $skip, $path ) = @_ ;
1586 if ( ref ( $format ) ne 'HASH' ) {
1587 my $schema = get_format
( $format );
1588 die "not a valid format: $format\n " if ! $schema ;
1593 check_object
( $path, $format, $data, undef , $errors );
1594 if ( scalar ( %$errors )) {
1595 raise
"format error" , errors
=> $errors ;
1598 my ( $default_key, $keyAliasProps ) = & $find_schema_default_key ( $format );
1603 my $add_option_string = sub {
1606 $res .= ',' if $add_sep ;
1611 my $format_value = sub {
1612 my ( $key, $value, $format ) = @_ ;
1614 if ( defined ( $format ) && ( $format eq 'disk-size' )) {
1615 return format_size
( $value );
1617 die "illegal value with commas for $key\n " if $value =~ /,/ ;
1622 my $done = { map { $_ => 1 } @$skip };
1624 my $cond_add_key = sub {
1625 my ( $key, $isdefault ) = @_ ;
1627 return if $done ->{ $key }; # avoid duplicates
1631 my $value = $data ->{ $key };
1633 return if ! defined ( $value );
1635 my $phash = $format ->{ $key };
1637 # try to combine values if we have key aliases
1638 if ( my $combine = $keyAliasProps ->{ $key }) {
1639 if ( defined ( my $combine_value = $data ->{ $combine })) {
1640 my $combine_format = $format ->{ $combine }->{ format
};
1641 my $value_str = & $format_value ( $key, $value, $phash ->{ format
});
1642 my $combine_str = & $format_value ( $combine, $combine_value, $combine_format );
1643 & $add_option_string ( "${value_str}=${combine_str}" );
1644 $done ->{ $combine } = 1 ;
1649 if ( $phash && $phash ->{ alias
}) {
1650 $phash = $format ->{ $phash ->{ alias
}};
1653 die "invalid key ' $key ' \n " if ! $phash ;
1654 die "internal error" if defined ( $phash ->{ alias
});
1656 my $value_str = & $format_value ( $key, $value, $phash ->{ format
});
1658 & $add_option_string ( $value_str );
1660 & $add_option_string ( " $key =${value_str}" );
1664 # add default key first
1665 & $cond_add_key ( $default_key, 1 ) if defined ( $default_key );
1667 # add required keys first
1668 foreach my $key ( sort keys %$data ) {
1669 my $phash = $format ->{ $key };
1670 & $cond_add_key ( $key ) if $phash && ! $phash ->{ optional
};
1674 foreach my $key ( sort keys %$data ) {
1675 & $cond_add_key ( $key );
1681 sub schema_get_type_text
{
1682 my ( $phash, $style ) = @_ ;
1684 my $type = $phash ->{ type
} || 'string' ;
1686 if ( $phash ->{ typetext
}) {
1687 return $phash ->{ typetext
};
1688 } elsif ( $phash ->{ format_description
}) {
1689 return "< $phash ->{format_description}>" ;
1690 } elsif ( $phash ->{ enum
}) {
1691 return "<" . join ( ' | ' , sort @{ $phash ->{ enum
}}) . ">" ;
1692 } elsif ( $phash ->{ pattern
}) {
1693 return $phash ->{ pattern
};
1694 } elsif ( $type eq 'integer' || $type eq 'number' ) {
1695 # NOTE: always access values as number (avoid converion to string)
1696 if ( defined ( $phash ->{ minimum
}) && defined ( $phash ->{ maximum
})) {
1697 return "< $type > (" . ( $phash ->{ minimum
} + 0 ) . " - " .
1698 ( $phash ->{ maximum
} + 0 ) . ")" ;
1699 } elsif ( defined ( $phash ->{ minimum
})) {
1700 return "< $type > (" . ( $phash ->{ minimum
} + 0 ) . " - N)" ;
1701 } elsif ( defined ( $phash ->{ maximum
})) {
1702 return "< $type > (-N - " . ( $phash ->{ maximum
} + 0 ) . ")" ;
1704 } elsif ( $type eq 'string' ) {
1705 if ( my $format = $phash ->{ format
}) {
1706 $format = get_format
( $format ) if ref ( $format ) ne 'HASH' ;
1707 if ( ref ( $format ) eq 'HASH' ) {
1709 $list_enums = 1 if $style && $style eq 'config-sub' ;
1710 return generate_typetext
( $format, $list_enums );