]>
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); | |
e143e9d8 | 258 | if ($comp =~ m/^\{(\w+)(:(.*))?\}$/) { |
c1089794 DM |
259 | $name = $1; |
260 | $regex = $3 ? $3 : '\S+'; | |
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 { | |
4cbcd138 | 429 | my ($self, $info, $param) = @_; |
e143e9d8 DM |
430 | |
431 | my $func = $info->{code}; | |
432 | ||
433 | if (!($info->{name} && $func)) { | |
434 | raise("Method lookup failed ('$info->{name}')\n", | |
435 | code => HTTP_INTERNAL_SERVER_ERROR); | |
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 | ||
4cbcd138 | 453 | my $result = &$func($param); |
e143e9d8 DM |
454 | |
455 | # todo: this is only to be safe - disable? | |
456 | if (my $schema = $info->{returns}) { | |
9030a7d7 | 457 | PVE::JSONSchema::validate($result, $schema, "Result verification failed\n"); |
e143e9d8 DM |
458 | } |
459 | ||
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 | ||
522 | $res .= $wdescr . "\n"; | |
8fa050cd DM |
523 | |
524 | if (my $req = $phash->{requires}) { | |
525 | my $tmp .= ref($req) ? join(', ', @$req) : $req; | |
526 | $res .= "+\nNOTE: Requires option(s): `$tmp`\n"; | |
527 | } | |
528 | $res .= "\n"; | |
529 | ||
b0e5e9fa | 530 | } elsif ($format eq 'short' || $format eq 'long' || $format eq 'full') { |
8fa050cd DM |
531 | |
532 | my $defaulttxt = ''; | |
533 | if (defined(my $dv = $phash->{default})) { | |
534 | $defaulttxt = " (default=$dv)"; | |
535 | } | |
536 | ||
df79b428 DM |
537 | my $display_name; |
538 | if ($style eq 'config') { | |
539 | $display_name = "$name:"; | |
540 | } elsif ($style eq 'arg') { | |
541 | $display_name = "-$name"; | |
542 | } elsif ($style eq 'fixed') { | |
543 | $display_name = "<$name>"; | |
544 | } else { | |
545 | die "unknown style '$style'"; | |
546 | } | |
8fa050cd | 547 | |
cc6792c7 | 548 | my $tmp = sprintf " %-10s %s%s\n", $display_name, "$type_text", "$defaulttxt"; |
8fa050cd DM |
549 | my $indend = " "; |
550 | ||
551 | $res .= Text::Wrap::wrap('', $indend, ($tmp)); | |
552 | $res .= "\n", | |
553 | $res .= Text::Wrap::wrap($indend, $indend, ($descr)) . "\n\n"; | |
554 | ||
555 | if (my $req = $phash->{requires}) { | |
556 | my $tmp = "Requires option(s): "; | |
557 | $tmp .= ref($req) ? join(', ', @$req) : $req; | |
558 | $res .= Text::Wrap::wrap($indend, $indend, ($tmp)). "\n\n"; | |
559 | } | |
560 | ||
561 | } else { | |
562 | die "unknown format '$format'"; | |
563 | } | |
564 | ||
565 | return $res; | |
566 | }; | |
567 | ||
dfc6cff8 DM |
568 | # translate parameter mapping definition |
569 | # $mapping_array is a array which can contain: | |
570 | # strings ... in that case we assume it is a parameter name, and | |
571 | # we want to load that parameter from a file | |
572 | # [ param_name, func, desc] ... allows you to specify a arbitrary | |
573 | # mapping func for any param | |
574 | # | |
575 | # Returns: a hash indexed by parameter_name, | |
576 | # i.e. { param_name => { func => .., desc => ... } } | |
577 | my $compute_param_mapping_hash = sub { | |
578 | my ($mapping_array) = @_; | |
579 | ||
580 | my $res = {}; | |
581 | ||
582 | return $res if !defined($mapping_array); | |
583 | ||
584 | foreach my $item (@$mapping_array) { | |
c7171ff2 | 585 | my ($name, $func, $desc, $interactive); |
dfc6cff8 | 586 | if (ref($item) eq 'ARRAY') { |
c7171ff2 | 587 | ($name, $func, $desc, $interactive) = @$item; |
a1c7edda DC |
588 | } elsif (ref($item) eq 'HASH') { |
589 | # just use the hash | |
590 | $res->{$item->{name}} = $item; | |
591 | next; | |
dfc6cff8 DM |
592 | } else { |
593 | $name = $item; | |
594 | $func = sub { return PVE::Tools::file_get_contents($_[0]) }; | |
595 | } | |
596 | $desc //= '<filepath>'; | |
c7171ff2 | 597 | $res->{$name} = { desc => $desc, func => $func, interactive => $interactive }; |
dfc6cff8 DM |
598 | } |
599 | ||
600 | return $res; | |
601 | }; | |
602 | ||
e143e9d8 DM |
603 | # generate usage information for command line tools |
604 | # | |
c03b7509 | 605 | # $info ... method info |
e143e9d8 | 606 | # $prefix ... usually something like "$exename $cmd" ('pvesm add') |
9bbc4e17 | 607 | # $arg_param ... list of parameters we want to get as ordered arguments |
1d21344c | 608 | # on the command line (or single parameter name for lists) |
e143e9d8 DM |
609 | # $fixed_param ... do not generate and info about those parameters |
610 | # $format: | |
b0e5e9fa DM |
611 | # 'long' ... default (text, list all options) |
612 | # 'short' ... command line only (text, one line) | |
613 | # 'full' ... text, include description | |
fe3f1fde | 614 | # 'asciidoc' ... generate asciidoc for man pages (like 'full') |
9da27228 DM |
615 | # $param_cb ... mapping for string parameters to file path parameters |
616 | # $formatter_properties ... additional property definitions (passed to output formatter) | |
c03b7509 | 617 | sub getopt_usage { |
9da27228 | 618 | my ($info, $prefix, $arg_param, $fixed_param, $format, $param_cb, $formatter_properties) = @_; |
e143e9d8 DM |
619 | |
620 | $format = 'long' if !$format; | |
621 | ||
e143e9d8 | 622 | my $schema = $info->{parameters}; |
c03b7509 | 623 | my $name = $info->{name}; |
772038d4 TL |
624 | my $prop = {}; |
625 | if ($schema->{properties}) { | |
626 | $prop = { %{$schema->{properties}} }; # copy | |
627 | } | |
62c66c47 DM |
628 | |
629 | my $has_output_format_option = $formatter_properties->{'output-format'} ? 1 : 0; | |
630 | ||
631 | if ($formatter_properties) { | |
632 | foreach my $key (keys %$formatter_properties) { | |
633 | if (!$standard_output_options->{$key}) { | |
634 | $prop->{$key} = $formatter_properties->{$key}; | |
635 | } | |
5c59d8f9 DM |
636 | } |
637 | } | |
638 | ||
639 | # also remove $standard_output_options from $prop (pvesh, pveclient) | |
640 | if ($prop->{'output-format'}) { | |
641 | $has_output_format_option = 1; | |
642 | foreach my $key (keys %$prop) { | |
643 | if ($standard_output_options->{$key}) { | |
644 | delete $prop->{$key}; | |
645 | } | |
62c66c47 DM |
646 | } |
647 | } | |
e143e9d8 DM |
648 | |
649 | my $out = ''; | |
650 | ||
651 | my $arg_hash = {}; | |
652 | ||
653 | my $args = ''; | |
1d21344c DM |
654 | |
655 | $arg_param = [ $arg_param ] if $arg_param && !ref($arg_param); | |
656 | ||
e143e9d8 DM |
657 | foreach my $p (@$arg_param) { |
658 | next if !$prop->{$p}; # just to be sure | |
1d21344c | 659 | my $pd = $prop->{$p}; |
e143e9d8 DM |
660 | |
661 | $arg_hash->{$p} = 1; | |
662 | $args .= " " if $args; | |
1d21344c | 663 | if ($pd->{format} && $pd->{format} =~ m/-list/) { |
7f0f0610 | 664 | $args .= "{<$p>}"; |
1d21344c DM |
665 | } else { |
666 | $args .= $pd->{optional} ? "[<$p>]" : "<$p>"; | |
667 | } | |
e143e9d8 DM |
668 | } |
669 | ||
e143e9d8 DM |
670 | my $argdescr = ''; |
671 | foreach my $k (@$arg_param) { | |
672 | next if defined($fixed_param->{$k}); # just to be sure | |
673 | next if !$prop->{$k}; # just to be sure | |
4842b651 | 674 | $argdescr .= $get_property_description->($k, 'fixed', $prop->{$k}, $format); |
e143e9d8 DM |
675 | } |
676 | ||
677 | my $idx_param = {}; # -vlan\d+ -scsi\d+ | |
678 | ||
679 | my $opts = ''; | |
680 | foreach my $k (sort keys %$prop) { | |
681 | next if $arg_hash->{$k}; | |
682 | next if defined($fixed_param->{$k}); | |
683 | ||
77096a6f | 684 | my $type_text = $prop->{$k}->{type} || 'string'; |
e143e9d8 | 685 | |
968bcf45 | 686 | my $param_map = {}; |
4842b651 | 687 | |
968bcf45 TL |
688 | if (defined($param_cb)) { |
689 | my $mapping = $param_cb->($name); | |
690 | $param_map = $compute_param_mapping_hash->($mapping); | |
691 | next if $k eq 'password' && $param_map->{$k} && !$prop->{$k}->{optional}; | |
4842b651 | 692 | } |
e143e9d8 DM |
693 | |
694 | my $base = $k; | |
695 | if ($k =~ m/^([a-z]+)(\d+)$/) { | |
b546f33e | 696 | my ($name, $idx) = ($1, $2); |
e143e9d8 | 697 | next if $idx_param->{$name}; |
b54ad320 | 698 | if ($idx == 0 && defined($prop->{"${name}1"})) { |
f922b8a8 FG |
699 | $idx_param->{$name} = 1; |
700 | $base = "${name}[n]"; | |
701 | } | |
e143e9d8 DM |
702 | } |
703 | ||
dfc6cff8 | 704 | |
968bcf45 | 705 | $opts .= $get_property_description->($base, 'arg', $prop->{$k}, $format, $param_map->{$k}); |
e143e9d8 DM |
706 | |
707 | if (!$prop->{$k}->{optional}) { | |
708 | $args .= " " if $args; | |
a4f181a3 | 709 | $args .= "--$base <$type_text>" |
e143e9d8 | 710 | } |
9bbc4e17 | 711 | } |
e143e9d8 | 712 | |
fe3f1fde DM |
713 | if ($format eq 'asciidoc') { |
714 | $out .= "*${prefix}*"; | |
715 | $out .= " `$args`" if $args; | |
62c66c47 DM |
716 | $out .= " `[OPTIONS]`" if $opts; |
717 | $out .= " `[FORMAT_OPTIONS]`" if $has_output_format_option; | |
718 | $out .= "\n"; | |
fe3f1fde DM |
719 | } else { |
720 | $out .= "USAGE: " if $format ne 'short'; | |
721 | $out .= "$prefix $args"; | |
62c66c47 DM |
722 | $out .= " [OPTIONS]" if $opts; |
723 | $out .= " [FORMAT_OPTIONS]" if $has_output_format_option; | |
724 | $out .= "\n"; | |
fe3f1fde | 725 | } |
e143e9d8 DM |
726 | |
727 | return $out if $format eq 'short'; | |
728 | ||
fe3f1fde DM |
729 | if ($info->{description}) { |
730 | if ($format eq 'asciidoc') { | |
731 | my $desc = Text::Wrap::wrap('', '', ($info->{description})); | |
732 | $out .= "\n$desc\n\n"; | |
733 | } elsif ($format eq 'full') { | |
734 | my $desc = Text::Wrap::wrap(' ', ' ', ($info->{description})); | |
735 | $out .= "\n$desc\n\n"; | |
736 | } | |
e143e9d8 DM |
737 | } |
738 | ||
739 | $out .= $argdescr if $argdescr; | |
740 | ||
741 | $out .= $opts if $opts; | |
742 | ||
743 | return $out; | |
744 | } | |
745 | ||
c03b7509 | 746 | sub usage_str { |
9da27228 | 747 | my ($self, $name, $prefix, $arg_param, $fixed_param, $format, $param_cb, $formatter_properties) = @_; |
c03b7509 DM |
748 | |
749 | my $info = $self->map_method_by_name($name); | |
750 | ||
9da27228 | 751 | return getopt_usage($info, $prefix, $arg_param, $fixed_param, $format, $param_cb, $formatter_properties); |
c03b7509 DM |
752 | } |
753 | ||
8fa050cd DM |
754 | # generate docs from JSON schema properties |
755 | sub dump_properties { | |
df79b428 | 756 | my ($prop, $format, $style, $filterFn) = @_; |
8fa050cd DM |
757 | |
758 | my $raw = ''; | |
759 | ||
df79b428 | 760 | $style //= 'config'; |
9bbc4e17 | 761 | |
8fa050cd DM |
762 | my $idx_param = {}; # -vlan\d+ -scsi\d+ |
763 | ||
764 | foreach my $k (sort keys %$prop) { | |
765 | my $phash = $prop->{$k}; | |
766 | ||
767 | next if defined($filterFn) && &$filterFn($k, $phash); | |
32f8e0c7 | 768 | next if $phash->{alias}; |
8fa050cd DM |
769 | |
770 | my $base = $k; | |
771 | if ($k =~ m/^([a-z]+)(\d+)$/) { | |
b546f33e | 772 | my ($name, $idx) = ($1, $2); |
8fa050cd | 773 | next if $idx_param->{$name}; |
b54ad320 | 774 | if ($idx == 0 && defined($prop->{"${name}1"})) { |
f922b8a8 FG |
775 | $idx_param->{$name} = 1; |
776 | $base = "${name}[n]"; | |
777 | } | |
8fa050cd DM |
778 | } |
779 | ||
4842b651 | 780 | $raw .= $get_property_description->($base, $style, $phash, $format); |
32f8e0c7 DM |
781 | |
782 | next if $style ne 'config'; | |
783 | ||
784 | my $prop_fmt = $phash->{format}; | |
785 | next if !$prop_fmt; | |
786 | ||
787 | if (ref($prop_fmt) ne 'HASH') { | |
788 | $prop_fmt = PVE::JSONSchema::get_format($prop_fmt); | |
789 | } | |
790 | ||
791 | next if !(ref($prop_fmt) && (ref($prop_fmt) eq 'HASH')); | |
792 | ||
793 | $raw .= dump_properties($prop_fmt, $format, 'config-sub') | |
9bbc4e17 | 794 | |
8fa050cd | 795 | } |
b0e5e9fa | 796 | |
8fa050cd DM |
797 | return $raw; |
798 | } | |
799 | ||
408976c6 | 800 | my $replace_file_names_with_contents = sub { |
968bcf45 | 801 | my ($param, $param_map) = @_; |
408976c6 | 802 | |
968bcf45 | 803 | while (my ($k, $d) = each %$param_map) { |
c7171ff2 | 804 | next if $d->{interactive}; # handled by the JSONSchema's get_options code |
dfc6cff8 DM |
805 | $param->{$k} = $d->{func}->($param->{$k}) |
806 | if defined($param->{$k}); | |
408976c6 FG |
807 | } |
808 | ||
809 | return $param; | |
810 | }; | |
811 | ||
4cbcd138 DM |
812 | sub add_standard_output_properties { |
813 | my ($propdef, $list) = @_; | |
0adee985 | 814 | |
4cbcd138 | 815 | $propdef //= {}; |
0adee985 | 816 | |
4cbcd138 DM |
817 | $list //= [ keys %$standard_output_options ]; |
818 | ||
819 | my $res = { %$propdef }; # copy | |
820 | ||
821 | foreach my $opt (@$list) { | |
822 | die "no such standard output option '$opt'\n" if !defined($standard_output_options->{$opt}); | |
823 | die "detected overwriten standard CLI parameter '$opt'\n" if defined($res->{$opt}); | |
824 | $res->{$opt} = $standard_output_options->{$opt}; | |
825 | } | |
826 | ||
827 | return $res; | |
828 | } | |
829 | ||
830 | sub extract_standard_output_properties { | |
831 | my ($data) = @_; | |
832 | ||
833 | my $options = {}; | |
834 | foreach my $opt (keys %$standard_output_options) { | |
835 | $options->{$opt} = delete $data->{$opt} if defined($data->{$opt}); | |
836 | } | |
837 | ||
838 | return $options; | |
839 | } | |
0adee985 | 840 | |
e143e9d8 | 841 | sub cli_handler { |
9da27228 | 842 | my ($self, $prefix, $name, $args, $arg_param, $fixed_param, $param_cb, $formatter_properties) = @_; |
e143e9d8 DM |
843 | |
844 | my $info = $self->map_method_by_name($name); | |
e143e9d8 | 845 | my $res; |
9da27228 DM |
846 | my $fmt_param = {}; |
847 | ||
e143e9d8 | 848 | eval { |
968bcf45 TL |
849 | my $param_map = {}; |
850 | $param_map = $compute_param_mapping_hash->($param_cb->($name)) if $param_cb; | |
9da27228 DM |
851 | my $schema = { %{$info->{parameters}} }; # copy |
852 | $schema->{properties} = { %{$schema->{properties}}, %$formatter_properties } if $formatter_properties; | |
853 | my $param = PVE::JSONSchema::get_options($schema, $args, $arg_param, $fixed_param, $param_map); | |
854 | ||
855 | if ($formatter_properties) { | |
856 | foreach my $opt (keys %$formatter_properties) { | |
857 | $fmt_param->{$opt} = delete $param->{$opt} if defined($param->{$opt}); | |
858 | } | |
859 | } | |
dfc6cff8 | 860 | |
968bcf45 TL |
861 | if (defined($param_map)) { |
862 | $replace_file_names_with_contents->($param, $param_map); | |
dfc6cff8 DM |
863 | } |
864 | ||
4cbcd138 | 865 | $res = $self->handle($info, $param); |
e143e9d8 DM |
866 | }; |
867 | if (my $err = $@) { | |
868 | my $ec = ref($err); | |
869 | ||
870 | die $err if !$ec || $ec ne "PVE::Exception" || !$err->is_param_exc(); | |
9bbc4e17 | 871 | |
9da27228 | 872 | $err->{usage} = $self->usage_str($name, $prefix, $arg_param, $fixed_param, 'short', $param_cb, $formatter_properties); |
e143e9d8 DM |
873 | |
874 | die $err; | |
875 | } | |
876 | ||
9da27228 | 877 | return wantarray ? ($res, $fmt_param) : $res; |
e143e9d8 DM |
878 | } |
879 | ||
880 | # utility methods | |
881 | # note: this modifies the original hash by adding the id property | |
882 | sub hash_to_array { | |
883 | my ($hash, $idprop) = @_; | |
884 | ||
885 | my $res = []; | |
886 | return $res if !$hash; | |
887 | ||
888 | foreach my $k (keys %$hash) { | |
889 | $hash->{$k}->{$idprop} = $k; | |
890 | push @$res, $hash->{$k}; | |
891 | } | |
892 | ||
893 | return $res; | |
894 | } | |
895 | ||
896 | 1; |