]>
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) ;
16 register_standard_option
20 # Note: This class implements something similar to JSON schema, but it is not 100% complete.
21 # see: http://tools.ietf.org/html/draft-zyp-json-schema-02
22 # see: http://json-schema.org/
24 # the code is similar to the javascript parser from http://code.google.com/p/jsonschema/
26 my $standard_options = {};
27 sub register_standard_option
{
28 my ( $name, $schema ) = @_ ;
30 die "standard option ' $name ' already registered \n "
31 if $standard_options ->{ $name };
33 $standard_options ->{ $name } = $schema ;
36 sub get_standard_option
{
37 my ( $name, $base ) = @_ ;
39 my $std = $standard_options ->{ $name };
40 die "no such standard option ' $name ' \n " if ! $std ;
42 my $res = $base || {};
44 foreach my $opt ( keys %$std ) {
45 next if defined ( $res ->{ $opt });
46 $res ->{ $opt } = $std ->{ $opt };
52 register_standard_option
( 'pve-vmid' , {
53 description
=> "The (unique) ID of the VM." ,
54 type
=> 'integer' , format
=> 'pve-vmid' ,
58 register_standard_option
( 'pve-node' , {
59 description
=> "The cluster node name." ,
60 type
=> 'string' , format
=> 'pve-node' ,
63 register_standard_option
( 'pve-node-list' , {
64 description
=> "List of cluster node names." ,
65 type
=> 'string' , format
=> 'pve-node-list' ,
68 register_standard_option
( 'pve-iface' , {
69 description
=> "Network interface name." ,
70 type
=> 'string' , format
=> 'pve-iface' ,
71 minLength
=> 2 , maxLength
=> 20 ,
74 PVE
:: JSONSchema
:: register_standard_option
( 'pve-storage-id' , {
75 description
=> "The storage identifier." ,
76 type
=> 'string' , format
=> 'pve-storage-id' ,
79 PVE
:: JSONSchema
:: register_standard_option
( 'pve-config-digest' , {
80 description
=> 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.' ,
83 maxLength
=> 40 , # sha1 hex digest lenght is 40
86 PVE
:: JSONSchema
:: register_standard_option
( 'extra-args' , {
87 description
=> "Extra arguments as array" ,
89 items
=> { type
=> 'string' },
96 my ( $format, $code ) = @_ ;
98 die "JSON schema format ' $format ' already registered \n "
99 if $format_list ->{ $format };
101 $format_list ->{ $format } = $code ;
104 # register some common type for pve
106 register_format
( 'string' , sub {}); # allow format => 'string-list'
108 register_format
( 'pve-configid' , \
& pve_verify_configid
);
109 sub pve_verify_configid
{
110 my ( $id, $noerr ) = @_ ;
112 if ( $id !~ m/^[a-z][a-z0-9_]+$/i ) {
113 return undef if $noerr ;
114 die "invalid configuration ID ' $id ' \n " ;
119 PVE
:: JSONSchema
:: register_format
( 'pve-storage-id' , \
& parse_storage_id
);
120 sub parse_storage_id
{
121 my ( $storeid, $noerr ) = @_ ;
123 if ( $storeid !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i ) {
124 return undef if $noerr ;
125 die "storage ID ' $storeid ' contains illegal characters \n " ;
131 register_format
( 'pve-vmid' , \
& pve_verify_vmid
);
132 sub pve_verify_vmid
{
133 my ( $vmid, $noerr ) = @_ ;
135 if ( $vmid !~ m/^[1-9][0-9]+$/ ) {
136 return undef if $noerr ;
137 die "value does not look like a valid VM ID \n " ;
142 register_format
( 'pve-node' , \
& pve_verify_node_name
);
143 sub pve_verify_node_name
{
144 my ( $node, $noerr ) = @_ ;
146 if ( $node !~ m/^([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)$/ ) {
147 return undef if $noerr ;
148 die "value does not look like a valid node name \n " ;
153 register_format
( 'ipv4' , \
& pve_verify_ipv4
);
154 sub pve_verify_ipv4
{
155 my ( $ipv4, $noerr ) = @_ ;
157 if ( $ipv4 !~ m/^(?:$IPV4RE)$/ ) {
158 return undef if $noerr ;
159 die "value does not look like a valid IPv4 address \n " ;
164 register_format
( 'ipv6' , \
& pve_verify_ipv6
);
165 sub pve_verify_ipv6
{
166 my ( $ipv6, $noerr ) = @_ ;
168 if ( $ipv6 !~ m/^(?:$IPV6RE)$/ ) {
169 return undef if $noerr ;
170 die "value does not look like a valid IPv6 address \n " ;
175 register_format
( 'ip' , \
& pve_verify_ip
);
177 my ( $ip, $noerr ) = @_ ;
179 if ( $ip !~ m/^(?:(?:$IPV4RE)|(?:$IPV6RE))$/ ) {
180 return undef if $noerr ;
181 die "value does not look like a valid IP address \n " ;
186 my $ipv4_mask_hash = {
203 '255.255.128.0' => 17 ,
204 '255.255.192.0' => 18 ,
205 '255.255.224.0' => 19 ,
206 '255.255.240.0' => 20 ,
207 '255.255.248.0' => 21 ,
208 '255.255.252.0' => 22 ,
209 '255.255.254.0' => 23 ,
210 '255.255.255.0' => 24 ,
211 '255.255.255.128' => 25 ,
212 '255.255.255.192' => 26 ,
213 '255.255.255.224' => 27 ,
214 '255.255.255.240' => 28 ,
215 '255.255.255.248' => 29 ,
216 '255.255.255.252' => 30
219 register_format
( 'ipv4mask' , \
& pve_verify_ipv4mask
);
220 sub pve_verify_ipv4mask
{
221 my ( $mask, $noerr ) = @_ ;
223 if (! defined ( $ipv4_mask_hash ->{ $mask })) {
224 return undef if $noerr ;
225 die "value does not look like a valid IP netmask \n " ;
230 register_format
( 'CIDR' , \
& pve_verify_cidr
);
231 sub pve_verify_cidr
{
232 my ( $cidr, $noerr ) = @_ ;
234 if ( $cidr =~ m!^(?:$IPV4RE)(?:/(\d+))$! && ( $1 > 7 ) && ( $1 < 32 )) {
236 } elsif ( $cidr =~ m!^(?:$IPV6RE)(?:/(\d+))$! && ( $1 > 7 ) && ( $1 <= 120 )) {
240 return undef if $noerr ;
241 die "value does not look like a valid CIDR network \n " ;
244 register_format
( 'email' , \
& pve_verify_email
);
245 sub pve_verify_email
{
246 my ( $email, $noerr ) = @_ ;
248 # we use same regex as in Utils.js
249 if ( $email !~ /^(\w+)([\-+.][\w]+)*@(\w[\-\w]*\.){1,5}([A-Za-z]){2,63}$/ ) {
250 return undef if $noerr ;
251 die "value does not look like a valid email address \n " ;
256 register_format
( 'dns-name' , \
& pve_verify_dns_name
);
257 sub pve_verify_dns_name
{
258 my ( $name, $noerr ) = @_ ;
260 my $namere = "([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)" ;
262 if ( $name !~ /^(${namere}\.)*${namere}$/ ) {
263 return undef if $noerr ;
264 die "value does not look like a valid DNS name \n " ;
269 # network interface name
270 register_format
( 'pve-iface' , \
& pve_verify_iface
);
271 sub pve_verify_iface
{
272 my ( $id, $noerr ) = @_ ;
274 if ( $id !~ m/^[a-z][a-z0-9_]{1,20}([:\.]\d+)?$/i ) {
275 return undef if $noerr ;
276 die "invalid network interface name ' $id ' \n " ;
281 # general addresses by name or IP
282 register_format
( 'address' , \
& pve_verify_address
);
283 sub pve_verify_address
{
284 my ( $addr, $noerr ) = @_ ;
286 if (!( pve_verify_ip
( $addr, 1 ) ||
287 pve_verify_dns_name
( $addr, 1 )))
289 return undef if $noerr ;
290 die "value does not look like a valid address: $addr\n " ;
295 register_standard_option
( 'spice-proxy' , {
296 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)." ,
297 type
=> 'string' , format
=> 'address' ,
300 register_standard_option
( 'remote-viewer-config' , {
301 description
=> "Returned values can be directly passed to the 'remote-viewer' application." ,
302 additionalProperties
=> 1 ,
304 type
=> { type
=> 'string' },
305 password
=> { type
=> 'string' },
306 proxy
=> { type
=> 'string' },
307 host
=> { type
=> 'string' },
308 'tls-port' => { type
=> 'integer' },
312 register_format
( 'pve-startup-order' , \
& pve_verify_startup_order
);
313 sub pve_verify_startup_order
{
314 my ( $value, $noerr ) = @_ ;
316 return $value if pve_parse_startup_order
( $value );
318 return undef if $noerr ;
320 die "unable to parse startup options \n " ;
323 sub pve_parse_startup_order
{
326 return undef if ! $value ;
330 foreach my $p ( split ( /,/ , $value )) {
331 next if $p =~ m/^\s*$/ ;
333 if ( $p =~ m/^(order=)?(\d+)$/ ) {
335 } elsif ( $p =~ m/^up=(\d+)$/ ) {
337 } elsif ( $p =~ m/^down=(\d+)$/ ) {
347 PVE
:: JSONSchema
:: register_standard_option
( 'pve-startup-order' , {
348 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." ,
350 type
=> 'string' , format
=> 'pve-startup-order' ,
351 typetext
=> '[[order=]\d+] [,up=\d+] [,down=\d+] ' ,
355 my ( $format, $value ) = @_ ;
357 return if $format eq 'regex' ;
359 if ( $format =~ m/^(.*)-a?list$/ ) {
361 my $code = $format_list ->{ $1 };
363 die "undefined format ' $format ' \n " if ! $code ;
365 # Note: we allow empty lists
366 foreach my $v ( split_list
( $value )) {
370 } elsif ( $format =~ m/^(.*)-opt$/ ) {
372 my $code = $format_list ->{ $1 };
374 die "undefined format ' $format ' \n " if ! $code ;
376 return if ! $value ; # allow empty string
382 my $code = $format_list ->{ $format };
384 die "undefined format ' $format ' \n " if ! $code ;
391 my ( $errors, $path, $msg ) = @_ ;
393 $path = '_root' if ! $path ;
395 if ( $errors ->{ $path }) {
396 $errors ->{ $path } = join ( ' \n ' , $errors ->{ $path }, $msg );
398 $errors ->{ $path } = $msg ;
405 # see 'man perlretut'
406 return $value =~ /^[+-]?(\d+\.\d+|\d+\.|\.\d+|\d+)([eE][+-]?\d+)?$/ ;
412 return $value =~ m/^[+-]?\d+$/ ;
416 my ( $path, $type, $value, $errors ) = @_ ;
420 if (! defined ( $value )) {
421 return 1 if $type eq 'null' ;
425 if ( my $tt = ref ( $type )) {
426 if ( $tt eq 'ARRAY' ) {
427 foreach my $t ( @$type ) {
429 check_type
( $path, $t, $value, $tmperr );
430 return 1 if ! scalar ( %$tmperr );
432 my $ttext = join ( '|' , @$type );
433 add_error
( $errors, $path, "type check (' $ttext ') failed" );
435 } elsif ( $tt eq 'HASH' ) {
437 check_prop
( $value, $type, $path, $tmperr );
438 return 1 if ! scalar ( %$tmperr );
439 add_error
( $errors, $path, "type check failed" );
442 die "internal error - got reference type ' $tt '" ;
447 return 1 if $type eq 'any' ;
449 if ( $type eq 'null' ) {
450 if ( defined ( $value )) {
451 add_error
( $errors, $path, "type check (' $type ') failed - value is not null" );
457 my $vt = ref ( $value );
459 if ( $type eq 'array' ) {
460 if (! $vt || $vt ne 'ARRAY' ) {
461 add_error
( $errors, $path, "type check (' $type ') failed" );
465 } elsif ( $type eq 'object' ) {
466 if (! $vt || $vt ne 'HASH' ) {
467 add_error
( $errors, $path, "type check (' $type ') failed" );
471 } elsif ( $type eq 'coderef' ) {
472 if (! $vt || $vt ne 'CODE' ) {
473 add_error
( $errors, $path, "type check (' $type ') failed" );
479 add_error
( $errors, $path, "type check (' $type ') failed - got $vt " );
482 if ( $type eq 'string' ) {
483 return 1 ; # nothing to check ?
484 } elsif ( $type eq 'boolean' ) {
485 #if ($value =~ m/^(1|true|yes|on)$/i) {
488 #} elsif ($value =~ m/^(0|false|no|off)$/i) {
489 } elsif ( $value eq '0' ) {
492 add_error
( $errors, $path, "type check (' $type ') failed - got ' $value '" );
495 } elsif ( $type eq 'integer' ) {
496 if (! is_integer
( $value )) {
497 add_error
( $errors, $path, "type check (' $type ') failed - got ' $value '" );
501 } elsif ( $type eq 'number' ) {
502 if (! is_number
( $value )) {
503 add_error
( $errors, $path, "type check (' $type ') failed - got ' $value '" );
508 return 1 ; # no need to verify unknown types
518 my ( $path, $schema, $value, $additional_properties, $errors ) = @_ ;
520 # print "Check Object " . Dumper($value) . "\nSchema: " . Dumper($schema);
522 my $st = ref ( $schema );
523 if (! $st || $st ne 'HASH' ) {
524 add_error
( $errors, $path, "Invalid schema definition." );
528 my $vt = ref ( $value );
529 if (! $vt || $vt ne 'HASH' ) {
530 add_error
( $errors, $path, "an object is required" );
534 foreach my $k ( keys %$schema ) {
535 check_prop
( $value ->{ $k }, $schema ->{ $k }, $path ?
" $path . $k " : $k, $errors );
538 foreach my $k ( keys %$value ) {
540 my $newpath = $path ?
" $path . $k " : $k ;
542 if ( my $subschema = $schema ->{ $k }) {
543 if ( my $requires = $subschema ->{ requires
}) {
544 if ( ref ( $requires )) {
545 #print "TEST: " . Dumper($value) . "\n", Dumper($requires) ;
546 check_prop
( $value, $requires, $path, $errors );
547 } elsif (! defined ( $value ->{ $requires })) {
548 add_error
( $errors, $path ?
" $path . $requires " : $requires,
549 "missing property - ' $newpath ' requiers this property" );
553 next ; # value is already checked above
556 if ( defined ( $additional_properties ) && ! $additional_properties ) {
557 add_error
( $errors, $newpath, "property is not defined in schema " .
558 "and the schema does not allow additional properties" );
561 check_prop
( $value ->{ $k }, $additional_properties, $newpath, $errors )
562 if ref ( $additional_properties );
566 sub check_object_warn
{
567 my ( $path, $schema, $value, $additional_properties ) = @_ ;
569 check_object
( $path, $schema, $value, $additional_properties, $errors );
570 if ( scalar ( %$errors )) {
571 foreach my $k ( keys %$errors ) {
572 warn "parse error: $k : $errors ->{ $k } \n " ;
580 my ( $value, $schema, $path, $errors ) = @_ ;
582 die "internal error - no schema" if ! $schema ;
583 die "internal error" if ! $errors ;
585 #print "check_prop $path\n" if $value;
587 my $st = ref ( $schema );
588 if (! $st || $st ne 'HASH' ) {
589 add_error
( $errors, $path, "Invalid schema definition." );
593 # if it extends another schema, it must pass that schema as well
594 if ( $schema ->{ extends
}) {
595 check_prop
( $value, $schema ->{ extends
}, $path, $errors );
598 if (! defined ( $value )) {
599 return if $schema ->{ type
} && $schema ->{ type
} eq 'null' ;
600 if (! $schema ->{ optional
}) {
601 add_error
( $errors, $path, "property is missing and it is not optional" );
606 return if ! check_type
( $path, $schema ->{ type
}, $value, $errors );
608 if ( $schema ->{ disallow
}) {
610 if ( check_type
( $path, $schema ->{ disallow
}, $value, $tmperr )) {
611 add_error
( $errors, $path, "disallowed value was matched" );
616 if ( my $vt = ref ( $value )) {
618 if ( $vt eq 'ARRAY' ) {
619 if ( $schema ->{ items
}) {
620 my $it = ref ( $schema ->{ items
});
621 if ( $it && $it eq 'ARRAY' ) {
622 #die "implement me $path: $vt " . Dumper($schema) ."\n". Dumper($value);
623 die "not implemented" ;
626 foreach my $el ( @$value ) {
627 check_prop
( $el, $schema ->{ items
}, "${path}[ $ind ]" , $errors );
633 } elsif ( $schema ->{ properties
} || $schema ->{ additionalProperties
}) {
634 check_object
( $path, defined ( $schema ->{ properties
}) ?
$schema ->{ properties
} : {},
635 $value, $schema ->{ additionalProperties
}, $errors );
641 if ( my $format = $schema ->{ format
}) {
642 eval { check_format
( $format, $value ); };
644 add_error
( $errors, $path, "invalid format - $@ " );
649 if ( my $pattern = $schema ->{ pattern
}) {
650 if ( $value !~ m/^$pattern$/ ) {
651 add_error
( $errors, $path, "value does not match the regex pattern" );
656 if ( defined ( my $max = $schema ->{ maxLength
})) {
657 if ( length ( $value ) > $max ) {
658 add_error
( $errors, $path, "value may only be $max characters long" );
663 if ( defined ( my $min = $schema ->{ minLength
})) {
664 if ( length ( $value ) < $min ) {
665 add_error
( $errors, $path, "value must be at least $min characters long" );
670 if ( is_number
( $value )) {
671 if ( defined ( my $max = $schema ->{ maximum
})) {
673 add_error
( $errors, $path, "value must have a maximum value of $max " );
678 if ( defined ( my $min = $schema ->{ minimum
})) {
680 add_error
( $errors, $path, "value must have a minimum value of $min " );
686 if ( my $ea = $schema ->{ enum
}) {
689 foreach my $ev ( @$ea ) {
696 add_error
( $errors, $path, "value ' $value ' does not have a value in the enumeration '" .
697 join ( ", " , @$ea ) . "'" );
704 my ( $instance, $schema, $errmsg ) = @_ ;
707 $errmsg = "Parameter verification failed. \n " if ! $errmsg ;
709 # todo: cycle detection is only needed for debugging, I guess
710 # we can disable that in the final release
711 # todo: is there a better/faster way to detect cycles?
713 find_cycle
( $instance, sub { $cycles = 1 });
715 add_error
( $errors, undef , "data structure contains recursive cycles" );
717 check_prop
( $instance, $schema, '' , $errors );
720 if ( scalar ( %$errors )) {
721 raise
$errmsg, code
=> HTTP_BAD_REQUEST
, errors
=> $errors ;
727 my $schema_valid_types = [ "string" , "object" , "coderef" , "array" , "boolean" , "number" , "integer" , "null" , "any" ];
728 my $default_schema_noref = {
729 description
=> "This is the JSON Schema for JSON Schemas." ,
730 type
=> [ "object" ],
731 additionalProperties
=> 0 ,
734 type
=> [ "string" , "array" ],
735 description
=> "This is a type definition value. This can be a simple type, or a union type" ,
740 enum
=> $schema_valid_types,
742 enum
=> $schema_valid_types,
746 description
=> "This indicates that the instance property in the instance object is not required." ,
752 description
=> "This is a definition for the properties of an object value" ,
758 description
=> "When the value is an array, this indicates the schema to use to validate each item in an array" ,
762 additionalProperties
=> {
763 type
=> [ "boolean" , "object" ],
764 description
=> "This provides a default property definition for all properties that are not explicitly defined in an object type definition." ,
771 description
=> "This indicates the minimum value for the instance property when the type of the instance value is a number." ,
776 description
=> "This indicates the maximum value for the instance property when the type of the instance value is a number." ,
780 description
=> "When the instance value is a string, this indicates minimum length of the string" ,
787 description
=> "When the instance value is a string, this indicates maximum length of the string." ,
793 description
=> "A text representation of the type (used to generate documentation)." ,
798 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." ,
806 description
=> "This provides an enumeration of possible values that are valid for the instance property." ,
811 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)." ,
813 format_description
=> {
816 description
=> "This provides a shorter (usually just one word) description for a property used to generate descriptions for comma separated list property strings." ,
821 description
=> "This provides the title of the property" ,
824 type
=> [ "string" , "object" ],
826 description
=> "indicates a required property or a schema that must be validated if this property is present" ,
831 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" ,
836 description
=> "This indicates the default for the instance property."
840 description
=> "Bash completion function. This function should return a list of possible values." ,
846 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, than this instance is not valid." ,
851 description
=> "This indicates the schema extends the given schema. All instances of this schema must be valid to by the extended schema also." ,
854 # this is from hyper schema
857 description
=> "This defines the link relations of the instance objects" ,
864 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" ,
868 description
=> "This is the name of the link relation" ,
874 description
=> "For submission links, this defines the method that should be used to access the target resource" ,
884 my $default_schema = Storable
:: dclone
( $default_schema_noref );
886 $default_schema ->{ properties
}->{ properties
}->{ additionalProperties
} = $default_schema ;
887 $default_schema ->{ properties
}->{ additionalProperties
}->{ properties
} = $default_schema ->{ properties
};
889 $default_schema ->{ properties
}->{ items
}->{ properties
} = $default_schema ->{ properties
};
890 $default_schema ->{ properties
}->{ items
}->{ additionalProperties
} = 0 ;
892 $default_schema ->{ properties
}->{ disallow
}->{ properties
} = $default_schema ->{ properties
};
893 $default_schema ->{ properties
}->{ disallow
}->{ additionalProperties
} = 0 ;
895 $default_schema ->{ properties
}->{ requires
}->{ properties
} = $default_schema ->{ properties
};
896 $default_schema ->{ properties
}->{ requires
}->{ additionalProperties
} = 0 ;
898 $default_schema ->{ properties
}->{ extends
}->{ properties
} = $default_schema ->{ properties
};
899 $default_schema ->{ properties
}->{ extends
}->{ additionalProperties
} = 0 ;
901 my $method_schema = {
903 additionalProperties
=> 0 ,
906 description
=> "This a description of the method" ,
911 description
=> "This indicates the name of the function to call." ,
914 additionalProperties
=> 1 ,
929 description
=> "The HTTP method name." ,
930 enum
=> [ 'GET' , 'POST' , 'PUT' , 'DELETE' ],
935 description
=> "Method needs special privileges - only pvedaemon can execute it" ,
940 description
=> "A parameter name. If specified, all calls to this method are proxied to the host contained in that parameter." ,
945 description
=> "Required access permissions. By default only 'root' is allowed to access this method." ,
947 additionalProperties
=> 0 ,
950 description
=> "Describe access permissions." ,
954 description
=> "A simply way to allow access for 'all' authenticated users. Value 'world' is used to allow access without credentials." ,
956 enum
=> [ 'all' , 'world' ],
960 description
=> "Array of permission checks (prefix notation)." ,
967 description
=> "Used internally" ,
971 description
=> "Used internally" ,
976 description
=> "path for URL matching (uri template)" ,
978 fragmentDelimiter
=> {
980 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." ,
985 description
=> "JSON Schema for parameters." ,
990 description
=> "Used to store page formatter information (set by PVE::RESTHandler->register_page_formatter)." ,
995 description
=> "JSON Schema for return value." ,
1000 description
=> "method implementaion (code reference)" ,
1005 description
=> "Delegate call to this class (perl class string)." ,
1008 additionalProperties
=> 0 ,
1014 fragmentDelimiter
=> { optional
=> 1 }
1022 sub validate_schema
{
1025 my $errmsg = "internal error - unable to verify schema \n " ;
1026 validate
( $schema, $default_schema, $errmsg );
1029 sub validate_method_info
{
1032 my $errmsg = "internal error - unable to verify method info \n " ;
1033 validate
( $info, $method_schema, $errmsg );
1035 validate_schema
( $info ->{ parameters
}) if $info ->{ parameters
};
1036 validate_schema
( $info ->{ returns
}) if $info ->{ returns
};
1039 # run a self test on load
1040 # make sure we can verify the default schema
1041 validate_schema
( $default_schema_noref );
1042 validate_schema
( $method_schema );
1044 # and now some utility methods (used by pve api)
1045 sub method_get_child_link
{
1048 return undef if ! $info ;
1050 my $schema = $info ->{ returns
};
1051 return undef if ! $schema || ! $schema ->{ type
} || $schema ->{ type
} ne 'array' ;
1053 my $links = $schema ->{ links
};
1054 return undef if ! $links ;
1057 foreach my $lnk ( @$links ) {
1058 if ( $lnk ->{ href
} && $lnk ->{ rel
} && ( $lnk ->{ rel
} eq 'child' )) {
1067 # a way to parse command line parameters, using a
1068 # schema to configure Getopt::Long
1070 my ( $schema, $args, $arg_param, $fixed_param, $pwcallback ) = @_ ;
1072 if (! $schema || ! $schema ->{ properties
}) {
1073 raise
( "too many arguments \n " , code
=> HTTP_BAD_REQUEST
)
1074 if scalar ( @$args ) != 0 ;
1079 if ( $arg_param && ! ref ( $arg_param )) {
1080 my $pd = $schema ->{ properties
}->{ $arg_param };
1081 die "expected list format $pd ->{format}"
1082 if !( $pd && $pd ->{ format
} && $pd ->{ format
} =~ m/-list/ );
1083 $list_param = $arg_param ;
1087 foreach my $prop ( keys %{ $schema ->{ properties
}}) {
1088 my $pd = $schema ->{ properties
}->{ $prop };
1089 next if $list_param && $prop eq $list_param ;
1090 next if defined ( $fixed_param ->{ $prop });
1092 if ( $prop eq 'password' && $pwcallback ) {
1093 # we do not accept plain password on input line, instead
1094 # we turn this into a boolean option and ask for password below
1095 # using $pwcallback() (for security reasons).
1096 push @getopt, " $prop " ;
1097 } elsif ( $pd ->{ type
} eq 'boolean' ) {
1098 push @getopt, " $prop :s" ;
1100 if ( $pd ->{ format
} && $pd ->{ format
} =~ m/-a?list/ ) {
1101 push @getopt, " $prop =s@" ;
1103 push @getopt, " $prop =s" ;
1108 Getopt
:: Long
:: Configure
( 'prefix_pattern=(--|-)' );
1111 raise
( "unable to parse option \n " , code
=> HTTP_BAD_REQUEST
)
1112 if ! Getopt
:: Long
:: GetOptionsFromArray
( $args, $opts, @getopt );
1116 $opts ->{ $list_param } = $args ;
1118 } elsif ( ref ( $arg_param )) {
1119 foreach my $arg_name ( @$arg_param ) {
1120 if ( $opts ->{ 'extra-args' }) {
1121 raise
( "internal error: extra-args must be the last argument \n " , code
=> HTTP_BAD_REQUEST
);
1123 if ( $arg_name eq 'extra-args' ) {
1124 $opts ->{ 'extra-args' } = $args ;
1128 raise
( "not enough arguments \n " , code
=> HTTP_BAD_REQUEST
) if ! @$args ;
1129 $opts ->{ $arg_name } = shift @$args ;
1131 raise
( "too many arguments \n " , code
=> HTTP_BAD_REQUEST
) if @$args ;
1133 raise
( "too many arguments \n " , code
=> HTTP_BAD_REQUEST
)
1134 if scalar ( @$args ) != 0 ;
1138 if ( my $pd = $schema ->{ properties
}->{ password
}) {
1139 if ( $pd ->{ type
} ne 'boolean' && $pwcallback ) {
1140 if ( $opts ->{ password
} || ! $pd ->{ optional
}) {
1141 $opts ->{ password
} = & $pwcallback ();
1146 $opts = PVE
:: Tools
:: decode_utf8_parameters
( $opts );
1148 foreach my $p ( keys %$opts ) {
1149 if ( my $pd = $schema ->{ properties
}->{ $p }) {
1150 if ( $pd ->{ type
} eq 'boolean' ) {
1151 if ( $opts ->{ $p } eq '' ) {
1153 } elsif ( $opts ->{ $p } =~ m/^(1|true|yes|on)$/i ) {
1155 } elsif ( $opts ->{ $p } =~ m/^(0|false|no|off)$/i ) {
1158 raise
( "unable to parse boolean option \n " , code
=> HTTP_BAD_REQUEST
);
1160 } elsif ( $pd ->{ format
}) {
1162 if ( $pd ->{ format
} =~ m/-list/ ) {
1163 # allow --vmid 100 --vmid 101 and --vmid 100,101
1164 # allow --dow mon --dow fri and --dow mon,fri
1165 $opts ->{ $p } = join ( "," , @{ $opts ->{ $p }}) if ref ( $opts ->{ $p }) eq 'ARRAY' ;
1166 } elsif ( $pd ->{ format
} =~ m/-alist/ ) {
1167 # we encode array as \0 separated strings
1168 # Note: CGI.pm also use this encoding
1169 if ( scalar (@{ $opts ->{ $p }}) != 1 ) {
1170 $opts ->{ $p } = join ( "\0" , @{ $opts ->{ $p }});
1172 # st that split_list knows it is \0 terminated
1173 my $v = $opts ->{ $p }->[ 0 ];
1174 $opts ->{ $p } = " $v\0 " ;
1181 foreach my $p ( keys %$fixed_param ) {
1182 $opts ->{ $p } = $fixed_param ->{ $p };
1188 # A way to parse configuration data by giving a json schema
1190 my ( $schema, $filename, $raw ) = @_ ;
1192 # do fast check (avoid validate_schema($schema))
1193 die "got strange schema" if ! $schema ->{ type
} ||
1194 ! $schema ->{ properties
} || $schema ->{ type
} ne 'object' ;
1198 while ( $raw =~ /^\s*(.+?)\s*$/gm ) {
1201 next if $line =~ /^#/ ;
1203 if ( $line =~ m/^(\S+?):\s*(.*)$/ ) {
1206 if ( $schema ->{ properties
}->{ $key } &&
1207 $schema ->{ properties
}->{ $key }->{ type
} eq 'boolean' ) {
1209 $value = 1 if $value =~ m/^(1|on|yes|true)$/i ;
1210 $value = 0 if $value =~ m/^(0|off|no|false)$/i ;
1212 $cfg ->{ $key } = $value ;
1214 warn "ignore config line: $line\n "
1219 check_prop
( $cfg, $schema, '' , $errors );
1221 foreach my $k ( keys %$errors ) {
1222 warn "parse error in ' $filename ' - ' $k ': $errors ->{ $k } \n " ;
1229 # generate simple key/value file
1231 my ( $schema, $filename, $cfg ) = @_ ;
1233 # do fast check (avoid validate_schema($schema))
1234 die "got strange schema" if ! $schema ->{ type
} ||
1235 ! $schema ->{ properties
} || $schema ->{ type
} ne 'object' ;
1237 validate
( $cfg, $schema, "validation error in ' $filename ' \n " );
1241 foreach my $k ( keys %$cfg ) {
1242 $data .= " $k : $cfg ->{ $k } \n " ;
1248 sub generate_typetext
{
1251 my ( @optional, @required );
1252 foreach my $key ( sort keys %$schema ) {
1253 next if ! $schema ->{ $key }->{ format_description
};
1254 if ( $schema ->{ $key }->{ optional
}) {
1255 push @optional, $key ;
1257 push @required, $key ;
1260 my ( $pre, $post ) = ( '' , '' );
1261 foreach my $key ( @required ) {
1262 my $desc = $schema ->{ $key }->{ format_description
};
1263 $typetext .= " $pre$key =< $desc > $post " ;
1266 $pre = ' [,' if $pre ;
1267 foreach my $key ( @optional ) {
1268 my $desc = $schema ->{ $key }->{ format_description
};
1269 $typetext .= " $pre$key =< $desc > $post " ;