]>
git.proxmox.com Git - pve-common.git/blob - src/PVE/JSONSchema.pm
f6ee161957dfc096b438d378d45bb4617cbd3188
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
( 'extra-args' , {
88 description
=> "Extra arguments as array" ,
90 items
=> { type
=> 'string' },
97 my ( $format, $code ) = @_ ;
99 die "JSON schema format ' $format ' already registered \n "
100 if $format_list ->{ $format };
102 $format_list ->{ $format } = $code ;
107 return $format_list ->{ $format };
110 # register some common type for pve
112 register_format
( 'string' , sub {}); # allow format => 'string-list'
114 register_format
( 'urlencoded' , \
& pve_verify_urlencoded
);
115 sub pve_verify_urlencoded
{
116 my ( $text, $noerr ) = @_ ;
117 if ( $text !~ /^[-%a-zA-Z0-9_.!~*'()]*$/ ) {
118 return undef if $noerr ;
119 die "invalid urlencoded string: $text\n " ;
124 register_format
( 'pve-configid' , \
& pve_verify_configid
);
125 sub pve_verify_configid
{
126 my ( $id, $noerr ) = @_ ;
128 if ( $id !~ m/^[a-z][a-z0-9_]+$/i ) {
129 return undef if $noerr ;
130 die "invalid configuration ID ' $id ' \n " ;
135 PVE
:: JSONSchema
:: register_format
( 'pve-storage-id' , \
& parse_storage_id
);
136 sub parse_storage_id
{
137 my ( $storeid, $noerr ) = @_ ;
139 if ( $storeid !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i ) {
140 return undef if $noerr ;
141 die "storage ID ' $storeid ' contains illegal characters \n " ;
147 register_format
( 'pve-vmid' , \
& pve_verify_vmid
);
148 sub pve_verify_vmid
{
149 my ( $vmid, $noerr ) = @_ ;
151 if ( $vmid !~ m/^[1-9][0-9]{2,8}$/ ) {
152 return undef if $noerr ;
153 die "value does not look like a valid VM ID \n " ;
158 register_format
( 'pve-node' , \
& pve_verify_node_name
);
159 sub pve_verify_node_name
{
160 my ( $node, $noerr ) = @_ ;
162 if ( $node !~ m/^([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)$/ ) {
163 return undef if $noerr ;
164 die "value does not look like a valid node name \n " ;
169 register_format
( 'ipv4' , \
& pve_verify_ipv4
);
170 sub pve_verify_ipv4
{
171 my ( $ipv4, $noerr ) = @_ ;
173 if ( $ipv4 !~ m/^(?:$IPV4RE)$/ ) {
174 return undef if $noerr ;
175 die "value does not look like a valid IPv4 address \n " ;
180 register_format
( 'ipv6' , \
& pve_verify_ipv6
);
181 sub pve_verify_ipv6
{
182 my ( $ipv6, $noerr ) = @_ ;
184 if ( $ipv6 !~ m/^(?:$IPV6RE)$/ ) {
185 return undef if $noerr ;
186 die "value does not look like a valid IPv6 address \n " ;
191 register_format
( 'ip' , \
& pve_verify_ip
);
193 my ( $ip, $noerr ) = @_ ;
195 if ( $ip !~ m/^(?:(?:$IPV4RE)|(?:$IPV6RE))$/ ) {
196 return undef if $noerr ;
197 die "value does not look like a valid IP address \n " ;
202 my $ipv4_mask_hash = {
219 '255.255.128.0' => 17 ,
220 '255.255.192.0' => 18 ,
221 '255.255.224.0' => 19 ,
222 '255.255.240.0' => 20 ,
223 '255.255.248.0' => 21 ,
224 '255.255.252.0' => 22 ,
225 '255.255.254.0' => 23 ,
226 '255.255.255.0' => 24 ,
227 '255.255.255.128' => 25 ,
228 '255.255.255.192' => 26 ,
229 '255.255.255.224' => 27 ,
230 '255.255.255.240' => 28 ,
231 '255.255.255.248' => 29 ,
232 '255.255.255.252' => 30
235 register_format
( 'ipv4mask' , \
& pve_verify_ipv4mask
);
236 sub pve_verify_ipv4mask
{
237 my ( $mask, $noerr ) = @_ ;
239 if (! defined ( $ipv4_mask_hash ->{ $mask })) {
240 return undef if $noerr ;
241 die "value does not look like a valid IP netmask \n " ;
246 register_format
( 'CIDRv6' , \
& pve_verify_cidrv6
);
247 sub pve_verify_cidrv6
{
248 my ( $cidr, $noerr ) = @_ ;
250 if ( $cidr =~ m!^(?:$IPV6RE)(?:/(\d+))$! && ( $1 > 7 ) && ( $1 <= 120 )) {
254 return undef if $noerr ;
255 die "value does not look like a valid IPv6 CIDR network \n " ;
258 register_format
( 'CIDRv4' , \
& pve_verify_cidrv4
);
259 sub pve_verify_cidrv4
{
260 my ( $cidr, $noerr ) = @_ ;
262 if ( $cidr =~ m!^(?:$IPV4RE)(?:/(\d+))$! && ( $1 > 7 ) && ( $1 <= 32 )) {
266 return undef if $noerr ;
267 die "value does not look like a valid IPv4 CIDR network \n " ;
270 register_format
( 'CIDR' , \
& pve_verify_cidr
);
271 sub pve_verify_cidr
{
272 my ( $cidr, $noerr ) = @_ ;
274 if (!( pve_verify_cidrv4
( $cidr, 1 ) ||
275 pve_verify_cidrv6
( $cidr, 1 )))
277 return undef if $noerr ;
278 die "value does not look like a valid CIDR network \n " ;
284 register_format
( 'pve-ipv4-config' , \
& pve_verify_ipv4_config
);
285 sub pve_verify_ipv4_config
{
286 my ( $config, $noerr ) = @_ ;
288 return $config if $config =~ /^(?:dhcp|manual)$/ ||
289 pve_verify_cidrv4
( $config, 1 );
290 return undef if $noerr ;
291 die "value does not look like a valid ipv4 network configuration \n " ;
294 register_format
( 'pve-ipv6-config' , \
& pve_verify_ipv6_config
);
295 sub pve_verify_ipv6_config
{
296 my ( $config, $noerr ) = @_ ;
298 return $config if $config =~ /^(?:auto|dhcp|manual)$/ ||
299 pve_verify_cidrv6
( $config, 1 );
300 return undef if $noerr ;
301 die "value does not look like a valid ipv6 network configuration \n " ;
304 register_format
( 'email' , \
& pve_verify_email
);
305 sub pve_verify_email
{
306 my ( $email, $noerr ) = @_ ;
308 # we use same regex as in Utils.js
309 if ( $email !~ /^(\w+)([\-+.][\w]+)*@(\w[\-\w]*\.){1,5}([A-Za-z]){2,63}$/ ) {
310 return undef if $noerr ;
311 die "value does not look like a valid email address \n " ;
316 register_format
( 'dns-name' , \
& pve_verify_dns_name
);
317 sub pve_verify_dns_name
{
318 my ( $name, $noerr ) = @_ ;
320 my $namere = "([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)" ;
322 if ( $name !~ /^(${namere}\.)*${namere}$/ ) {
323 return undef if $noerr ;
324 die "value does not look like a valid DNS name \n " ;
329 # network interface name
330 register_format
( 'pve-iface' , \
& pve_verify_iface
);
331 sub pve_verify_iface
{
332 my ( $id, $noerr ) = @_ ;
334 if ( $id !~ m/^[a-z][a-z0-9_]{1,20}([:\.]\d+)?$/i ) {
335 return undef if $noerr ;
336 die "invalid network interface name ' $id ' \n " ;
341 # general addresses by name or IP
342 register_format
( 'address' , \
& pve_verify_address
);
343 sub pve_verify_address
{
344 my ( $addr, $noerr ) = @_ ;
346 if (!( pve_verify_ip
( $addr, 1 ) ||
347 pve_verify_dns_name
( $addr, 1 )))
349 return undef if $noerr ;
350 die "value does not look like a valid address: $addr\n " ;
355 register_format
( 'disk-size' , \
& pve_verify_disk_size
);
356 sub pve_verify_disk_size
{
357 my ( $size, $noerr ) = @_ ;
358 if (! defined ( parse_size
( $size ))) {
359 return undef if $noerr ;
360 die "value does not look like a valid disk size: $size\n " ;
365 register_standard_option
( 'spice-proxy' , {
366 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)." ,
367 type
=> 'string' , format
=> 'address' ,
370 register_standard_option
( 'remote-viewer-config' , {
371 description
=> "Returned values can be directly passed to the 'remote-viewer' application." ,
372 additionalProperties
=> 1 ,
374 type
=> { type
=> 'string' },
375 password
=> { type
=> 'string' },
376 proxy
=> { type
=> 'string' },
377 host
=> { type
=> 'string' },
378 'tls-port' => { type
=> 'integer' },
382 register_format
( 'pve-startup-order' , \
& pve_verify_startup_order
);
383 sub pve_verify_startup_order
{
384 my ( $value, $noerr ) = @_ ;
386 return $value if pve_parse_startup_order
( $value );
388 return undef if $noerr ;
390 die "unable to parse startup options \n " ;
393 sub pve_parse_startup_order
{
396 return undef if ! $value ;
400 foreach my $p ( split ( /,/ , $value )) {
401 next if $p =~ m/^\s*$/ ;
403 if ( $p =~ m/^(order=)?(\d+)$/ ) {
405 } elsif ( $p =~ m/^up=(\d+)$/ ) {
407 } elsif ( $p =~ m/^down=(\d+)$/ ) {
417 PVE
:: JSONSchema
:: register_standard_option
( 'pve-startup-order' , {
418 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." ,
420 type
=> 'string' , format
=> 'pve-startup-order' ,
421 typetext
=> '[[order=]\d+] [,up=\d+] [,down=\d+] ' ,
425 my ( $format, $value, $path ) = @_ ;
427 return parse_property_string
( $format, $value, $path ) if ref ( $format ) eq 'HASH' ;
428 return if $format eq 'regex' ;
430 if ( $format =~ m/^(.*)-a?list$/ ) {
432 my $code = $format_list ->{ $1 };
434 die "undefined format ' $format ' \n " if ! $code ;
436 # Note: we allow empty lists
437 foreach my $v ( split_list
( $value )) {
441 } elsif ( $format =~ m/^(.*)-opt$/ ) {
443 my $code = $format_list ->{ $1 };
445 die "undefined format ' $format ' \n " if ! $code ;
447 return if ! $value ; # allow empty string
453 my $code = $format_list ->{ $format };
455 die "undefined format ' $format ' \n " if ! $code ;
457 return parse_property_string
( $code, $value, $path ) if ref ( $code ) eq 'HASH' ;
465 return undef if $value !~ m/^(\d+(\.\d+)?)([KMGT])?$/ ;
466 my ( $size, $unit ) = ( $1, $3 );
469 $size = $size * 1024 ;
470 } elsif ( $unit eq 'M' ) {
471 $size = $size * 1024 * 1024 ;
472 } elsif ( $unit eq 'G' ) {
473 $size = $size * 1024 * 1024 * 1024 ;
474 } elsif ( $unit eq 'T' ) {
475 $size = $size * 1024 * 1024 * 1024 * 1024 ;
486 my $kb = int ( $size/1024 );
487 return $size if $kb*1024 != $size ;
489 my $mb = int ( $kb/1024 );
490 return "${kb}K" if $mb*1024 != $kb ;
492 my $gb = int ( $mb/1024 );
493 return "${mb}M" if $gb*1024 != $mb ;
495 my $tb = int ( $gb/1024 );
496 return "${gb}G" if $tb*1024 != $gb ;
501 sub parse_property_string
{
502 my ( $format, $data, $path, $additional_properties ) = @_ ;
504 # In property strings we default to not allowing additional properties
505 $additional_properties = 0 if ! defined ( $additional_properties );
510 foreach my $part ( split ( /,/ , $data )) {
511 next if $part =~ /^\s*$/ ;
513 if ( $part =~ /^([^=]+)=(.+)$/ ) {
514 my ( $k, $v ) = ( $1, $2 );
515 die "duplicate key in comma-separated list property: $k\n " if defined ( $res ->{ $k });
516 my $schema = $format ->{ $k };
517 if ( my $alias = $schema ->{ alias
}) {
518 if ( my $key_alias = $schema ->{ keyAlias
}) {
519 die "key alias ' $key_alias ' is already defined \n " if defined ( $res ->{ $key_alias });
520 $res ->{ $key_alias } = $k ;
523 $schema = $format ->{ $k };
526 die "invalid key in comma-separated list property: $k\n " if ! $schema ;
527 if ( $schema ->{ type
} && $schema ->{ type
} eq 'boolean' ) {
528 $v = 1 if $v =~ m/^(1|on|yes|true)$/i ;
529 $v = 0 if $v =~ m/^(0|off|no|false)$/i ;
532 } elsif ( $part !~ /=/ ) {
533 die "duplicate key in comma-separated list property: $default_key\n " if $default_key ;
534 foreach my $key ( keys %$format ) {
535 if ( $format ->{ $key }->{ default_key
}) {
537 if (! $res ->{ $default_key }) {
538 $res ->{ $default_key } = $part ;
541 die "duplicate key in comma-separated list property: $default_key\n " ;
544 die "value without key, but schema does not define a default key \n " if ! $default_key ;
546 die "missing key in comma-separated list property \n " ;
551 check_object
( $path, $format, $res, $additional_properties, $errors );
552 if ( scalar ( %$errors )) {
553 raise
"format error \n " , errors
=> $errors ;
560 my ( $errors, $path, $msg ) = @_ ;
562 $path = '_root' if ! $path ;
564 if ( $errors ->{ $path }) {
565 $errors ->{ $path } = join ( ' \n ' , $errors ->{ $path }, $msg );
567 $errors ->{ $path } = $msg ;
574 # see 'man perlretut'
575 return $value =~ /^[+-]?(\d+\.\d+|\d+\.|\.\d+|\d+)([eE][+-]?\d+)?$/ ;
581 return $value =~ m/^[+-]?\d+$/ ;
585 my ( $path, $type, $value, $errors ) = @_ ;
589 if (! defined ( $value )) {
590 return 1 if $type eq 'null' ;
594 if ( my $tt = ref ( $type )) {
595 if ( $tt eq 'ARRAY' ) {
596 foreach my $t ( @$type ) {
598 check_type
( $path, $t, $value, $tmperr );
599 return 1 if ! scalar ( %$tmperr );
601 my $ttext = join ( '|' , @$type );
602 add_error
( $errors, $path, "type check (' $ttext ') failed" );
604 } elsif ( $tt eq 'HASH' ) {
606 check_prop
( $value, $type, $path, $tmperr );
607 return 1 if ! scalar ( %$tmperr );
608 add_error
( $errors, $path, "type check failed" );
611 die "internal error - got reference type ' $tt '" ;
616 return 1 if $type eq 'any' ;
618 if ( $type eq 'null' ) {
619 if ( defined ( $value )) {
620 add_error
( $errors, $path, "type check (' $type ') failed - value is not null" );
626 my $vt = ref ( $value );
628 if ( $type eq 'array' ) {
629 if (! $vt || $vt ne 'ARRAY' ) {
630 add_error
( $errors, $path, "type check (' $type ') failed" );
634 } elsif ( $type eq 'object' ) {
635 if (! $vt || $vt ne 'HASH' ) {
636 add_error
( $errors, $path, "type check (' $type ') failed" );
640 } elsif ( $type eq 'coderef' ) {
641 if (! $vt || $vt ne 'CODE' ) {
642 add_error
( $errors, $path, "type check (' $type ') failed" );
648 add_error
( $errors, $path, "type check (' $type ') failed - got $vt " );
651 if ( $type eq 'string' ) {
652 return 1 ; # nothing to check ?
653 } elsif ( $type eq 'boolean' ) {
654 #if ($value =~ m/^(1|true|yes|on)$/i) {
657 #} elsif ($value =~ m/^(0|false|no|off)$/i) {
658 } elsif ( $value eq '0' ) {
661 add_error
( $errors, $path, "type check (' $type ') failed - got ' $value '" );
664 } elsif ( $type eq 'integer' ) {
665 if (! is_integer
( $value )) {
666 add_error
( $errors, $path, "type check (' $type ') failed - got ' $value '" );
670 } elsif ( $type eq 'number' ) {
671 if (! is_number
( $value )) {
672 add_error
( $errors, $path, "type check (' $type ') failed - got ' $value '" );
677 return 1 ; # no need to verify unknown types
687 my ( $path, $schema, $value, $additional_properties, $errors ) = @_ ;
689 # print "Check Object " . Dumper($value) . "\nSchema: " . Dumper($schema);
691 my $st = ref ( $schema );
692 if (! $st || $st ne 'HASH' ) {
693 add_error
( $errors, $path, "Invalid schema definition." );
697 my $vt = ref ( $value );
698 if (! $vt || $vt ne 'HASH' ) {
699 add_error
( $errors, $path, "an object is required" );
703 foreach my $k ( keys %$schema ) {
704 check_prop
( $value ->{ $k }, $schema ->{ $k }, $path ?
" $path . $k " : $k, $errors );
707 foreach my $k ( keys %$value ) {
709 my $newpath = $path ?
" $path . $k " : $k ;
711 if ( my $subschema = $schema ->{ $k }) {
712 if ( my $requires = $subschema ->{ requires
}) {
713 if ( ref ( $requires )) {
714 #print "TEST: " . Dumper($value) . "\n", Dumper($requires) ;
715 check_prop
( $value, $requires, $path, $errors );
716 } elsif (! defined ( $value ->{ $requires })) {
717 add_error
( $errors, $path ?
" $path . $requires " : $requires,
718 "missing property - ' $newpath ' requiers this property" );
722 next ; # value is already checked above
725 if ( defined ( $additional_properties ) && ! $additional_properties ) {
726 add_error
( $errors, $newpath, "property is not defined in schema " .
727 "and the schema does not allow additional properties" );
730 check_prop
( $value ->{ $k }, $additional_properties, $newpath, $errors )
731 if ref ( $additional_properties );
735 sub check_object_warn
{
736 my ( $path, $schema, $value, $additional_properties ) = @_ ;
738 check_object
( $path, $schema, $value, $additional_properties, $errors );
739 if ( scalar ( %$errors )) {
740 foreach my $k ( keys %$errors ) {
741 warn "parse error: $k : $errors ->{ $k } \n " ;
749 my ( $value, $schema, $path, $errors ) = @_ ;
751 die "internal error - no schema" if ! $schema ;
752 die "internal error" if ! $errors ;
754 #print "check_prop $path\n" if $value;
756 my $st = ref ( $schema );
757 if (! $st || $st ne 'HASH' ) {
758 add_error
( $errors, $path, "Invalid schema definition." );
762 # if it extends another schema, it must pass that schema as well
763 if ( $schema ->{ extends
}) {
764 check_prop
( $value, $schema ->{ extends
}, $path, $errors );
767 if (! defined ( $value )) {
768 return if $schema ->{ type
} && $schema ->{ type
} eq 'null' ;
769 if (! $schema ->{ optional
} && ! $schema ->{ alias
} && ! $schema ->{ group
}) {
770 add_error
( $errors, $path, "property is missing and it is not optional" );
775 return if ! check_type
( $path, $schema ->{ type
}, $value, $errors );
777 if ( $schema ->{ disallow
}) {
779 if ( check_type
( $path, $schema ->{ disallow
}, $value, $tmperr )) {
780 add_error
( $errors, $path, "disallowed value was matched" );
785 if ( my $vt = ref ( $value )) {
787 if ( $vt eq 'ARRAY' ) {
788 if ( $schema ->{ items
}) {
789 my $it = ref ( $schema ->{ items
});
790 if ( $it && $it eq 'ARRAY' ) {
791 #die "implement me $path: $vt " . Dumper($schema) ."\n". Dumper($value);
792 die "not implemented" ;
795 foreach my $el ( @$value ) {
796 check_prop
( $el, $schema ->{ items
}, "${path}[ $ind ]" , $errors );
802 } elsif ( $schema ->{ properties
} || $schema ->{ additionalProperties
}) {
803 check_object
( $path, defined ( $schema ->{ properties
}) ?
$schema ->{ properties
} : {},
804 $value, $schema ->{ additionalProperties
}, $errors );
810 if ( my $format = $schema ->{ format
}) {
811 eval { check_format
( $format, $value, $path ); };
813 add_error
( $errors, $path, "invalid format - $@ " );
818 if ( my $pattern = $schema ->{ pattern
}) {
819 if ( $value !~ m/^$pattern$/ ) {
820 add_error
( $errors, $path, "value does not match the regex pattern" );
825 if ( defined ( my $max = $schema ->{ maxLength
})) {
826 if ( length ( $value ) > $max ) {
827 add_error
( $errors, $path, "value may only be $max characters long" );
832 if ( defined ( my $min = $schema ->{ minLength
})) {
833 if ( length ( $value ) < $min ) {
834 add_error
( $errors, $path, "value must be at least $min characters long" );
839 if ( is_number
( $value )) {
840 if ( defined ( my $max = $schema ->{ maximum
})) {
842 add_error
( $errors, $path, "value must have a maximum value of $max " );
847 if ( defined ( my $min = $schema ->{ minimum
})) {
849 add_error
( $errors, $path, "value must have a minimum value of $min " );
855 if ( my $ea = $schema ->{ enum
}) {
858 foreach my $ev ( @$ea ) {
865 add_error
( $errors, $path, "value ' $value ' does not have a value in the enumeration '" .
866 join ( ", " , @$ea ) . "'" );
873 my ( $instance, $schema, $errmsg ) = @_ ;
876 $errmsg = "Parameter verification failed. \n " if ! $errmsg ;
878 # todo: cycle detection is only needed for debugging, I guess
879 # we can disable that in the final release
880 # todo: is there a better/faster way to detect cycles?
882 find_cycle
( $instance, sub { $cycles = 1 });
884 add_error
( $errors, undef , "data structure contains recursive cycles" );
886 check_prop
( $instance, $schema, '' , $errors );
889 if ( scalar ( %$errors )) {
890 raise
$errmsg, code
=> HTTP_BAD_REQUEST
, errors
=> $errors ;
896 my $schema_valid_types = [ "string" , "object" , "coderef" , "array" , "boolean" , "number" , "integer" , "null" , "any" ];
897 my $default_schema_noref = {
898 description
=> "This is the JSON Schema for JSON Schemas." ,
899 type
=> [ "object" ],
900 additionalProperties
=> 0 ,
903 type
=> [ "string" , "array" ],
904 description
=> "This is a type definition value. This can be a simple type, or a union type" ,
909 enum
=> $schema_valid_types,
911 enum
=> $schema_valid_types,
915 description
=> "This indicates that the instance property in the instance object is not required." ,
921 description
=> "This is a definition for the properties of an object value" ,
927 description
=> "When the value is an array, this indicates the schema to use to validate each item in an array" ,
931 additionalProperties
=> {
932 type
=> [ "boolean" , "object" ],
933 description
=> "This provides a default property definition for all properties that are not explicitly defined in an object type definition." ,
940 description
=> "This indicates the minimum value for the instance property when the type of the instance value is a number." ,
945 description
=> "This indicates the maximum value for the instance property when the type of the instance value is a number." ,
949 description
=> "When the instance value is a string, this indicates minimum length of the string" ,
956 description
=> "When the instance value is a string, this indicates maximum length of the string." ,
962 description
=> "A text representation of the type (used to generate documentation)." ,
967 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." ,
974 description
=> "This provides an enumeration of possible values that are valid for the instance property." ,
979 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)." ,
981 format_description
=> {
984 description
=> "This provides a shorter (usually just one word) description for a property used to generate descriptions for comma separated list property strings." ,
989 description
=> "This provides the title of the property" ,
992 type
=> [ "string" , "object" ],
994 description
=> "indicates a required property or a schema that must be validated if this property is present" ,
997 type
=> [ "string" , "object" ],
999 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" ,
1004 description
=> "Whether this is the default key in a comma separated list property string." ,
1009 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." ,
1014 description
=> "Allows to store the current 'key' as value of another property. Only valid if used together with 'alias'." ,
1015 requires
=> 'alias' ,
1020 description
=> "This indicates the default for the instance property."
1024 description
=> "Bash completion function. This function should return a list of possible values." ,
1030 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." ,
1035 description
=> "This indicates the schema extends the given schema. All instances of this schema must be valid to by the extended schema also." ,
1038 # this is from hyper schema
1041 description
=> "This defines the link relations of the instance objects" ,
1048 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" ,
1052 description
=> "This is the name of the link relation" ,
1058 description
=> "For submission links, this defines the method that should be used to access the target resource" ,
1068 my $default_schema = Storable
:: dclone
( $default_schema_noref );
1070 $default_schema ->{ properties
}->{ properties
}->{ additionalProperties
} = $default_schema ;
1071 $default_schema ->{ properties
}->{ additionalProperties
}->{ properties
} = $default_schema ->{ properties
};
1073 $default_schema ->{ properties
}->{ items
}->{ properties
} = $default_schema ->{ properties
};
1074 $default_schema ->{ properties
}->{ items
}->{ additionalProperties
} = 0 ;
1076 $default_schema ->{ properties
}->{ disallow
}->{ properties
} = $default_schema ->{ properties
};
1077 $default_schema ->{ properties
}->{ disallow
}->{ additionalProperties
} = 0 ;
1079 $default_schema ->{ properties
}->{ requires
}->{ properties
} = $default_schema ->{ properties
};
1080 $default_schema ->{ properties
}->{ requires
}->{ additionalProperties
} = 0 ;
1082 $default_schema ->{ properties
}->{ extends
}->{ properties
} = $default_schema ->{ properties
};
1083 $default_schema ->{ properties
}->{ extends
}->{ additionalProperties
} = 0 ;
1085 my $method_schema = {
1087 additionalProperties
=> 0 ,
1090 description
=> "This a description of the method" ,
1095 description
=> "This indicates the name of the function to call." ,
1098 additionalProperties
=> 1 ,
1113 description
=> "The HTTP method name." ,
1114 enum
=> [ 'GET' , 'POST' , 'PUT' , 'DELETE' ],
1119 description
=> "Method needs special privileges - only pvedaemon can execute it" ,
1124 description
=> "A parameter name. If specified, all calls to this method are proxied to the host contained in that parameter." ,
1129 description
=> "Required access permissions. By default only 'root' is allowed to access this method." ,
1131 additionalProperties
=> 0 ,
1134 description
=> "Describe access permissions." ,
1138 description
=> "A simply way to allow access for 'all' authenticated users. Value 'world' is used to allow access without credentials." ,
1140 enum
=> [ 'all' , 'world' ],
1144 description
=> "Array of permission checks (prefix notation)." ,
1151 description
=> "Used internally" ,
1155 description
=> "Used internally" ,
1160 description
=> "path for URL matching (uri template)" ,
1162 fragmentDelimiter
=> {
1164 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." ,
1169 description
=> "JSON Schema for parameters." ,
1174 description
=> "Used to store page formatter information (set by PVE::RESTHandler->register_page_formatter)." ,
1179 description
=> "JSON Schema for return value." ,
1184 description
=> "method implementaion (code reference)" ,
1189 description
=> "Delegate call to this class (perl class string)." ,
1192 additionalProperties
=> 0 ,
1198 fragmentDelimiter
=> { optional
=> 1 }
1206 sub validate_schema
{
1209 my $errmsg = "internal error - unable to verify schema \n " ;
1210 validate
( $schema, $default_schema, $errmsg );
1213 sub validate_method_info
{
1216 my $errmsg = "internal error - unable to verify method info \n " ;
1217 validate
( $info, $method_schema, $errmsg );
1219 validate_schema
( $info ->{ parameters
}) if $info ->{ parameters
};
1220 validate_schema
( $info ->{ returns
}) if $info ->{ returns
};
1223 # run a self test on load
1224 # make sure we can verify the default schema
1225 validate_schema
( $default_schema_noref );
1226 validate_schema
( $method_schema );
1228 # and now some utility methods (used by pve api)
1229 sub method_get_child_link
{
1232 return undef if ! $info ;
1234 my $schema = $info ->{ returns
};
1235 return undef if ! $schema || ! $schema ->{ type
} || $schema ->{ type
} ne 'array' ;
1237 my $links = $schema ->{ links
};
1238 return undef if ! $links ;
1241 foreach my $lnk ( @$links ) {
1242 if ( $lnk ->{ href
} && $lnk ->{ rel
} && ( $lnk ->{ rel
} eq 'child' )) {
1251 # a way to parse command line parameters, using a
1252 # schema to configure Getopt::Long
1254 my ( $schema, $args, $arg_param, $fixed_param, $pwcallback ) = @_ ;
1256 if (! $schema || ! $schema ->{ properties
}) {
1257 raise
( "too many arguments \n " , code
=> HTTP_BAD_REQUEST
)
1258 if scalar ( @$args ) != 0 ;
1263 if ( $arg_param && ! ref ( $arg_param )) {
1264 my $pd = $schema ->{ properties
}->{ $arg_param };
1265 die "expected list format $pd ->{format}"
1266 if !( $pd && $pd ->{ format
} && $pd ->{ format
} =~ m/-list/ );
1267 $list_param = $arg_param ;
1271 foreach my $prop ( keys %{ $schema ->{ properties
}}) {
1272 my $pd = $schema ->{ properties
}->{ $prop };
1273 next if $list_param && $prop eq $list_param ;
1274 next if defined ( $fixed_param ->{ $prop });
1276 if ( $prop eq 'password' && $pwcallback ) {
1277 # we do not accept plain password on input line, instead
1278 # we turn this into a boolean option and ask for password below
1279 # using $pwcallback() (for security reasons).
1280 push @getopt, " $prop " ;
1281 } elsif ( $pd ->{ type
} eq 'boolean' ) {
1282 push @getopt, " $prop :s" ;
1284 if ( $pd ->{ format
} && $pd ->{ format
} =~ m/-a?list/ ) {
1285 push @getopt, " $prop =s@" ;
1287 push @getopt, " $prop =s" ;
1292 Getopt
:: Long
:: Configure
( 'prefix_pattern=(--|-)' );
1295 raise
( "unable to parse option \n " , code
=> HTTP_BAD_REQUEST
)
1296 if ! Getopt
:: Long
:: GetOptionsFromArray
( $args, $opts, @getopt );
1300 $opts ->{ $list_param } = $args ;
1302 } elsif ( ref ( $arg_param )) {
1303 foreach my $arg_name ( @$arg_param ) {
1304 if ( $opts ->{ 'extra-args' }) {
1305 raise
( "internal error: extra-args must be the last argument \n " , code
=> HTTP_BAD_REQUEST
);
1307 if ( $arg_name eq 'extra-args' ) {
1308 $opts ->{ 'extra-args' } = $args ;
1312 raise
( "not enough arguments \n " , code
=> HTTP_BAD_REQUEST
) if ! @$args ;
1313 $opts ->{ $arg_name } = shift @$args ;
1315 raise
( "too many arguments \n " , code
=> HTTP_BAD_REQUEST
) if @$args ;
1317 raise
( "too many arguments \n " , code
=> HTTP_BAD_REQUEST
)
1318 if scalar ( @$args ) != 0 ;
1322 if ( my $pd = $schema ->{ properties
}->{ password
}) {
1323 if ( $pd ->{ type
} ne 'boolean' && $pwcallback ) {
1324 if ( $opts ->{ password
} || ! $pd ->{ optional
}) {
1325 $opts ->{ password
} = & $pwcallback ();
1330 $opts = PVE
:: Tools
:: decode_utf8_parameters
( $opts );
1332 foreach my $p ( keys %$opts ) {
1333 if ( my $pd = $schema ->{ properties
}->{ $p }) {
1334 if ( $pd ->{ type
} eq 'boolean' ) {
1335 if ( $opts ->{ $p } eq '' ) {
1337 } elsif ( $opts ->{ $p } =~ m/^(1|true|yes|on)$/i ) {
1339 } elsif ( $opts ->{ $p } =~ m/^(0|false|no|off)$/i ) {
1342 raise
( "unable to parse boolean option \n " , code
=> HTTP_BAD_REQUEST
);
1344 } elsif ( $pd ->{ format
}) {
1346 if ( $pd ->{ format
} =~ m/-list/ ) {
1347 # allow --vmid 100 --vmid 101 and --vmid 100,101
1348 # allow --dow mon --dow fri and --dow mon,fri
1349 $opts ->{ $p } = join ( "," , @{ $opts ->{ $p }}) if ref ( $opts ->{ $p }) eq 'ARRAY' ;
1350 } elsif ( $pd ->{ format
} =~ m/-alist/ ) {
1351 # we encode array as \0 separated strings
1352 # Note: CGI.pm also use this encoding
1353 if ( scalar (@{ $opts ->{ $p }}) != 1 ) {
1354 $opts ->{ $p } = join ( "\0" , @{ $opts ->{ $p }});
1356 # st that split_list knows it is \0 terminated
1357 my $v = $opts ->{ $p }->[ 0 ];
1358 $opts ->{ $p } = " $v\0 " ;
1365 foreach my $p ( keys %$fixed_param ) {
1366 $opts ->{ $p } = $fixed_param ->{ $p };
1372 # A way to parse configuration data by giving a json schema
1374 my ( $schema, $filename, $raw ) = @_ ;
1376 # do fast check (avoid validate_schema($schema))
1377 die "got strange schema" if ! $schema ->{ type
} ||
1378 ! $schema ->{ properties
} || $schema ->{ type
} ne 'object' ;
1382 while ( $raw =~ /^\s*(.+?)\s*$/gm ) {
1385 next if $line =~ /^#/ ;
1387 if ( $line =~ m/^(\S+?):\s*(.*)$/ ) {
1390 if ( $schema ->{ properties
}->{ $key } &&
1391 $schema ->{ properties
}->{ $key }->{ type
} eq 'boolean' ) {
1393 $value = 1 if $value =~ m/^(1|on|yes|true)$/i ;
1394 $value = 0 if $value =~ m/^(0|off|no|false)$/i ;
1396 $cfg ->{ $key } = $value ;
1398 warn "ignore config line: $line\n "
1403 check_prop
( $cfg, $schema, '' , $errors );
1405 foreach my $k ( keys %$errors ) {
1406 warn "parse error in ' $filename ' - ' $k ': $errors ->{ $k } \n " ;
1413 # generate simple key/value file
1415 my ( $schema, $filename, $cfg ) = @_ ;
1417 # do fast check (avoid validate_schema($schema))
1418 die "got strange schema" if ! $schema ->{ type
} ||
1419 ! $schema ->{ properties
} || $schema ->{ type
} ne 'object' ;
1421 validate
( $cfg, $schema, "validation error in ' $filename ' \n " );
1425 foreach my $k ( keys %$cfg ) {
1426 $data .= " $k : $cfg ->{ $k } \n " ;
1432 # helpers used to generate our manual pages
1434 my $find_schema_default_key = sub {
1438 my $keyAliasProps = {};
1440 foreach my $key ( keys %$format ) {
1441 my $phash = $format ->{ $key };
1442 if ( $phash ->{ default_key
}) {
1443 die "multiple default keys in schema ( $default_key, $key ) \n "
1444 if defined ( $default_key );
1445 die "default key ' $key ' is an alias - this is not allowed \n "
1446 if defined ( $phash ->{ alias
});
1447 die "default key ' $key ' with keyAlias attribute is not allowed \n "
1448 if $phash ->{ keyAlias
};
1449 die "found keyAlias without 'alias definition for ' $key ' \n "
1450 if $phash ->{ keyAlias
} && ! $phash ->{ alias
};
1452 $default_key = $key ;
1454 my $key_alias = $phash ->{ keyAlias
};
1455 if ( $phash ->{ alias
} && $key_alias ) {
1456 die "inconsistent keyAlias ' $key_alias ' definition"
1457 if defined ( $keyAliasProps ->{ $key_alias }) &&
1458 $keyAliasProps ->{ $key_alias } ne $phash ->{ alias
};
1459 $keyAliasProps ->{ $key_alias } = $phash ->{ alias
};
1463 return wantarray ?
( $default_key, $keyAliasProps ) : $default_key ;
1466 sub generate_typetext
{
1469 my ( $default_key, $keyAliasProps ) = & $find_schema_default_key ( $format );
1474 my $add_option_string = sub {
1475 my ( $text, $optional ) = @_ ;
1481 $text = "[ $text ]" if $optional ;
1486 my $format_key_value = sub {
1487 my ( $key, $phash ) = @_ ;
1489 die "internal error" if defined ( $phash ->{ alias
});
1495 if ( my $desc = $phash ->{ format_description
}) {
1496 $typetext .= "< $desc >" ;
1497 } elsif ( my $text = $phash ->{ typetext
}) {
1499 } elsif ( my $enum = $phash ->{ enum
}) {
1500 $typetext .= '<' . join ( '|' , @$enum ) . '>' ;
1501 } elsif ( $phash ->{ type
} eq 'boolean' ) {
1502 $typetext .= '<1|0>' ;
1503 } elsif ( $phash ->{ type
} eq 'integer' ) {
1504 $typetext .= '<integer>' ;
1505 } elsif ( $phash ->{ type
} eq 'number' ) {
1506 $typetext .= '<number>' ;
1508 die "internal error: neither format_description nor typetext found for option ' $key '" ;
1511 if ( defined ( $default_key ) && ( $default_key eq $key )) {
1512 & $add_option_string ( "[ $keytext =] $typetext " , $phash ->{ optional
});
1514 & $add_option_string ( " $keytext = $typetext " , $phash ->{ optional
});
1520 my $cond_add_key = sub {
1523 return if $done ->{ $key }; # avoid duplicates
1527 my $phash = $format ->{ $key };
1529 return if ! $phash ; # should not happen
1531 return if $phash ->{ alias
};
1533 & $format_key_value ( $key, $phash );
1537 & $cond_add_key ( $default_key ) if defined ( $default_key );
1539 # add required keys first
1540 foreach my $key ( sort keys %$format ) {
1541 my $phash = $format ->{ $key };
1542 & $cond_add_key ( $key ) if $phash && ! $phash ->{ optional
};
1546 foreach my $key ( sort keys %$format ) {
1547 & $cond_add_key ( $key );
1550 foreach my $keyAlias ( sort keys %$keyAliasProps ) {
1551 & $add_option_string ( "< $keyAlias >=< $keyAliasProps ->{ $keyAlias }>" , 1 );
1557 sub print_property_string
{
1558 my ( $data, $format, $skip, $path ) = @_ ;
1560 if ( ref ( $format ) ne 'HASH' ) {
1561 my $schema = get_format
( $format );
1562 die "not a valid format: $format\n " if ! $schema ;
1567 check_object
( $path, $format, $data, undef , $errors );
1568 if ( scalar ( %$errors )) {
1569 raise
"format error" , errors
=> $errors ;
1572 my ( $default_key, $keyAliasProps ) = & $find_schema_default_key ( $format );
1577 my $add_option_string = sub {
1580 $res .= ',' if $add_sep ;
1585 my $format_value = sub {
1586 my ( $key, $value, $format ) = @_ ;
1588 if ( defined ( $format ) && ( $format eq 'disk-size' )) {
1589 return format_size
( $value );
1591 die "illegal value with commas for $key\n " if $value =~ /,/ ;
1598 my $cond_add_key = sub {
1601 return if $done ->{ $key }; # avoid duplicates
1605 my $value = $data ->{ $key };
1607 return if ! defined ( $value );
1609 my $phash = $format ->{ $key };
1611 # try to combine values if we have key aliases
1612 if ( my $combine = $keyAliasProps ->{ $key }) {
1613 if ( defined ( my $combine_value = $data ->{ $combine })) {
1614 my $combine_format = $format ->{ $combine }->{ format
};
1615 my $value_str = & $format_value ( $key, $value, $phash ->{ format
});
1616 my $combine_str = & $format_value ( $combine, $combine_value, $combine_format );
1617 & $add_option_string ( "${value_str}=${combine_str}" );
1618 $done ->{ $combine } = 1 ;
1623 if ( $phash && $phash ->{ alias
}) {
1624 $phash = $format ->{ $phash ->{ alias
}};
1627 die "invalid key ' $key ' \n " if ! $phash ;
1628 die "internal error" if defined ( $phash ->{ alias
});
1630 my $value_str = & $format_value ( $key, $value, $phash ->{ format
});
1631 & $add_option_string ( " $key =${value_str}" );
1634 # add default key first
1635 & $cond_add_key ( $default_key ) if defined ( $default_key );
1637 # add required keys first
1638 foreach my $key ( sort keys %$data ) {
1639 my $phash = $format ->{ $key };
1640 & $cond_add_key ( $key ) if $phash && ! $phash ->{ optional
};
1644 foreach my $key ( sort keys %$data ) {
1645 & $cond_add_key ( $key );
1651 sub schema_get_type_text
{
1654 if ( $phash ->{ typetext
}) {
1655 return $phash ->{ typetext
};
1656 } elsif ( $phash ->{ format_description
}) {
1657 return "< $phash ->{format_description}>" ;
1658 } elsif ( $phash ->{ enum
}) {
1659 return "(" . join ( ' | ' , sort @{ $phash ->{ enum
}}) . ")" ;
1660 } elsif ( $phash ->{ pattern
}) {
1661 return $phash ->{ pattern
};
1662 } elsif ( $phash ->{ type
} eq 'integer' || $phash ->{ type
} eq 'number' ) {
1663 if ( defined ( $phash ->{ minimum
}) && defined ( $phash ->{ maximum
})) {
1664 return " $phash ->{type} ( $phash ->{minimum} - $phash ->{maximum})" ;
1665 } elsif ( defined ( $phash ->{ minimum
})) {
1666 return " $phash ->{type} ( $phash ->{minimum} - N)" ;
1667 } elsif ( defined ( $phash ->{ maximum
})) {
1668 return " $phash ->{type} (-N - $phash ->{maximum})" ;
1670 } elsif ( $phash ->{ type
} eq 'string' ) {
1671 if ( my $format = $phash ->{ format
}) {
1672 $format = get_format
( $format ) if ref ( $format ) ne 'HASH' ;
1673 if ( ref ( $format ) eq 'HASH' ) {
1674 return generate_typetext
( $format );
1679 my $type = $phash ->{ type
} || 'string' ;