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