]>
Commit | Line | Data |
---|---|---|
e143e9d8 DM |
1 | package PVE::RESTHandler; |
2 | ||
3 | use strict; | |
4 | no strict 'refs'; # our autoload requires this | |
e143e9d8 DM |
5 | use warnings; |
6 | use PVE::SafeSyslog; | |
7 | use PVE::Exception qw(raise raise_param_exc); | |
8 | use PVE::JSONSchema; | |
dfc6cff8 | 9 | use PVE::Tools; |
e143e9d8 DM |
10 | use HTTP::Status qw(:constants :is status_message); |
11 | use Text::Wrap; | |
4be9b5f7 | 12 | use Clone qw(clone); |
e143e9d8 DM |
13 | |
14 | my $method_registry = {}; | |
15 | my $method_by_name = {}; | |
c1089794 | 16 | my $method_path_lookup = {}; |
e143e9d8 DM |
17 | |
18 | our $AUTOLOAD; # it's a package global | |
19 | ||
62c66c47 DM |
20 | our $standard_output_options = { |
21 | 'output-format' => PVE::JSONSchema::get_standard_option('pve-output-format'), | |
22 | noheader => { | |
23 | description => "Do not show column headers (for 'text' format).", | |
24 | type => 'boolean', | |
25 | optional => 1, | |
26 | default => 1, | |
27 | }, | |
28 | noborder => { | |
29 | description => "Do not draw borders (for 'text' format).", | |
30 | type => 'boolean', | |
31 | optional => 1, | |
32 | default => 1, | |
33 | }, | |
34 | quiet => { | |
35 | description => "Suppress printing results.", | |
36 | type => 'boolean', | |
37 | optional => 1, | |
38 | }, | |
39 | 'human-readable' => { | |
40 | description => "Call output rendering functions to produce human readable text.", | |
41 | type => 'boolean', | |
42 | optional => 1, | |
43 | default => 1, | |
44 | } | |
45 | }; | |
46 | ||
e143e9d8 DM |
47 | sub api_clone_schema { |
48 | my ($schema) = @_; | |
49 | ||
50 | my $res = {}; | |
51 | my $ref = ref($schema); | |
52 | die "not a HASH reference" if !($ref && $ref eq 'HASH'); | |
53 | ||
54 | foreach my $k (keys %$schema) { | |
55 | my $d = $schema->{$k}; | |
56 | if ($k ne 'properties') { | |
4be9b5f7 | 57 | $res->{$k} = ref($d) ? clone($d) : $d; |
e143e9d8 DM |
58 | next; |
59 | } | |
60 | # convert indexed parameters like -net\d+ to -net[n] | |
61 | foreach my $p (keys %$d) { | |
62 | my $pd = $d->{$p}; | |
63 | if ($p =~ m/^([a-z]+)(\d+)$/) { | |
b546f33e | 64 | my ($name, $idx) = ($1, $2); |
b54ad320 | 65 | if ($idx == 0 && defined($d->{"${name}1"})) { |
b546f33e | 66 | $p = "${name}[n]"; |
34d89996 | 67 | } elsif ($idx > 0 && defined($d->{"${name}0"})) { |
2c2a5fd3 | 68 | next; # only handle once for -xx0, but only if -xx0 exists |
e143e9d8 DM |
69 | } |
70 | } | |
534d4270 DM |
71 | my $tmp = ref($pd) ? clone($pd) : $pd; |
72 | # NOTE: add typetext property for more complex types, to | |
73 | # make the web api viewer code simpler | |
74 | if (!(defined($tmp->{enum}) || defined($tmp->{pattern}))) { | |
75 | my $typetext = PVE::JSONSchema::schema_get_type_text($tmp); | |
76 | if ($tmp->{type} && ($tmp->{type} ne $typetext)) { | |
77 | $tmp->{typetext} = $typetext; | |
78 | } | |
79 | } | |
80 | $res->{$k}->{$p} = $tmp; | |
e143e9d8 DM |
81 | } |
82 | } | |
83 | ||
84 | return $res; | |
85 | } | |
86 | ||
87 | sub api_dump_full { | |
1e5ecf7b | 88 | my ($tree, $index, $class, $prefix, $raw_dump) = @_; |
e143e9d8 DM |
89 | |
90 | $prefix = '' if !$prefix; | |
91 | ||
92 | my $ma = $method_registry->{$class}; | |
93 | ||
94 | foreach my $info (@$ma) { | |
95 | ||
96 | my $path = "$prefix/$info->{path}"; | |
97 | $path =~ s/\/+$//; | |
98 | ||
99 | if ($info->{subclass}) { | |
1e5ecf7b | 100 | api_dump_full($tree, $index, $info->{subclass}, $path, $raw_dump); |
e143e9d8 DM |
101 | } else { |
102 | next if !$path; | |
103 | ||
104 | # check if method is unique | |
105 | my $realpath = $path; | |
106 | $realpath =~ s/\{[^\}]+\}/\{\}/g; | |
107 | my $fullpath = "$info->{method} $realpath"; | |
108 | die "duplicate path '$realpath'" if $index->{$fullpath}; | |
109 | $index->{$fullpath} = $info; | |
110 | ||
111 | # insert into tree | |
112 | my $treedir = $tree; | |
113 | my $res; | |
114 | my $sp = ''; | |
115 | foreach my $dir (split('/', $path)) { | |
116 | next if !$dir; | |
117 | $sp .= "/$dir"; | |
118 | $res = (grep { $_->{text} eq $dir } @$treedir)[0]; | |
119 | if ($res) { | |
120 | $res->{children} = [] if !$res->{children}; | |
121 | $treedir = $res->{children}; | |
122 | } else { | |
123 | $res = { | |
124 | path => $sp, | |
125 | text => $dir, | |
126 | children => [], | |
127 | }; | |
128 | push @$treedir, $res; | |
129 | $treedir = $res->{children}; | |
130 | } | |
131 | } | |
132 | ||
133 | if ($res) { | |
134 | my $data = {}; | |
135 | foreach my $k (keys %$info) { | |
136 | next if $k eq 'code' || $k eq "match_name" || $k eq "match_re" || | |
137 | $k eq "path"; | |
138 | ||
139 | my $d = $info->{$k}; | |
e143e9d8 | 140 | |
1e5ecf7b DM |
141 | if ($raw_dump) { |
142 | $data->{$k} = $d; | |
143 | } else { | |
144 | if ($k eq 'parameters') { | |
145 | $data->{$k} = api_clone_schema($d); | |
146 | } else { | |
147 | $data->{$k} = ref($d) ? clone($d) : $d; | |
148 | } | |
e143e9d8 DM |
149 | } |
150 | } | |
151 | $res->{info}->{$info->{method}} = $data; | |
152 | }; | |
153 | } | |
154 | } | |
155 | }; | |
156 | ||
157 | sub api_dump_cleanup_tree { | |
158 | my ($tree) = @_; | |
159 | ||
160 | foreach my $rec (@$tree) { | |
161 | delete $rec->{children} if $rec->{children} && !scalar(@{$rec->{children}}); | |
162 | if ($rec->{children}) { | |
163 | $rec->{leaf} = 0; | |
164 | api_dump_cleanup_tree($rec->{children}); | |
165 | } else { | |
166 | $rec->{leaf} = 1; | |
167 | } | |
168 | } | |
169 | ||
170 | } | |
171 | ||
903ea3ff DM |
172 | # api_dump_remove_refs: prepare API tree for use with to_json($tree) |
173 | sub api_dump_remove_refs { | |
174 | my ($tree) = @_; | |
175 | ||
176 | my $class = ref($tree); | |
177 | return $tree if !$class; | |
178 | ||
179 | if ($class eq 'ARRAY') { | |
180 | my $res = []; | |
181 | foreach my $el (@$tree) { | |
182 | push @$res, api_dump_remove_refs($el); | |
183 | } | |
184 | return $res; | |
185 | } elsif ($class eq 'HASH') { | |
186 | my $res = {}; | |
187 | foreach my $k (keys %$tree) { | |
188 | if (my $itemclass = ref($tree->{$k})) { | |
189 | if ($itemclass eq 'CODE') { | |
190 | next if $k eq 'completion'; | |
191 | } | |
192 | $res->{$k} = api_dump_remove_refs($tree->{$k}); | |
193 | } else { | |
194 | $res->{$k} = $tree->{$k}; | |
195 | } | |
196 | } | |
197 | return $res; | |
198 | } elsif ($class eq 'Regexp') { | |
199 | return "$tree"; # return string representation | |
200 | } else { | |
201 | die "unknown class '$class'\n"; | |
202 | } | |
203 | } | |
204 | ||
e143e9d8 | 205 | sub api_dump { |
1e5ecf7b | 206 | my ($class, $prefix, $raw_dump) = @_; |
e143e9d8 DM |
207 | |
208 | my $tree = []; | |
209 | ||
210 | my $index = {}; | |
1e5ecf7b | 211 | api_dump_full($tree, $index, $class, $prefix, $raw_dump); |
e143e9d8 DM |
212 | api_dump_cleanup_tree($tree); |
213 | return $tree; | |
214 | }; | |
215 | ||
216 | sub validate_method_schemas { | |
217 | ||
218 | foreach my $class (keys %$method_registry) { | |
219 | my $ma = $method_registry->{$class}; | |
220 | ||
221 | foreach my $info (@$ma) { | |
222 | PVE::JSONSchema::validate_method_info($info); | |
223 | } | |
224 | } | |
225 | } | |
226 | ||
227 | sub register_method { | |
228 | my ($self, $info) = @_; | |
229 | ||
230 | my $match_re = []; | |
231 | my $match_name = []; | |
232 | ||
c1089794 DM |
233 | my $errprefix; |
234 | ||
235 | my $method; | |
236 | if ($info->{subclass}) { | |
237 | $errprefix = "register subclass $info->{subclass} at ${self}/$info->{path} -"; | |
238 | $method = 'SUBCLASS'; | |
239 | } else { | |
240 | $errprefix = "register method ${self}/$info->{path} -"; | |
241 | $info->{method} = 'GET' if !$info->{method}; | |
242 | $method = $info->{method}; | |
243 | } | |
244 | ||
245 | $method_path_lookup->{$self} = {} if !defined($method_path_lookup->{$self}); | |
246 | my $path_lookup = $method_path_lookup->{$self}; | |
247 | ||
248 | die "$errprefix no path" if !defined($info->{path}); | |
249 | ||
e143e9d8 | 250 | foreach my $comp (split(/\/+/, $info->{path})) { |
c1089794 DM |
251 | die "$errprefix path compoment has zero length\n" if $comp eq ''; |
252 | my ($name, $regex); | |
e143e9d8 | 253 | if ($comp =~ m/^\{(\w+)(:(.*))?\}$/) { |
c1089794 DM |
254 | $name = $1; |
255 | $regex = $3 ? $3 : '\S+'; | |
256 | push @$match_re, $regex; | |
257 | push @$match_name, $name; | |
e143e9d8 | 258 | } else { |
c1089794 DM |
259 | $name = $comp; |
260 | push @$match_re, $name; | |
261 | push @$match_name, undef; | |
262 | } | |
263 | ||
264 | if ($regex) { | |
265 | $path_lookup->{regex} = {} if !defined($path_lookup->{regex}); | |
266 | ||
267 | my $old_name = $path_lookup->{regex}->{match_name}; | |
268 | die "$errprefix found changed regex match name\n" | |
269 | if defined($old_name) && ($old_name ne $name); | |
270 | my $old_re = $path_lookup->{regex}->{match_re}; | |
271 | die "$errprefix found changed regex\n" | |
272 | if defined($old_re) && ($old_re ne $regex); | |
273 | $path_lookup->{regex}->{match_name} = $name; | |
274 | $path_lookup->{regex}->{match_re} = $regex; | |
275 | ||
276 | die "$errprefix path match error - regex and fixed items\n" | |
277 | if defined($path_lookup->{folders}); | |
278 | ||
279 | $path_lookup = $path_lookup->{regex}; | |
280 | ||
281 | } else { | |
282 | $path_lookup->{folders}->{$name} = {} if !defined($path_lookup->{folders}->{$name}); | |
283 | ||
284 | die "$errprefix path match error - regex and fixed items\n" | |
285 | if defined($path_lookup->{regex}); | |
286 | ||
287 | $path_lookup = $path_lookup->{folders}->{$name}; | |
e143e9d8 DM |
288 | } |
289 | } | |
290 | ||
c1089794 DM |
291 | die "$errprefix duplicate method definition\n" |
292 | if defined($path_lookup->{$method}); | |
293 | ||
1dad1ca5 DM |
294 | if ($method eq 'SUBCLASS') { |
295 | foreach my $m (qw(GET PUT POST DELETE)) { | |
296 | die "$errprefix duplicate method definition SUBCLASS and $m\n" if $path_lookup->{$m}; | |
297 | } | |
298 | } | |
c1089794 DM |
299 | $path_lookup->{$method} = $info; |
300 | ||
e143e9d8 DM |
301 | $info->{match_re} = $match_re; |
302 | $info->{match_name} = $match_name; | |
303 | ||
304 | $method_by_name->{$self} = {} if !defined($method_by_name->{$self}); | |
305 | ||
306 | if ($info->{name}) { | |
c1089794 | 307 | die "$errprefix method name already defined\n" |
e143e9d8 DM |
308 | if defined($method_by_name->{$self}->{$info->{name}}); |
309 | ||
310 | $method_by_name->{$self}->{$info->{name}} = $info; | |
311 | } | |
312 | ||
313 | push @{$method_registry->{$self}}, $info; | |
314 | } | |
315 | ||
f1fb34a0 DM |
316 | sub DESTROY {}; # avoid problems with autoload |
317 | ||
e143e9d8 DM |
318 | sub AUTOLOAD { |
319 | my ($this) = @_; | |
320 | ||
321 | # also see "man perldiag" | |
322 | ||
323 | my $sub = $AUTOLOAD; | |
324 | (my $method = $sub) =~ s/.*:://; | |
325 | ||
e143e9d8 DM |
326 | my $info = $this->map_method_by_name($method); |
327 | ||
328 | *{$sub} = sub { | |
329 | my $self = shift; | |
330 | return $self->handle($info, @_); | |
331 | }; | |
332 | goto &$AUTOLOAD; | |
333 | } | |
334 | ||
335 | sub method_attributes { | |
336 | my ($self) = @_; | |
337 | ||
338 | return $method_registry->{$self}; | |
339 | } | |
340 | ||
341 | sub map_method_by_name { | |
342 | my ($self, $name) = @_; | |
343 | ||
344 | my $info = $method_by_name->{$self}->{$name}; | |
345 | die "no such method '${self}::$name'\n" if !$info; | |
346 | ||
347 | return $info; | |
348 | } | |
349 | ||
c1089794 | 350 | sub map_path_to_methods { |
39313ced | 351 | my ($class, $stack, $uri_param, $pathmatchref) = @_; |
e143e9d8 | 352 | |
c1089794 | 353 | my $path_lookup = $method_path_lookup->{$class}; |
e143e9d8 | 354 | |
39313ced DM |
355 | # Note: $pathmatchref can be used to obtain path including |
356 | # uri patterns like '/cluster/firewall/groups/{group}'. | |
357 | # Used by pvesh to display help | |
358 | if (defined($pathmatchref)) { | |
359 | $$pathmatchref = '' if !$$pathmatchref; | |
360 | } | |
361 | ||
844a246d | 362 | while (defined(my $comp = shift @$stack)) { |
c1089794 DM |
363 | return undef if !$path_lookup; # not registerd? |
364 | if ($path_lookup->{regex}) { | |
365 | my $name = $path_lookup->{regex}->{match_name}; | |
366 | my $regex = $path_lookup->{regex}->{match_re}; | |
e143e9d8 | 367 | |
c1089794 DM |
368 | return undef if $comp !~ m/^($regex)$/; |
369 | $uri_param->{$name} = $1; | |
370 | $path_lookup = $path_lookup->{regex}; | |
39313ced | 371 | $$pathmatchref .= '/{' . $name . '}' if defined($pathmatchref); |
c1089794 DM |
372 | } elsif ($path_lookup->{folders}) { |
373 | $path_lookup = $path_lookup->{folders}->{$comp}; | |
39313ced | 374 | $$pathmatchref .= '/' . $comp if defined($pathmatchref); |
e143e9d8 | 375 | } else { |
c1089794 | 376 | die "internal error"; |
e143e9d8 | 377 | } |
c1089794 DM |
378 | |
379 | return undef if !$path_lookup; | |
e143e9d8 | 380 | |
c1089794 DM |
381 | if (my $info = $path_lookup->{SUBCLASS}) { |
382 | $class = $info->{subclass}; | |
e143e9d8 | 383 | |
c1089794 | 384 | my $fd = $info->{fragmentDelimiter}; |
e143e9d8 | 385 | |
c1089794 DM |
386 | if (defined($fd)) { |
387 | # we only support the empty string '' (match whole URI) | |
388 | die "unsupported fragmentDelimiter '$fd'" | |
389 | if $fd ne ''; | |
e143e9d8 | 390 | |
c1089794 DM |
391 | $stack = [ join ('/', @$stack) ] if scalar(@$stack) > 1; |
392 | } | |
393 | $path_lookup = $method_path_lookup->{$class}; | |
e143e9d8 | 394 | } |
c1089794 | 395 | } |
e143e9d8 | 396 | |
c1089794 | 397 | return undef if !$path_lookup; |
e143e9d8 | 398 | |
c1089794 DM |
399 | return ($class, $path_lookup); |
400 | } | |
e143e9d8 | 401 | |
c1089794 | 402 | sub find_handler { |
39313ced | 403 | my ($class, $method, $path, $uri_param, $pathmatchref) = @_; |
e143e9d8 | 404 | |
c1089794 | 405 | my $stack = [ grep { length($_) > 0 } split('\/+' , $path)]; # skip empty fragments |
e143e9d8 | 406 | |
c1089794 DM |
407 | my ($handler_class, $path_info); |
408 | eval { | |
39313ced | 409 | ($handler_class, $path_info) = $class->map_path_to_methods($stack, $uri_param, $pathmatchref); |
c1089794 DM |
410 | }; |
411 | my $err = $@; | |
412 | syslog('err', $err) if $err; | |
e143e9d8 | 413 | |
c1089794 | 414 | return undef if !($handler_class && $path_info); |
e143e9d8 | 415 | |
c1089794 | 416 | my $method_info = $path_info->{$method}; |
e143e9d8 | 417 | |
c1089794 | 418 | return undef if !$method_info; |
e143e9d8 | 419 | |
c1089794 | 420 | return ($handler_class, $method_info); |
e143e9d8 DM |
421 | } |
422 | ||
423 | sub handle { | |
4cbcd138 | 424 | my ($self, $info, $param) = @_; |
e143e9d8 DM |
425 | |
426 | my $func = $info->{code}; | |
427 | ||
428 | if (!($info->{name} && $func)) { | |
429 | raise("Method lookup failed ('$info->{name}')\n", | |
430 | code => HTTP_INTERNAL_SERVER_ERROR); | |
431 | } | |
432 | ||
433 | if (my $schema = $info->{parameters}) { | |
434 | # warn "validate ". Dumper($param}) . "\n" . Dumper($schema); | |
435 | PVE::JSONSchema::validate($param, $schema); | |
436 | # untaint data (already validated) | |
5851be88 | 437 | my $extra = delete $param->{'extra-args'}; |
e143e9d8 DM |
438 | while (my ($key, $val) = each %$param) { |
439 | ($param->{$key}) = $val =~ /^(.*)$/s; | |
440 | } | |
5851be88 | 441 | $param->{'extra-args'} = [map { /^(.*)$/ } @$extra] if $extra; |
e143e9d8 DM |
442 | } |
443 | ||
4cbcd138 | 444 | my $result = &$func($param); |
e143e9d8 DM |
445 | |
446 | # todo: this is only to be safe - disable? | |
447 | if (my $schema = $info->{returns}) { | |
9030a7d7 | 448 | PVE::JSONSchema::validate($result, $schema, "Result verification failed\n"); |
e143e9d8 DM |
449 | } |
450 | ||
451 | return $result; | |
452 | } | |
453 | ||
8fa050cd | 454 | # format option, display type and description |
921f91d1 DM |
455 | # $name: option name |
456 | # $display_name: for example "-$name" of "<$name>", pass undef to use "$name:" | |
8fa050cd | 457 | # $phash: json schema property hash |
b0e5e9fa | 458 | # $format: 'asciidoc', 'short', 'long' or 'full' |
abc1afd8 | 459 | # $style: 'config', 'config-sub', 'arg' or 'fixed' |
dfc6cff8 | 460 | # $mapdef: parameter mapping ({ desc => XXX, func => sub {...} }) |
8fa050cd | 461 | my $get_property_description = sub { |
4842b651 | 462 | my ($name, $style, $phash, $format, $mapdef) = @_; |
8fa050cd DM |
463 | |
464 | my $res = ''; | |
465 | ||
466 | $format = 'asciidoc' if !defined($format); | |
467 | ||
468 | my $descr = $phash->{description} || "no description available"; | |
469 | ||
32f8e0c7 DM |
470 | if ($phash->{verbose_description} && |
471 | ($style eq 'config' || $style eq 'config-sub')) { | |
472 | $descr = $phash->{verbose_description}; | |
473 | } | |
474 | ||
8fa050cd DM |
475 | chomp $descr; |
476 | ||
77096a6f | 477 | my $type_text = PVE::JSONSchema::schema_get_type_text($phash, $style); |
8fa050cd | 478 | |
dfc6cff8 DM |
479 | if ($mapdef && $phash->{type} eq 'string') { |
480 | $type_text = $mapdef->{desc}; | |
4845032a FG |
481 | } |
482 | ||
8fa050cd DM |
483 | if ($format eq 'asciidoc') { |
484 | ||
df79b428 | 485 | if ($style eq 'config') { |
921f91d1 | 486 | $res .= "`$name`: "; |
32f8e0c7 DM |
487 | } elsif ($style eq 'config-sub') { |
488 | $res .= "`$name`="; | |
df79b428 | 489 | } elsif ($style eq 'arg') { |
a4f181a3 | 490 | $res .= "`--$name` "; |
df79b428 | 491 | } elsif ($style eq 'fixed') { |
abc1afd8 | 492 | $res .= "`<$name>`: "; |
df79b428 DM |
493 | } else { |
494 | die "unknown style '$style'"; | |
8fa050cd DM |
495 | } |
496 | ||
77096a6f | 497 | $res .= "`$type_text` " if $type_text; |
8fa050cd DM |
498 | |
499 | if (defined(my $dv = $phash->{default})) { | |
25d9bda9 | 500 | $res .= "('default =' `$dv`)"; |
8fa050cd | 501 | } |
a736ac99 | 502 | |
32f8e0c7 DM |
503 | if ($style eq 'config-sub') { |
504 | $res .= ";;\n\n"; | |
505 | } else { | |
506 | $res .= "::\n\n"; | |
507 | } | |
508 | ||
509 | my $wdescr = $descr; | |
a736ac99 DM |
510 | chomp $wdescr; |
511 | $wdescr =~ s/^$/+/mg; | |
512 | ||
513 | $res .= $wdescr . "\n"; | |
8fa050cd DM |
514 | |
515 | if (my $req = $phash->{requires}) { | |
516 | my $tmp .= ref($req) ? join(', ', @$req) : $req; | |
517 | $res .= "+\nNOTE: Requires option(s): `$tmp`\n"; | |
518 | } | |
519 | $res .= "\n"; | |
520 | ||
b0e5e9fa | 521 | } elsif ($format eq 'short' || $format eq 'long' || $format eq 'full') { |
8fa050cd DM |
522 | |
523 | my $defaulttxt = ''; | |
524 | if (defined(my $dv = $phash->{default})) { | |
525 | $defaulttxt = " (default=$dv)"; | |
526 | } | |
527 | ||
df79b428 DM |
528 | my $display_name; |
529 | if ($style eq 'config') { | |
530 | $display_name = "$name:"; | |
531 | } elsif ($style eq 'arg') { | |
532 | $display_name = "-$name"; | |
533 | } elsif ($style eq 'fixed') { | |
534 | $display_name = "<$name>"; | |
535 | } else { | |
536 | die "unknown style '$style'"; | |
537 | } | |
8fa050cd | 538 | |
77096a6f | 539 | my $tmp = sprintf " %-10s %s$defaulttxt\n", $display_name, "$type_text"; |
8fa050cd DM |
540 | my $indend = " "; |
541 | ||
542 | $res .= Text::Wrap::wrap('', $indend, ($tmp)); | |
543 | $res .= "\n", | |
544 | $res .= Text::Wrap::wrap($indend, $indend, ($descr)) . "\n\n"; | |
545 | ||
546 | if (my $req = $phash->{requires}) { | |
547 | my $tmp = "Requires option(s): "; | |
548 | $tmp .= ref($req) ? join(', ', @$req) : $req; | |
549 | $res .= Text::Wrap::wrap($indend, $indend, ($tmp)). "\n\n"; | |
550 | } | |
551 | ||
552 | } else { | |
553 | die "unknown format '$format'"; | |
554 | } | |
555 | ||
556 | return $res; | |
557 | }; | |
558 | ||
dfc6cff8 DM |
559 | # translate parameter mapping definition |
560 | # $mapping_array is a array which can contain: | |
561 | # strings ... in that case we assume it is a parameter name, and | |
562 | # we want to load that parameter from a file | |
563 | # [ param_name, func, desc] ... allows you to specify a arbitrary | |
564 | # mapping func for any param | |
565 | # | |
566 | # Returns: a hash indexed by parameter_name, | |
567 | # i.e. { param_name => { func => .., desc => ... } } | |
568 | my $compute_param_mapping_hash = sub { | |
569 | my ($mapping_array) = @_; | |
570 | ||
571 | my $res = {}; | |
572 | ||
573 | return $res if !defined($mapping_array); | |
574 | ||
575 | foreach my $item (@$mapping_array) { | |
c7171ff2 | 576 | my ($name, $func, $desc, $interactive); |
dfc6cff8 | 577 | if (ref($item) eq 'ARRAY') { |
c7171ff2 | 578 | ($name, $func, $desc, $interactive) = @$item; |
a1c7edda DC |
579 | } elsif (ref($item) eq 'HASH') { |
580 | # just use the hash | |
581 | $res->{$item->{name}} = $item; | |
582 | next; | |
dfc6cff8 DM |
583 | } else { |
584 | $name = $item; | |
585 | $func = sub { return PVE::Tools::file_get_contents($_[0]) }; | |
586 | } | |
587 | $desc //= '<filepath>'; | |
c7171ff2 | 588 | $res->{$name} = { desc => $desc, func => $func, interactive => $interactive }; |
dfc6cff8 DM |
589 | } |
590 | ||
591 | return $res; | |
592 | }; | |
593 | ||
e143e9d8 DM |
594 | # generate usage information for command line tools |
595 | # | |
c03b7509 | 596 | # $info ... method info |
e143e9d8 | 597 | # $prefix ... usually something like "$exename $cmd" ('pvesm add') |
1d21344c DM |
598 | # $arg_param ... list of parameters we want to get as ordered arguments |
599 | # on the command line (or single parameter name for lists) | |
e143e9d8 DM |
600 | # $fixed_param ... do not generate and info about those parameters |
601 | # $format: | |
b0e5e9fa DM |
602 | # 'long' ... default (text, list all options) |
603 | # 'short' ... command line only (text, one line) | |
604 | # 'full' ... text, include description | |
fe3f1fde | 605 | # 'asciidoc' ... generate asciidoc for man pages (like 'full') |
9da27228 DM |
606 | # $param_cb ... mapping for string parameters to file path parameters |
607 | # $formatter_properties ... additional property definitions (passed to output formatter) | |
c03b7509 | 608 | sub getopt_usage { |
9da27228 | 609 | my ($info, $prefix, $arg_param, $fixed_param, $format, $param_cb, $formatter_properties) = @_; |
e143e9d8 DM |
610 | |
611 | $format = 'long' if !$format; | |
612 | ||
e143e9d8 | 613 | my $schema = $info->{parameters}; |
c03b7509 | 614 | my $name = $info->{name}; |
9da27228 | 615 | my $prop = { %{$schema->{properties}} }; # copy |
62c66c47 DM |
616 | |
617 | my $has_output_format_option = $formatter_properties->{'output-format'} ? 1 : 0; | |
618 | ||
619 | if ($formatter_properties) { | |
620 | foreach my $key (keys %$formatter_properties) { | |
621 | if (!$standard_output_options->{$key}) { | |
622 | $prop->{$key} = $formatter_properties->{$key}; | |
623 | } | |
624 | } | |
625 | } | |
e143e9d8 DM |
626 | |
627 | my $out = ''; | |
628 | ||
629 | my $arg_hash = {}; | |
630 | ||
631 | my $args = ''; | |
1d21344c DM |
632 | |
633 | $arg_param = [ $arg_param ] if $arg_param && !ref($arg_param); | |
634 | ||
e143e9d8 DM |
635 | foreach my $p (@$arg_param) { |
636 | next if !$prop->{$p}; # just to be sure | |
1d21344c | 637 | my $pd = $prop->{$p}; |
e143e9d8 DM |
638 | |
639 | $arg_hash->{$p} = 1; | |
640 | $args .= " " if $args; | |
1d21344c | 641 | if ($pd->{format} && $pd->{format} =~ m/-list/) { |
7f0f0610 | 642 | $args .= "{<$p>}"; |
1d21344c DM |
643 | } else { |
644 | $args .= $pd->{optional} ? "[<$p>]" : "<$p>"; | |
645 | } | |
e143e9d8 DM |
646 | } |
647 | ||
e143e9d8 DM |
648 | my $argdescr = ''; |
649 | foreach my $k (@$arg_param) { | |
650 | next if defined($fixed_param->{$k}); # just to be sure | |
651 | next if !$prop->{$k}; # just to be sure | |
4842b651 | 652 | $argdescr .= $get_property_description->($k, 'fixed', $prop->{$k}, $format); |
e143e9d8 DM |
653 | } |
654 | ||
655 | my $idx_param = {}; # -vlan\d+ -scsi\d+ | |
656 | ||
657 | my $opts = ''; | |
658 | foreach my $k (sort keys %$prop) { | |
659 | next if $arg_hash->{$k}; | |
660 | next if defined($fixed_param->{$k}); | |
661 | ||
77096a6f | 662 | my $type_text = $prop->{$k}->{type} || 'string'; |
e143e9d8 | 663 | |
968bcf45 | 664 | my $param_map = {}; |
4842b651 | 665 | |
968bcf45 TL |
666 | if (defined($param_cb)) { |
667 | my $mapping = $param_cb->($name); | |
668 | $param_map = $compute_param_mapping_hash->($mapping); | |
669 | next if $k eq 'password' && $param_map->{$k} && !$prop->{$k}->{optional}; | |
4842b651 | 670 | } |
e143e9d8 DM |
671 | |
672 | my $base = $k; | |
673 | if ($k =~ m/^([a-z]+)(\d+)$/) { | |
b546f33e | 674 | my ($name, $idx) = ($1, $2); |
e143e9d8 | 675 | next if $idx_param->{$name}; |
b54ad320 | 676 | if ($idx == 0 && defined($prop->{"${name}1"})) { |
f922b8a8 FG |
677 | $idx_param->{$name} = 1; |
678 | $base = "${name}[n]"; | |
679 | } | |
e143e9d8 DM |
680 | } |
681 | ||
dfc6cff8 | 682 | |
968bcf45 | 683 | $opts .= $get_property_description->($base, 'arg', $prop->{$k}, $format, $param_map->{$k}); |
e143e9d8 DM |
684 | |
685 | if (!$prop->{$k}->{optional}) { | |
686 | $args .= " " if $args; | |
a4f181a3 | 687 | $args .= "--$base <$type_text>" |
e143e9d8 DM |
688 | } |
689 | } | |
690 | ||
fe3f1fde DM |
691 | if ($format eq 'asciidoc') { |
692 | $out .= "*${prefix}*"; | |
693 | $out .= " `$args`" if $args; | |
62c66c47 DM |
694 | $out .= " `[OPTIONS]`" if $opts; |
695 | $out .= " `[FORMAT_OPTIONS]`" if $has_output_format_option; | |
696 | $out .= "\n"; | |
fe3f1fde DM |
697 | } else { |
698 | $out .= "USAGE: " if $format ne 'short'; | |
699 | $out .= "$prefix $args"; | |
62c66c47 DM |
700 | $out .= " [OPTIONS]" if $opts; |
701 | $out .= " [FORMAT_OPTIONS]" if $has_output_format_option; | |
702 | $out .= "\n"; | |
fe3f1fde | 703 | } |
e143e9d8 DM |
704 | |
705 | return $out if $format eq 'short'; | |
706 | ||
fe3f1fde DM |
707 | if ($info->{description}) { |
708 | if ($format eq 'asciidoc') { | |
709 | my $desc = Text::Wrap::wrap('', '', ($info->{description})); | |
710 | $out .= "\n$desc\n\n"; | |
711 | } elsif ($format eq 'full') { | |
712 | my $desc = Text::Wrap::wrap(' ', ' ', ($info->{description})); | |
713 | $out .= "\n$desc\n\n"; | |
714 | } | |
e143e9d8 DM |
715 | } |
716 | ||
717 | $out .= $argdescr if $argdescr; | |
718 | ||
719 | $out .= $opts if $opts; | |
720 | ||
721 | return $out; | |
722 | } | |
723 | ||
c03b7509 | 724 | sub usage_str { |
9da27228 | 725 | my ($self, $name, $prefix, $arg_param, $fixed_param, $format, $param_cb, $formatter_properties) = @_; |
c03b7509 DM |
726 | |
727 | my $info = $self->map_method_by_name($name); | |
728 | ||
9da27228 | 729 | return getopt_usage($info, $prefix, $arg_param, $fixed_param, $format, $param_cb, $formatter_properties); |
c03b7509 DM |
730 | } |
731 | ||
8fa050cd DM |
732 | # generate docs from JSON schema properties |
733 | sub dump_properties { | |
df79b428 | 734 | my ($prop, $format, $style, $filterFn) = @_; |
8fa050cd DM |
735 | |
736 | my $raw = ''; | |
737 | ||
df79b428 DM |
738 | $style //= 'config'; |
739 | ||
8fa050cd DM |
740 | my $idx_param = {}; # -vlan\d+ -scsi\d+ |
741 | ||
742 | foreach my $k (sort keys %$prop) { | |
743 | my $phash = $prop->{$k}; | |
744 | ||
745 | next if defined($filterFn) && &$filterFn($k, $phash); | |
32f8e0c7 | 746 | next if $phash->{alias}; |
8fa050cd DM |
747 | |
748 | my $base = $k; | |
749 | if ($k =~ m/^([a-z]+)(\d+)$/) { | |
b546f33e | 750 | my ($name, $idx) = ($1, $2); |
8fa050cd | 751 | next if $idx_param->{$name}; |
b54ad320 | 752 | if ($idx == 0 && defined($prop->{"${name}1"})) { |
f922b8a8 FG |
753 | $idx_param->{$name} = 1; |
754 | $base = "${name}[n]"; | |
755 | } | |
8fa050cd DM |
756 | } |
757 | ||
4842b651 | 758 | $raw .= $get_property_description->($base, $style, $phash, $format); |
32f8e0c7 DM |
759 | |
760 | next if $style ne 'config'; | |
761 | ||
762 | my $prop_fmt = $phash->{format}; | |
763 | next if !$prop_fmt; | |
764 | ||
765 | if (ref($prop_fmt) ne 'HASH') { | |
766 | $prop_fmt = PVE::JSONSchema::get_format($prop_fmt); | |
767 | } | |
768 | ||
769 | next if !(ref($prop_fmt) && (ref($prop_fmt) eq 'HASH')); | |
770 | ||
771 | $raw .= dump_properties($prop_fmt, $format, 'config-sub') | |
772 | ||
8fa050cd | 773 | } |
b0e5e9fa | 774 | |
8fa050cd DM |
775 | return $raw; |
776 | } | |
777 | ||
408976c6 | 778 | my $replace_file_names_with_contents = sub { |
968bcf45 | 779 | my ($param, $param_map) = @_; |
408976c6 | 780 | |
968bcf45 | 781 | while (my ($k, $d) = each %$param_map) { |
c7171ff2 | 782 | next if $d->{interactive}; # handled by the JSONSchema's get_options code |
dfc6cff8 DM |
783 | $param->{$k} = $d->{func}->($param->{$k}) |
784 | if defined($param->{$k}); | |
408976c6 FG |
785 | } |
786 | ||
787 | return $param; | |
788 | }; | |
789 | ||
4cbcd138 DM |
790 | sub add_standard_output_properties { |
791 | my ($propdef, $list) = @_; | |
0adee985 | 792 | |
4cbcd138 | 793 | $propdef //= {}; |
0adee985 | 794 | |
4cbcd138 DM |
795 | $list //= [ keys %$standard_output_options ]; |
796 | ||
797 | my $res = { %$propdef }; # copy | |
798 | ||
799 | foreach my $opt (@$list) { | |
800 | die "no such standard output option '$opt'\n" if !defined($standard_output_options->{$opt}); | |
801 | die "detected overwriten standard CLI parameter '$opt'\n" if defined($res->{$opt}); | |
802 | $res->{$opt} = $standard_output_options->{$opt}; | |
803 | } | |
804 | ||
805 | return $res; | |
806 | } | |
807 | ||
808 | sub extract_standard_output_properties { | |
809 | my ($data) = @_; | |
810 | ||
811 | my $options = {}; | |
812 | foreach my $opt (keys %$standard_output_options) { | |
813 | $options->{$opt} = delete $data->{$opt} if defined($data->{$opt}); | |
814 | } | |
815 | ||
816 | return $options; | |
817 | } | |
0adee985 | 818 | |
e143e9d8 | 819 | sub cli_handler { |
9da27228 | 820 | my ($self, $prefix, $name, $args, $arg_param, $fixed_param, $param_cb, $formatter_properties) = @_; |
e143e9d8 DM |
821 | |
822 | my $info = $self->map_method_by_name($name); | |
e143e9d8 | 823 | my $res; |
9da27228 DM |
824 | my $fmt_param = {}; |
825 | ||
e143e9d8 | 826 | eval { |
968bcf45 TL |
827 | my $param_map = {}; |
828 | $param_map = $compute_param_mapping_hash->($param_cb->($name)) if $param_cb; | |
9da27228 DM |
829 | my $schema = { %{$info->{parameters}} }; # copy |
830 | $schema->{properties} = { %{$schema->{properties}}, %$formatter_properties } if $formatter_properties; | |
831 | my $param = PVE::JSONSchema::get_options($schema, $args, $arg_param, $fixed_param, $param_map); | |
832 | ||
833 | if ($formatter_properties) { | |
834 | foreach my $opt (keys %$formatter_properties) { | |
835 | $fmt_param->{$opt} = delete $param->{$opt} if defined($param->{$opt}); | |
836 | } | |
837 | } | |
dfc6cff8 | 838 | |
968bcf45 TL |
839 | if (defined($param_map)) { |
840 | $replace_file_names_with_contents->($param, $param_map); | |
dfc6cff8 DM |
841 | } |
842 | ||
4cbcd138 | 843 | $res = $self->handle($info, $param); |
e143e9d8 DM |
844 | }; |
845 | if (my $err = $@) { | |
846 | my $ec = ref($err); | |
847 | ||
848 | die $err if !$ec || $ec ne "PVE::Exception" || !$err->is_param_exc(); | |
849 | ||
9da27228 | 850 | $err->{usage} = $self->usage_str($name, $prefix, $arg_param, $fixed_param, 'short', $param_cb, $formatter_properties); |
e143e9d8 DM |
851 | |
852 | die $err; | |
853 | } | |
854 | ||
9da27228 | 855 | return wantarray ? ($res, $fmt_param) : $res; |
e143e9d8 DM |
856 | } |
857 | ||
858 | # utility methods | |
859 | # note: this modifies the original hash by adding the id property | |
860 | sub hash_to_array { | |
861 | my ($hash, $idprop) = @_; | |
862 | ||
863 | my $res = []; | |
864 | return $res if !$hash; | |
865 | ||
866 | foreach my $k (keys %$hash) { | |
867 | $hash->{$k}->{$idprop} = $k; | |
868 | push @$res, $hash->{$k}; | |
869 | } | |
870 | ||
871 | return $res; | |
872 | } | |
873 | ||
874 | 1; |