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