]> git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Helpers.pm
PVE::APIClient::Helper::print_ordered_result - correctly declare local var
[pve-client.git] / PVE / APIClient / Helpers.pm
1 package PVE::APIClient::Helpers;
2
3 use strict;
4 use warnings;
5
6 use Storable;
7 use JSON;
8 use File::Path qw(make_path);
9
10 use PVE::APIClient::JSONSchema;
11 use PVE::APIClient::Exception qw(raise);
12 use PVE::APIClient::CLIFormatter;
13 use PVE::APIClient::CLIHandler;
14 use PVE::APIClient::PTY;
15 use Encode::Locale;
16 use Encode;
17 use HTTP::Status qw(:constants);
18
19 my $pve_api_definition;
20
21 my $pve_api_definition_fn = "/usr/share/pve-client/pve-api-definition.dat";
22
23 my $method_map = {
24 create => 'POST',
25 set => 'PUT',
26 get => 'GET',
27 delete => 'DELETE',
28 };
29
30 my $default_output_format = 'text';
31 my $client_output_format = $default_output_format;
32
33 sub set_output_format {
34 my ($format) = @_;
35
36 if (!defined($format)) {
37 $client_output_format = $default_output_format;
38 } else {
39 $client_output_format = $format;
40 }
41 }
42
43 sub get_output_format {
44 return $client_output_format;
45 }
46
47 my $__real_remove_formats; $__real_remove_formats = sub {
48 my ($properties) = @_;
49
50 foreach my $pname (keys %$properties) {
51 if (my $d = $properties->{$pname}) {
52 if (defined(my $format = $d->{format})) {
53 if (ref($format)) {
54 $__real_remove_formats->($format);
55 } elsif (!PVE::APIClient::JSONSchema::get_format($format)) {
56 # simply remove unknown format definitions
57 delete $d->{format};
58 }
59 }
60 }
61 }
62 };
63
64 my $remove_unknown_formats; $remove_unknown_formats = sub {
65 my ($tree) = @_;
66
67 my $class = ref($tree);
68 return if !$class;
69
70 if ($class eq 'ARRAY') {
71 foreach my $el (@$tree) {
72 $remove_unknown_formats->($el);
73 }
74 } elsif ($class eq 'HASH') {
75 if (my $info = $tree->{info}) {
76 for my $method (qw(GET PUT PUSH DELETE)) {
77 next if !$info->{$method};
78 my $properties = $info->{$method}->{parameters}->{properties};
79 $__real_remove_formats->($properties) if $properties;
80 }
81 }
82 if ($tree->{children}) {
83 $remove_unknown_formats->($tree->{children});
84 }
85 }
86 return;
87 };
88
89 sub get_api_definition {
90
91 if (!defined($pve_api_definition)) {
92 open(my $fh, '<', $pve_api_definition_fn) ||
93 die "unable to open '$pve_api_definition_fn' - $!\n";
94 $pve_api_definition = Storable::fd_retrieve($fh);
95
96 $remove_unknown_formats->($pve_api_definition);
97 }
98
99 return $pve_api_definition;
100 }
101
102 my $map_path_to_info = sub {
103 my ($child_list, $stack, $uri_param) = @_;
104
105 while (defined(my $comp = shift @$stack)) {
106 foreach my $child (@$child_list) {
107 my $text = $child->{text};
108
109 if ($text eq $comp) {
110 # found
111 } elsif ($text =~ m/^\{(\S+)\}$/) {
112 # found
113 $uri_param->{$1} = $comp;
114 } else {
115 next; # text next child
116 }
117 if ($child->{leaf} || !scalar(@$stack)) {
118 return $child;
119 } else {
120 $child_list = $child->{children};
121 last; # test next path component
122 }
123 }
124 }
125 return undef;
126 };
127
128 sub find_method_info {
129 my ($path, $method, $uri_param, $noerr) = @_;
130
131 $uri_param //= {};
132
133 my $stack = [ grep { length($_) > 0 } split('\/+' , $path)]; # skip empty fragments
134
135 my $api = get_api_definition();
136
137 my $child = scalar(@$stack) ? $map_path_to_info->($api->{children}, $stack, $uri_param) : $api;
138
139 if (!($child && $child->{info} && $child->{info}->{$method})) {
140 return undef if $noerr;
141 die "unable to find API method '$method' for path '$path'\n";
142 }
143
144 return $child->{info}->{$method};
145 }
146
147 sub complete_api_call_options {
148 my ($cmd, $prop, $prev, $cur, $args) = @_;
149
150 my $print_result = sub {
151 foreach my $p (@_) {
152 print "$p\n" if $p =~ m/^$cur/;
153 }
154 };
155
156 my $print_parameter_completion = sub {
157 my ($pname) = @_;
158 my $d = $prop->{$pname};
159 if ($d->{completion}) {
160 my $vt = ref($d->{completion});
161 if ($vt eq 'CODE') {
162 my $res = $d->{completion}->($cmd, $pname, $cur, $args);
163 &$print_result(@$res);
164 }
165 } elsif ($d->{type} eq 'boolean') {
166 &$print_result('0', '1');
167 } elsif ($d->{enum}) {
168 &$print_result(@{$d->{enum}});
169 }
170 };
171
172 my @option_list = ();
173 foreach my $key (keys %$prop) {
174 push @option_list, "--$key";
175 }
176
177 if ($cur =~ m/^-/) {
178 &$print_result(@option_list);
179 return;
180 }
181
182 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
183 my $pname = $1;
184 &$print_parameter_completion($pname);
185 return;
186 }
187
188 &$print_result(@option_list);
189 }
190
191 sub merge_api_definition_properties {
192 my ($path, $method, $properties) = @_;
193
194 my $info = PVE::APIClient::Helpers::find_method_info($path, $method);
195
196 return { %{$info->{parameters}->{properties}}, %$properties };
197 }
198
199 sub complete_api_path {
200 my ($text) = @_;
201
202 my $api = get_api_definition(); # make sure API data is loaded
203
204 $text =~ s!^/!!;
205
206 my ($dir, $rest) = $text =~ m|^(?:(.*)/)?(?:([^/]*))?$|;
207
208 my $info;
209 if (!defined($dir)) {
210 $dir = '';
211 $info = $api;
212 } else {
213 my $stack = [ grep { length($_) > 0 } split('\/+' , $dir)]; # skip empty fragments
214 $info = $map_path_to_info->($api->{children}, $stack, {});
215 }
216
217 my $res = [];
218 if ($info) {
219 my $prefix = length($dir) ? "/$dir/" : '/';
220 if (my $children = $info->{children}) {
221 foreach my $c (@$children) {
222 my $ctext = $c->{text};
223 if ($ctext =~ m/^\{(\S+)\}$/) {
224 push @$res, "$prefix$ctext";
225 push @$res, "$prefix$ctext/";
226 if (length($rest)) {
227 push @$res, "$prefix$rest";
228 push @$res, "$prefix$rest/";
229 }
230 } elsif ($ctext =~ m/^\Q$rest/) {
231 push @$res, "$prefix$ctext";
232 push @$res, "$prefix$ctext/" if $c->{children};
233 }
234 }
235 }
236 }
237
238 return $res;
239 }
240
241 # test for command lines with api calls (or similar bash completion calls):
242 # example1: pveclient api get remote1 /cluster
243 sub extract_path_info {
244 my ($uri_param) = @_;
245
246 my $info;
247
248 my $test_path_properties = sub {
249 my ($args) = @_;
250
251 return if scalar(@$args) < 5;
252 return if $args->[1] ne 'api';
253
254 my $path = $args->[4];
255 if (my $method = $method_map->{$args->[2]}) {
256 $info = find_method_info($path, $method, $uri_param, 1);
257 }
258 };
259
260 if (defined(my $cmd = $ARGV[0])) {
261 if ($cmd eq 'api') {
262 $test_path_properties->([$0, @ARGV]);
263 } elsif ($cmd eq 'bashcomplete') {
264 my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT});
265 my $args = PVE::APIClient::Tools::split_args($cmdline);
266 $test_path_properties->($args);
267 }
268 }
269
270 return $info;
271 }
272
273 sub get_vmid_resource {
274 my ($conn, $vmid) = @_;
275
276 my $resources = $conn->get('api2/json/cluster/resources', {type => 'vm'});
277
278 my $resource;
279 for my $tmp (@$resources) {
280 if ($tmp->{vmid} eq $vmid) {
281 $resource = $tmp;
282 last;
283 }
284 }
285
286 if (!defined($resource)) {
287 die "\"$vmid\" not found";
288 }
289
290 return $resource;
291 }
292
293 sub poll_task {
294 my ($conn, $node, $upid, $quiet) = @_;
295
296 my $path = "api2/json/nodes/$node/tasks/$upid/status";
297
298 my $task_status;
299 my $last_line = 0;
300 while(1) {
301 if (!$quiet) {
302 my $path = "api2/json/nodes/$node/tasks/$upid/log";
303 my $task_log = $conn->get($path, {start => $last_line});
304
305 my $printme = '';
306 for my $li (@$task_log) {
307 if ($li->{t} eq 'no content') {
308 next;
309 }
310 $printme .= $li->{t} . "\n";
311 $last_line = $li->{n};
312 }
313
314 if ($printme ne '') {
315 print $printme;
316 }
317 }
318
319 $task_status = $conn->get($path, {});
320
321 if ($task_status->{status} eq "stopped") {
322 last;
323 }
324
325 sleep(2);
326 }
327
328 if ($task_status->{exitstatus} ne "OK") {
329 die $task_status->{exitstatus};
330 }
331
332 return $task_status->{exitstatus};
333 }
334
335 sub configuration_directory {
336
337 my $home = $ENV{HOME} // '';
338 my $xdg = $ENV{XDG_CONFIG_HOME} // '';
339
340 my $subdir = "pveclient";
341
342 return "$xdg/$subdir" if length($xdg);
343
344 return "$home/.config/$subdir" if length($home);
345
346 die "neither XDG_CONFIG_HOME nor HOME environment variable set\n";
347 }
348
349 my $ticket_cache_filename = "/.tickets";
350
351 sub ticket_cache_lookup {
352 my ($remote) = @_;
353
354 my $dir = configuration_directory();
355 my $filename = "$dir/$ticket_cache_filename";
356
357 my $data = {};
358 eval { $data = from_json(PVE::APIClient::Tools::file_get_contents($filename)); };
359 # ignore errors
360
361 my $ticket = $data->{$remote};
362 return undef if !defined($ticket);
363
364 my $min_age = - 60;
365 my $max_age = 3600*2 - 60;
366
367 if ($ticket =~ m/:([a-fA-F0-9]{8})::/) {
368 my $ttime = hex($1);
369 my $ctime = time();
370 my $age = $ctime - $ttime;
371
372 return $ticket if ($age > $min_age) && ($age < $max_age);
373 }
374
375 return undef;
376 }
377
378 sub ticket_cache_update {
379 my ($remote, $ticket) = @_;
380
381 my $dir = configuration_directory();
382 my $filename = "$dir/$ticket_cache_filename";
383
384 my $code = sub {
385 make_path($dir);
386 my $data = {};
387 if (-f $filename) {
388 my $raw = PVE::APIClient::Tools::file_get_contents($filename);
389 eval { $data = from_json($raw); };
390 # ignore errors
391 }
392 $data->{$remote} = $ticket;
393
394 PVE::APIClient::Tools::file_set_contents($filename, to_json($data), 0600);
395 };
396
397 PVE::APIClient::Tools::lock_file($filename, undef, $code);
398 die $@ if $@;
399 }
400
401 sub extract_even_elements {
402 my ($list) = @_;
403
404 my $ind = 0;
405 return [ grep { ($ind++ % 2) == 0 } @$list ];
406 }
407
408 sub print_ordered_result {
409 my ($property_list, $data, $result_schema) = @_;
410
411 my $format = get_output_format();
412 my $param_order = extract_even_elements($property_list);
413
414 my $options = {};
415 PVE::APIClient::CLIFormatter::query_terminal_options($options);
416
417 PVE::APIClient::CLIFormatter::print_api_result($format, $data, $result_schema, $param_order, $options);
418 }
419
420 1;