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