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