allow to pass undefined value to template_replace
[pve-common.git] / data / PVE / JSONSchema.pm
CommitLineData
e143e9d8
DM
1package PVE::JSONSchema;
2
3use warnings;
4use strict;
5use Storable; # for dclone
6use Getopt::Long;
7use Devel::Cycle -quiet; # todo: remove?
8use PVE::Tools qw(split_list);
9use PVE::Exception qw(raise);
10use HTTP::Status qw(:constants);
11
12use base 'Exporter';
13
14our @EXPORT_OK = qw(
15register_standard_option
16get_standard_option
17);
18
19# Note: This class implements something similar to JSON schema, but it is not 100% complete.
20# see: http://tools.ietf.org/html/draft-zyp-json-schema-02
21# see: http://json-schema.org/
22
23# the code is similar to the javascript parser from http://code.google.com/p/jsonschema/
24
25my $standard_options = {};
26sub register_standard_option {
27 my ($name, $schema) = @_;
28
29 die "standard option '$name' already registered\n"
30 if $standard_options->{$name};
31
32 $standard_options->{$name} = $schema;
33}
34
35sub get_standard_option {
36 my ($name, $base) = @_;
37
38 my $std = $standard_options->{$name};
39 die "no such standard option\n" if !$std;
40
41 my $res = $base || {};
42
43 foreach my $opt (keys %$std) {
44 next if $res->{$opt};
45 $res->{$opt} = $std->{$opt};
46 }
47
48 return $res;
49};
50
51register_standard_option('pve-vmid', {
52 description => "The (unique) ID of the VM.",
53 type => 'integer', format => 'pve-vmid',
54 minimum => 1
55});
56
57register_standard_option('pve-node', {
58 description => "The cluster node name.",
59 type => 'string', format => 'pve-node',
60});
61
62register_standard_option('pve-node-list', {
63 description => "List of cluster node names.",
64 type => 'string', format => 'pve-node-list',
65});
66
67register_standard_option('pve-iface', {
68 description => "Network interface name.",
69 type => 'string', format => 'pve-iface',
70 minLength => 2, maxLength => 20,
71});
72
73my $format_list = {};
74
75sub register_format {
76 my ($format, $code) = @_;
77
78 die "JSON schema format '$format' already registered\n"
79 if $format_list->{$format};
80
81 $format_list->{$format} = $code;
82}
83
84# register some common type for pve
8ba7c72b
DM
85
86register_format('string', sub {}); # allow format => 'string-list'
87
e143e9d8
DM
88register_format('pve-configid', \&pve_verify_configid);
89sub pve_verify_configid {
90 my ($id, $noerr) = @_;
91
92 if ($id !~ m/^[a-z][a-z0-9_]+$/i) {
93 return undef if $noerr;
94 die "invalid cofiguration ID '$id'\n";
95 }
96 return $id;
97}
98
99register_format('pve-vmid', \&pve_verify_vmid);
100sub pve_verify_vmid {
101 my ($vmid, $noerr) = @_;
102
103 if ($vmid !~ m/^[1-9][0-9]+$/) {
104 return undef if $noerr;
105 die "value does not look like a valid VM ID\n";
106 }
107 return $vmid;
108}
109
110register_format('pve-node', \&pve_verify_node_name);
111sub pve_verify_node_name {
112 my ($node, $noerr) = @_;
113
114 # todo: use better regex ?
115 if ($node !~ m/^[A-Za-z][[:alnum:]\-]*[[:alnum:]]+$/) {
116 return undef if $noerr;
117 die "value does not look like a valid node name\n";
118 }
119 return $node;
120}
121
122register_format('ipv4', \&pve_verify_ipv4);
123sub pve_verify_ipv4 {
124 my ($ipv4, $noerr) = @_;
125
126 if ($ipv4 !~ m/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ ||
127 !(($1 > 0) && ($1 < 255) &&
128 ($2 <= 255) && ($3 <= 255) &&
129 ($4 > 0) && ($4 < 255))) {
130 return undef if $noerr;
131 die "value does not look like a valid IP address\n";
132 }
133 return $ipv4;
134}
135register_format('ipv4mask', \&pve_verify_ipv4mask);
136sub pve_verify_ipv4mask {
137 my ($mask, $noerr) = @_;
138
139 if ($mask !~ m/^255\.255\.(\d{1,3})\.(\d{1,3})$/ ||
140 !(($1 <= 255) && ($2 <= 255))) {
141 return undef if $noerr;
142 die "value does not look like a valid IP netmask\n";
143 }
144 return $mask;
145}
146
147register_format('email', \&pve_verify_email);
148sub pve_verify_email {
149 my ($email, $noerr) = @_;
150
151 # we use same regex as extjs Ext.form.VTypes.email
152 if ($email !~ /^(\w+)([\-+.][\w]+)*@(\w[\-\w]*\.){1,5}([A-Za-z]){2,6}$/) {
153 return undef if $noerr;
154 die "value does not look like a valid email address\n";
155 }
156 return $email;
157}
158
159# network interface name
160register_format('pve-iface', \&pve_verify_iface);
161sub pve_verify_iface {
162 my ($id, $noerr) = @_;
163
164 if ($id !~ m/^[a-z][a-z0-9_]{1,20}([:\.]\d+)?$/i) {
165 return undef if $noerr;
166 die "invalid network interface name '$id'\n";
167 }
168 return $id;
169}
170
171sub check_format {
172 my ($format, $value) = @_;
173
174 return if $format eq 'regex';
175
23dc9401 176 if ($format =~ m/^(.*)-a?list$/) {
e143e9d8
DM
177
178 my $code = $format_list->{$1};
179
180 die "undefined format '$format'\n" if !$code;
181
182 # Note: we allow empty lists
183 foreach my $v (split_list($value)) {
184 &$code($v);
185 }
186
187 } elsif ($format =~ m/^(.*)-opt$/) {
188
189 my $code = $format_list->{$1};
190
191 die "undefined format '$format'\n" if !$code;
192
193 return if !$value; # allow empty string
194
195 &$code($value);
196
197 } else {
198
199 my $code = $format_list->{$format};
200
201 die "undefined format '$format'\n" if !$code;
202
203 &$code($value);
204 }
205}
206
207sub add_error {
208 my ($errors, $path, $msg) = @_;
209
210 $path = '_root' if !$path;
211
212 if ($errors->{$path}) {
213 $errors->{$path} = join ('\n', $errors->{$path}, $msg);
214 } else {
215 $errors->{$path} = $msg;
216 }
217}
218
219sub is_number {
220 my $value = shift;
221
222 # see 'man perlretut'
223 return $value =~ /^[+-]?(\d+\.\d+|\d+\.|\.\d+|\d+)([eE][+-]?\d+)?$/;
224}
225
226sub is_integer {
227 my $value = shift;
228
229 return $value =~ m/^[+-]?\d+$/;
230}
231
232sub check_type {
233 my ($path, $type, $value, $errors) = @_;
234
235 return 1 if !$type;
236
237 if (!defined($value)) {
238 return 1 if $type eq 'null';
239 die "internal error"
240 }
241
242 if (my $tt = ref($type)) {
243 if ($tt eq 'ARRAY') {
244 foreach my $t (@$type) {
245 my $tmperr = {};
246 check_type($path, $t, $value, $tmperr);
247 return 1 if !scalar(%$tmperr);
248 }
249 my $ttext = join ('|', @$type);
250 add_error($errors, $path, "type check ('$ttext') failed");
251 return undef;
252 } elsif ($tt eq 'HASH') {
253 my $tmperr = {};
254 check_prop($value, $type, $path, $tmperr);
255 return 1 if !scalar(%$tmperr);
256 add_error($errors, $path, "type check failed");
257 return undef;
258 } else {
259 die "internal error - got reference type '$tt'";
260 }
261
262 } else {
263
264 return 1 if $type eq 'any';
265
266 if ($type eq 'null') {
267 if (defined($value)) {
268 add_error($errors, $path, "type check ('$type') failed - value is not null");
269 return undef;
270 }
271 return 1;
272 }
273
274 my $vt = ref($value);
275
276 if ($type eq 'array') {
277 if (!$vt || $vt ne 'ARRAY') {
278 add_error($errors, $path, "type check ('$type') failed");
279 return undef;
280 }
281 return 1;
282 } elsif ($type eq 'object') {
283 if (!$vt || $vt ne 'HASH') {
284 add_error($errors, $path, "type check ('$type') failed");
285 return undef;
286 }
287 return 1;
288 } elsif ($type eq 'coderef') {
289 if (!$vt || $vt ne 'CODE') {
290 add_error($errors, $path, "type check ('$type') failed");
291 return undef;
292 }
293 return 1;
294 } else {
295 if ($vt) {
296 add_error($errors, $path, "type check ('$type') failed - got $vt");
297 return undef;
298 } else {
299 if ($type eq 'string') {
300 return 1; # nothing to check ?
301 } elsif ($type eq 'boolean') {
302 #if ($value =~ m/^(1|true|yes|on)$/i) {
303 if ($value eq '1') {
304 return 1;
305 #} elsif ($value =~ m/^(0|false|no|off)$/i) {
306 } elsif ($value eq '0') {
307 return 0;
308 } else {
309 add_error($errors, $path, "type check ('$type') failed - got '$value'");
310 return undef;
311 }
312 } elsif ($type eq 'integer') {
313 if (!is_integer($value)) {
314 add_error($errors, $path, "type check ('$type') failed - got '$value'");
315 return undef;
316 }
317 return 1;
318 } elsif ($type eq 'number') {
319 if (!is_number($value)) {
320 add_error($errors, $path, "type check ('$type') failed - got '$value'");
321 return undef;
322 }
323 return 1;
324 } else {
325 return 1; # no need to verify unknown types
326 }
327 }
328 }
329 }
330
331 return undef;
332}
333
334sub check_object {
335 my ($path, $schema, $value, $additional_properties, $errors) = @_;
336
337 # print "Check Object " . Dumper($value) . "\nSchema: " . Dumper($schema);
338
339 my $st = ref($schema);
340 if (!$st || $st ne 'HASH') {
341 add_error($errors, $path, "Invalid schema definition.");
342 return;
343 }
344
345 my $vt = ref($value);
346 if (!$vt || $vt ne 'HASH') {
347 add_error($errors, $path, "an object is required");
348 return;
349 }
350
351 foreach my $k (keys %$schema) {
352 check_prop($value->{$k}, $schema->{$k}, $path ? "$path.$k" : $k, $errors);
353 }
354
355 foreach my $k (keys %$value) {
356
357 my $newpath = $path ? "$path.$k" : $k;
358
359 if (my $subschema = $schema->{$k}) {
360 if (my $requires = $subschema->{requires}) {
361 if (ref($requires)) {
362 #print "TEST: " . Dumper($value) . "\n", Dumper($requires) ;
363 check_prop($value, $requires, $path, $errors);
364 } elsif (!defined($value->{$requires})) {
365 add_error($errors, $path ? "$path.$requires" : $requires,
366 "missing property - '$newpath' requiers this property");
367 }
368 }
369
370 next; # value is already checked above
371 }
372
373 if (defined ($additional_properties) && !$additional_properties) {
374 add_error($errors, $newpath, "property is not defined in schema " .
375 "and the schema does not allow additional properties");
376 next;
377 }
378 check_prop($value->{$k}, $additional_properties, $newpath, $errors)
379 if ref($additional_properties);
380 }
381}
382
383sub check_prop {
384 my ($value, $schema, $path, $errors) = @_;
385
386 die "internal error - no schema" if !$schema;
387 die "internal error" if !$errors;
388
389 #print "check_prop $path\n" if $value;
390
391 my $st = ref($schema);
392 if (!$st || $st ne 'HASH') {
393 add_error($errors, $path, "Invalid schema definition.");
394 return;
395 }
396
397 # if it extends another schema, it must pass that schema as well
398 if($schema->{extends}) {
399 check_prop($value, $schema->{extends}, $path, $errors);
400 }
401
402 if (!defined ($value)) {
403 return if $schema->{type} && $schema->{type} eq 'null';
404 if (!$schema->{optional}) {
405 add_error($errors, $path, "property is missing and it is not optional");
406 }
407 return;
408 }
409
410 return if !check_type($path, $schema->{type}, $value, $errors);
411
412 if ($schema->{disallow}) {
413 my $tmperr = {};
414 if (check_type($path, $schema->{disallow}, $value, $tmperr)) {
415 add_error($errors, $path, "disallowed value was matched");
416 return;
417 }
418 }
419
420 if (my $vt = ref($value)) {
421
422 if ($vt eq 'ARRAY') {
423 if ($schema->{items}) {
424 my $it = ref($schema->{items});
425 if ($it && $it eq 'ARRAY') {
426 #die "implement me $path: $vt " . Dumper($schema) ."\n". Dumper($value);
427 die "not implemented";
428 } else {
429 my $ind = 0;
430 foreach my $el (@$value) {
431 check_prop($el, $schema->{items}, "${path}[$ind]", $errors);
432 $ind++;
433 }
434 }
435 }
436 return;
437 } elsif ($schema->{properties} || $schema->{additionalProperties}) {
438 check_object($path, defined($schema->{properties}) ? $schema->{properties} : {},
439 $value, $schema->{additionalProperties}, $errors);
440 return;
441 }
442
443 } else {
444
445 if (my $format = $schema->{format}) {
446 eval { check_format($format, $value); };
447 if ($@) {
448 add_error($errors, $path, "invalid format - $@");
449 return;
450 }
451 }
452
453 if (my $pattern = $schema->{pattern}) {
454 if ($value !~ m/^$pattern$/) {
455 add_error($errors, $path, "value does not match the regex pattern");
456 return;
457 }
458 }
459
460 if (defined (my $max = $schema->{maxLength})) {
461 if (length($value) > $max) {
462 add_error($errors, $path, "value may only be $max characters long");
463 return;
464 }
465 }
466
467 if (defined (my $min = $schema->{minLength})) {
468 if (length($value) < $min) {
469 add_error($errors, $path, "value must be at least $min characters long");
470 return;
471 }
472 }
473
474 if (is_number($value)) {
475 if (defined (my $max = $schema->{maximum})) {
476 if ($value > $max) {
477 add_error($errors, $path, "value must have a maximum value of $max");
478 return;
479 }
480 }
481
482 if (defined (my $min = $schema->{minimum})) {
483 if ($value < $min) {
484 add_error($errors, $path, "value must have a minimum value of $min");
485 return;
486 }
487 }
488 }
489
490 if (my $ea = $schema->{enum}) {
491
492 my $found;
493 foreach my $ev (@$ea) {
494 if ($ev eq $value) {
495 $found = 1;
496 last;
497 }
498 }
499 if (!$found) {
500 add_error($errors, $path, "value '$value' does not have a value in the enumeration '" .
501 join(", ", @$ea) . "'");
502 }
503 }
504 }
505}
506
507sub validate {
508 my ($instance, $schema, $errmsg) = @_;
509
510 my $errors = {};
511 $errmsg = "Parameter verification failed.\n" if !$errmsg;
512
513 # todo: cycle detection is only needed for debugging, I guess
514 # we can disable that in the final release
515 # todo: is there a better/faster way to detect cycles?
516 my $cycles = 0;
517 find_cycle($instance, sub { $cycles = 1 });
518 if ($cycles) {
519 add_error($errors, undef, "data structure contains recursive cycles");
520 } elsif ($schema) {
521 check_prop($instance, $schema, '', $errors);
522 }
523
524 if (scalar(%$errors)) {
525 raise $errmsg, code => HTTP_BAD_REQUEST, errors => $errors;
526 }
527
528 return 1;
529}
530
531my $schema_valid_types = ["string", "object", "coderef", "array", "boolean", "number", "integer", "null", "any"];
532my $default_schema_noref = {
533 description => "This is the JSON Schema for JSON Schemas.",
534 type => [ "object" ],
535 additionalProperties => 0,
536 properties => {
537 type => {
538 type => ["string", "array"],
539 description => "This is a type definition value. This can be a simple type, or a union type",
540 optional => 1,
541 default => "any",
542 items => {
543 type => "string",
544 enum => $schema_valid_types,
545 },
546 enum => $schema_valid_types,
547 },
548 optional => {
549 type => "boolean",
550 description => "This indicates that the instance property in the instance object is not required.",
551 optional => 1,
552 default => 0
553 },
554 properties => {
555 type => "object",
556 description => "This is a definition for the properties of an object value",
557 optional => 1,
558 default => {},
559 },
560 items => {
561 type => "object",
562 description => "When the value is an array, this indicates the schema to use to validate each item in an array",
563 optional => 1,
564 default => {},
565 },
566 additionalProperties => {
567 type => [ "boolean", "object"],
568 description => "This provides a default property definition for all properties that are not explicitly defined in an object type definition.",
569 optional => 1,
570 default => {},
571 },
572 minimum => {
573 type => "number",
574 optional => 1,
575 description => "This indicates the minimum value for the instance property when the type of the instance value is a number.",
576 },
577 maximum => {
578 type => "number",
579 optional => 1,
580 description => "This indicates the maximum value for the instance property when the type of the instance value is a number.",
581 },
582 minLength => {
583 type => "integer",
584 description => "When the instance value is a string, this indicates minimum length of the string",
585 optional => 1,
586 minimum => 0,
587 default => 0,
588 },
589 maxLength => {
590 type => "integer",
591 description => "When the instance value is a string, this indicates maximum length of the string.",
592 optional => 1,
593 },
594 typetext => {
595 type => "string",
596 optional => 1,
597 description => "A text representation of the type (used to generate documentation).",
598 },
599 pattern => {
600 type => "string",
601 format => "regex",
602 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.",
603 optional => 1,
604 default => ".*",
605 },
606
607 enum => {
608 type => "array",
609 optional => 1,
610 description => "This provides an enumeration of possible values that are valid for the instance property.",
611 },
612 description => {
613 type => "string",
614 optional => 1,
615 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).",
616 },
617 title => {
618 type => "string",
619 optional => 1,
620 description => "This provides the title of the property",
621 },
622 requires => {
623 type => [ "string", "object" ],
624 optional => 1,
625 description => "indicates a required property or a schema that must be validated if this property is present",
626 },
627 format => {
628 type => "string",
629 optional => 1,
630 description => "This indicates what format the data is among some predefined formats which may include:\n\ndate - a string following the ISO format \naddress \nschema - a schema definition object \nperson \npage \nhtml - a string representing HTML",
631 },
632 default => {
633 type => "any",
634 optional => 1,
635 description => "This indicates the default for the instance property."
636 },
637 disallow => {
638 type => "object",
639 optional => 1,
640 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.",
641 },
642 extends => {
643 type => "object",
644 optional => 1,
645 description => "This indicates the schema extends the given schema. All instances of this schema must be valid to by the extended schema also.",
646 default => {},
647 },
648 # this is from hyper schema
649 links => {
650 type => "array",
651 description => "This defines the link relations of the instance objects",
652 optional => 1,
653 items => {
654 type => "object",
655 properties => {
656 href => {
657 type => "string",
658 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",
659 },
660 rel => {
661 type => "string",
662 description => "This is the name of the link relation",
663 optional => 1,
664 default => "full",
665 },
666 method => {
667 type => "string",
668 description => "For submission links, this defines the method that should be used to access the target resource",
669 optional => 1,
670 default => "GET",
671 },
672 },
673 },
674 },
675 }
676};
677
678my $default_schema = Storable::dclone($default_schema_noref);
679
680$default_schema->{properties}->{properties}->{additionalProperties} = $default_schema;
681$default_schema->{properties}->{additionalProperties}->{properties} = $default_schema->{properties};
682
683$default_schema->{properties}->{items}->{properties} = $default_schema->{properties};
684$default_schema->{properties}->{items}->{additionalProperties} = 0;
685
686$default_schema->{properties}->{disallow}->{properties} = $default_schema->{properties};
687$default_schema->{properties}->{disallow}->{additionalProperties} = 0;
688
689$default_schema->{properties}->{requires}->{properties} = $default_schema->{properties};
690$default_schema->{properties}->{requires}->{additionalProperties} = 0;
691
692$default_schema->{properties}->{extends}->{properties} = $default_schema->{properties};
693$default_schema->{properties}->{extends}->{additionalProperties} = 0;
694
695my $method_schema = {
696 type => "object",
697 additionalProperties => 0,
698 properties => {
699 description => {
700 description => "This a description of the method",
701 optional => 1,
702 },
703 name => {
704 type => 'string',
705 description => "This indicates the name of the function to call.",
706 optional => 1,
707 requires => {
708 additionalProperties => 1,
709 properties => {
710 name => {},
711 description => {},
712 code => {},
713 method => {},
714 parameters => {},
715 path => {},
716 parameters => {},
717 returns => {},
718 }
719 },
720 },
721 method => {
722 type => 'string',
723 description => "The HTTP method name.",
724 enum => [ 'GET', 'POST', 'PUT', 'DELETE' ],
725 optional => 1,
726 },
727 protected => {
728 type => 'boolean',
729 description => "Method needs special privileges - only pvedaemon can execute it",
730 optional => 1,
731 },
732 proxyto => {
733 type => 'string',
734 description => "A parameter name. If specified, all calls to this method are proxied to the host contained in that parameter.",
735 optional => 1,
736 },
737 permissions => {
738 type => 'object',
739 description => "Required access permissions. By default only 'root' is allowed to access this method.",
740 optional => 1,
741 additionalProperties => 0,
742 properties => {
b18d1722
DM
743 description => {
744 description => "Describe access permissions.",
745 optional => 1,
746 },
e143e9d8 747 user => {
b18d1722 748 description => "A simply way to allow access for 'all' authenticated users. Value 'world' is used to allow access without credentials.",
e143e9d8 749 type => 'string',
b18d1722 750 enum => ['all', 'world'],
e143e9d8
DM
751 optional => 1,
752 },
b18d1722
DM
753 check => {
754 description => "Array of permission checks (prefix notation).",
755 type => 'array',
756 optional => 1
757 },
e143e9d8
DM
758 },
759 },
760 match_name => {
761 description => "Used internally",
762 optional => 1,
763 },
764 match_re => {
765 description => "Used internally",
766 optional => 1,
767 },
768 path => {
769 type => 'string',
770 description => "path for URL matching (uri template)",
771 },
772 fragmentDelimiter => {
773 type => 'string',
774 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.",
775 optional => 1,
776 },
777 parameters => {
778 type => 'object',
779 description => "JSON Schema for parameters.",
780 optional => 1,
781 },
782 returns => {
783 type => 'object',
784 description => "JSON Schema for return value.",
785 optional => 1,
786 },
787 code => {
788 type => 'coderef',
789 description => "method implementaion (code reference)",
790 optional => 1,
791 },
792 subclass => {
793 type => 'string',
794 description => "Delegate call to this class (perl class string).",
795 optional => 1,
796 requires => {
797 additionalProperties => 0,
798 properties => {
799 subclass => {},
800 path => {},
801 match_name => {},
802 match_re => {},
803 fragmentDelimiter => { optional => 1 }
804 }
805 },
806 },
807 },
808
809};
810
811sub validate_schema {
812 my ($schema) = @_;
813
814 my $errmsg = "internal error - unable to verify schema\n";
815 validate($schema, $default_schema, $errmsg);
816}
817
818sub validate_method_info {
819 my $info = shift;
820
821 my $errmsg = "internal error - unable to verify method info\n";
822 validate($info, $method_schema, $errmsg);
823
824 validate_schema($info->{parameters}) if $info->{parameters};
825 validate_schema($info->{returns}) if $info->{returns};
826}
827
828# run a self test on load
829# make sure we can verify the default schema
830validate_schema($default_schema_noref);
831validate_schema($method_schema);
832
833# and now some utility methods (used by pve api)
834sub method_get_child_link {
835 my ($info) = @_;
836
837 return undef if !$info;
838
839 my $schema = $info->{returns};
840 return undef if !$schema || !$schema->{type} || $schema->{type} ne 'array';
841
842 my $links = $schema->{links};
843 return undef if !$links;
844
845 my $found;
846 foreach my $lnk (@$links) {
847 if ($lnk->{href} && $lnk->{rel} && ($lnk->{rel} eq 'child')) {
848 $found = $lnk;
849 last;
850 }
851 }
852
853 return $found;
854}
855
856# a way to parse command line parameters, using a
857# schema to configure Getopt::Long
858sub get_options {
0ce82909 859 my ($schema, $args, $arg_param, $fixed_param, $pwcallback) = @_;
e143e9d8
DM
860
861 if (!$schema || !$schema->{properties}) {
862 raise("too many arguments\n", code => HTTP_BAD_REQUEST)
863 if scalar(@$args) != 0;
864 return {};
865 }
866
0ce82909
DM
867 my $list_param;
868 if ($arg_param && !ref($arg_param)) {
869 my $pd = $schema->{properties}->{$arg_param};
870 die "expected list format $pd->{format}"
871 if !($pd && $pd->{format} && $pd->{format} =~ m/-list/);
872 $list_param = $arg_param;
873 }
874
e143e9d8
DM
875 my @getopt = ();
876 foreach my $prop (keys %{$schema->{properties}}) {
877 my $pd = $schema->{properties}->{$prop};
aab47b58 878 next if $list_param && $prop eq $list_param;
0ce82909 879 next if defined($fixed_param->{$prop});
e143e9d8
DM
880
881 if ($prop eq 'password' && $pwcallback) {
882 # we do not accept plain password on input line, instead
883 # we turn this into a boolean option and ask for password below
884 # using $pwcallback() (for security reasons).
885 push @getopt, "$prop";
886 } elsif ($pd->{type} eq 'boolean') {
887 push @getopt, "$prop:s";
888 } else {
23dc9401 889 if ($pd->{format} && $pd->{format} =~ m/-a?list/) {
8ba7c72b
DM
890 push @getopt, "$prop=s@";
891 } else {
892 push @getopt, "$prop=s";
893 }
e143e9d8
DM
894 }
895 }
896
897 my $opts = {};
898 raise("unable to parse option\n", code => HTTP_BAD_REQUEST)
899 if !Getopt::Long::GetOptionsFromArray($args, $opts, @getopt);
1d21344c 900
0ce82909
DM
901 if (my $acount = scalar(@$args)) {
902 if ($list_param) {
903 $opts->{$list_param} = $args;
904 $args = [];
905 } elsif (ref($arg_param)) {
906 raise("wrong number of arguments\n", code => HTTP_BAD_REQUEST)
907 if scalar(@$arg_param) != $acount;
908 foreach my $p (@$arg_param) {
909 $opts->{$p} = shift @$args;
910 }
911 } else {
912 raise("too many arguments\n", code => HTTP_BAD_REQUEST)
913 if scalar(@$args) != 0;
914 }
1d21344c
DM
915 }
916
e143e9d8
DM
917 if (my $pd = $schema->{properties}->{password}) {
918 if ($pd->{type} ne 'boolean' && $pwcallback) {
919 if ($opts->{password} || !$pd->{optional}) {
920 $opts->{password} = &$pwcallback();
921 }
922 }
923 }
815b2aba
DM
924
925 $opts = PVE::Tools::decode_utf8_parameters($opts);
926 if ($opts->{description}) {
927 print "TEST: " . PVE::Tools::encode_text($opts->{description}) . "\n";
928 }
929
e143e9d8
DM
930 foreach my $p (keys %$opts) {
931 if (my $pd = $schema->{properties}->{$p}) {
932 if ($pd->{type} eq 'boolean') {
933 if ($opts->{$p} eq '') {
934 $opts->{$p} = 1;
935 } elsif ($opts->{$p} =~ m/^(1|true|yes|on)$/i) {
936 $opts->{$p} = 1;
937 } elsif ($opts->{$p} =~ m/^(0|false|no|off)$/i) {
938 $opts->{$p} = 0;
939 } else {
940 raise("unable to parse boolean option\n", code => HTTP_BAD_REQUEST);
941 }
23dc9401 942 } elsif ($pd->{format}) {
8ba7c72b 943
23dc9401 944 if ($pd->{format} =~ m/-list/) {
8ba7c72b 945 # allow --vmid 100 --vmid 101 and --vmid 100,101
23dc9401 946 # allow --dow mon --dow fri and --dow mon,fri
8ba7c72b 947 $opts->{$p} = join(",", @{$opts->{$p}});
23dc9401 948 } elsif ($pd->{format} =~ m/-alist/) {
8ba7c72b
DM
949 # we encode array as \0 separated strings
950 # Note: CGI.pm also use this encoding
951 if (scalar(@{$opts->{$p}}) != 1) {
952 $opts->{$p} = join("\0", @{$opts->{$p}});
953 } else {
954 # st that split_list knows it is \0 terminated
955 my $v = $opts->{$p}->[0];
956 $opts->{$p} = "$v\0";
957 }
958 }
e143e9d8
DM
959 }
960 }
961 }
962
0ce82909
DM
963 foreach my $p (keys %$fixed_param) {
964 $opts->{$p} = $fixed_param->{$p};
e143e9d8
DM
965 }
966
967 return $opts;
968}
969
970# A way to parse configuration data by giving a json schema
971sub parse_config {
972 my ($schema, $filename, $raw) = @_;
973
974 # do fast check (avoid validate_schema($schema))
975 die "got strange schema" if !$schema->{type} ||
976 !$schema->{properties} || $schema->{type} ne 'object';
977
978 my $cfg = {};
979
980 while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
981 my $line = $1;
982
983 next if $line =~ m/^\#/; # skip comment lines
984 next if $line =~ m/^\s*$/; # skip empty lines
985
986 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
987 my $key = $1;
988 my $value = $2;
989 if ($schema->{properties}->{$key} &&
990 $schema->{properties}->{$key}->{type} eq 'boolean') {
991
992 $value = 1 if $value =~ m/^(1|on|yes|true)$/i;
993 $value = 0 if $value =~ m/^(0|off|no|false)$/i;
994 }
995 $cfg->{$key} = $value;
996 } else {
997 warn "ignore config line: $line\n"
998 }
999 }
1000
1001 my $errors = {};
1002 check_prop($cfg, $schema, '', $errors);
1003
1004 foreach my $k (keys %$errors) {
1005 warn "parse error in '$filename' - '$k': $errors->{$k}\n";
1006 delete $cfg->{$k};
1007 }
1008
1009 return $cfg;
1010}
1011
1012# generate simple key/value file
1013sub dump_config {
1014 my ($schema, $filename, $cfg) = @_;
1015
1016 # do fast check (avoid validate_schema($schema))
1017 die "got strange schema" if !$schema->{type} ||
1018 !$schema->{properties} || $schema->{type} ne 'object';
1019
1020 validate($cfg, $schema, "validation error in '$filename'\n");
1021
1022 my $data = '';
1023
1024 foreach my $k (keys %$cfg) {
1025 $data .= "$k: $cfg->{$k}\n";
1026 }
1027
1028 return $data;
1029}
1030
10311;