]> git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Helpers.pm
ed1822d4a514dea06b3807c6c9feab7097d7fd50
[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 Encode::Locale;
13 use Encode;
14 use HTTP::Status qw(:constants);
15
16 my $pve_api_definition;
17
18 my $pve_api_definition_fn = "/usr/share/pve-client/pve-api-definition.dat";
19
20 my $method_map = {
21 create => 'POST',
22 set => 'PUT',
23 get => 'GET',
24 delete => 'DELETE',
25 };
26
27 my $default_output_format = 'text';
28 my $client_output_format = $default_output_format;
29
30 sub 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
40 sub get_output_format {
41 return $client_output_format;
42 }
43
44 my $__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
61 my $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
86 sub get_api_definition {
87
88 if (!defined($pve_api_definition)) {
89 open(my $fh, '<', $pve_api_definition_fn) ||
90 die "unable to open '$pve_api_definition_fn' - $!\n";
91 $pve_api_definition = Storable::fd_retrieve($fh);
92
93 $remove_unknown_formats->($pve_api_definition);
94 }
95
96 return $pve_api_definition;
97 }
98
99 my $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 };
124
125 sub find_method_info {
126 my ($path, $method, $uri_param, $noerr) = @_;
127
128 $uri_param //= {};
129
130 my $stack = [ grep { length($_) > 0 } split('\/+' , $path)]; # skip empty fragments
131
132 my $api = get_api_definition();
133
134 my $child = scalar(@$stack) ? $map_path_to_info->($api->{children}, $stack, $uri_param) : $api;
135
136 if (!($child && $child->{info} && $child->{info}->{$method})) {
137 return undef if $noerr;
138 die "unable to find API method '$method' for path '$path'\n";
139 }
140
141 return $child->{info}->{$method};
142 }
143
144 sub 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
188 sub 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
196 sub complete_api_path {
197 my ($text) = @_;
198
199 my $api = get_api_definition(); # make sure API data is loaded
200
201 $text =~ s!^/!!;
202
203 my ($dir, $rest) = $text =~ m|^(?:(.*)/)?(?:([^/]*))?$|;
204
205 my $info;
206 if (!defined($dir)) {
207 $dir = '';
208 $info = $api;
209 } else {
210 my $stack = [ grep { length($_) > 0 } split('\/+' , $dir)]; # skip empty fragments
211 $info = $map_path_to_info->($api->{children}, $stack, {});
212 }
213
214 my $res = [];
215 if ($info) {
216 my $prefix = length($dir) ? "/$dir/" : '/';
217 if (my $children = $info->{children}) {
218 foreach my $c (@$children) {
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};
230 }
231 }
232 }
233 }
234
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
240 sub extract_path_info {
241 my ($uri_param) = @_;
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]}) {
253 $info = find_method_info($path, $method, $uri_param, 1);
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});
262 my $args = PVE::APIClient::Tools::split_args($cmdline);
263 $test_path_properties->($args);
264 }
265 }
266
267 return $info;
268 }
269
270 sub 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
290 sub poll_task {
291 my ($conn, $node, $upid, $quiet) = @_;
292
293 my $path = "api2/json/nodes/$node/tasks/$upid/status";
294
295 my $task_status;
296 my $last_line = 0;
297 while(1) {
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
316 $task_status = $conn->get($path, {});
317
318 if ($task_status->{status} eq "stopped") {
319 last;
320 }
321
322 sleep(2);
323 }
324
325 if ($task_status->{exitstatus} ne "OK") {
326 die $task_status->{exitstatus};
327 }
328
329 return $task_status->{exitstatus};
330 }
331
332 sub 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
346 my $ticket_cache_filename = "/.tickets";
347
348 sub 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
375 sub 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
399 1;