1 package PVE
::APIClient
::Helpers
;
8 use File
::Path
qw(make_path);
10 use PVE
::APIClient
::JSONSchema
;
11 use PVE
::APIClient
::Exception
qw(raise);
14 use HTTP
::Status
qw(:constants);
16 my $pve_api_definition;
18 my $pve_api_definition_fn = "/usr/share/pve-client/pve-api-definition.dat";
27 my $default_output_format = 'text';
28 my $client_output_format = $default_output_format;
30 sub set_output_format
{
33 if (!defined($format)) {
34 $client_output_format = $default_output_format;
36 $client_output_format = $format;
40 sub get_output_format
{
41 return $client_output_format;
45 my ($data, $result_schema) = @_;
47 my $format = get_output_format
();
49 return if $result_schema->{type
} eq 'null';
51 # TODO: implement different output formats ($format)
53 if ($format eq 'json') {
54 print to_json
($data, {utf8
=> 1, allow_nonref
=> 1, canonical
=> 1, pretty
=> 1 });
55 } elsif ($format eq 'text') {
56 my $type = $result_schema->{type
};
57 if ($type eq 'object') {
59 } elsif ($type eq 'array') {
60 my $item_type = $result_schema->{items
}->{type
};
61 if ($item_type eq 'object') {
63 } elsif ($item_type eq 'array') {
66 foreach my $el (@$data) {
74 die "internal error: unknown output format"; # should not happen
78 my $__real_remove_formats; $__real_remove_formats = sub {
79 my ($properties) = @_;
81 foreach my $pname (keys %$properties) {
82 if (my $d = $properties->{$pname}) {
83 if (defined(my $format = $d->{format
})) {
85 $__real_remove_formats->($format);
86 } elsif (!PVE
::APIClient
::JSONSchema
::get_format
($format)) {
87 # simply remove unknown format definitions
95 my $remove_unknown_formats; $remove_unknown_formats = sub {
98 my $class = ref($tree);
101 if ($class eq 'ARRAY') {
102 foreach my $el (@$tree) {
103 $remove_unknown_formats->($el);
105 } elsif ($class eq 'HASH') {
106 if (my $info = $tree->{info
}) {
107 for my $method (qw(GET PUT PUSH DELETE)) {
108 next if !$info->{$method};
109 my $properties = $info->{$method}->{parameters
}->{properties
};
110 $__real_remove_formats->($properties) if $properties;
113 if ($tree->{children
}) {
114 $remove_unknown_formats->($tree->{children
});
120 sub get_api_definition
{
122 if (!defined($pve_api_definition)) {
123 open(my $fh, '<', $pve_api_definition_fn) ||
124 die "unable to open '$pve_api_definition_fn' - $!\n";
125 $pve_api_definition = Storable
::fd_retrieve
($fh);
127 $remove_unknown_formats->($pve_api_definition);
130 return $pve_api_definition;
133 my $map_path_to_info = sub {
134 my ($child_list, $stack, $uri_param) = @_;
136 while (defined(my $comp = shift @$stack)) {
137 foreach my $child (@$child_list) {
138 my $text = $child->{text
};
140 if ($text eq $comp) {
142 } elsif ($text =~ m/^\{(\S+)\}$/) {
144 $uri_param->{$1} = $comp;
146 next; # text next child
148 if ($child->{leaf
} || !scalar(@$stack)) {
151 $child_list = $child->{children
};
152 last; # test next path component
159 sub find_method_info
{
160 my ($path, $method, $uri_param, $noerr) = @_;
164 my $stack = [ grep { length($_) > 0 } split('\/+' , $path)]; # skip empty fragments
166 my $api = get_api_definition
();
168 my $child = scalar(@$stack) ?
$map_path_to_info->($api->{children
}, $stack, $uri_param) : $api;
170 if (!($child && $child->{info
} && $child->{info
}->{$method})) {
171 return undef if $noerr;
172 die "unable to find API method '$method' for path '$path'\n";
175 return $child->{info
}->{$method};
178 sub complete_api_call_options
{
179 my ($cmd, $prop, $prev, $cur, $args) = @_;
181 my $print_result = sub {
183 print "$p\n" if $p =~ m/^$cur/;
187 my $print_parameter_completion = sub {
189 my $d = $prop->{$pname};
190 if ($d->{completion
}) {
191 my $vt = ref($d->{completion
});
193 my $res = $d->{completion
}->($cmd, $pname, $cur, $args);
194 &$print_result(@$res);
196 } elsif ($d->{type
} eq 'boolean') {
197 &$print_result('0', '1');
198 } elsif ($d->{enum
}) {
199 &$print_result(@{$d->{enum
}});
203 my @option_list = ();
204 foreach my $key (keys %$prop) {
205 push @option_list, "--$key";
209 &$print_result(@option_list);
213 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
215 &$print_parameter_completion($pname);
219 &$print_result(@option_list);
222 sub merge_api_definition_properties
{
223 my ($path, $method, $properties) = @_;
225 my $info = PVE
::APIClient
::Helpers
::find_method_info
($path, $method);
227 return { %{$info->{parameters
}->{properties
}}, %$properties };
230 sub complete_api_path
{
233 my $api = get_api_definition
(); # make sure API data is loaded
237 my ($dir, $rest) = $text =~ m
|^(?
:(.*)/)?(?:([^/]*))?
$|;
240 if (!defined($dir)) {
244 my $stack = [ grep { length($_) > 0 } split('\/+' , $dir)]; # skip empty fragments
245 $info = $map_path_to_info->($api->{children
}, $stack, {});
250 my $prefix = length($dir) ?
"/$dir/" : '/';
251 if (my $children = $info->{children
}) {
252 foreach my $c (@$children) {
253 my $ctext = $c->{text
};
254 if ($ctext =~ m/^\{(\S+)\}$/) {
255 push @$res, "$prefix$ctext";
256 push @$res, "$prefix$ctext/";
258 push @$res, "$prefix$rest";
259 push @$res, "$prefix$rest/";
261 } elsif ($ctext =~ m/^\Q$rest/) {
262 push @$res, "$prefix$ctext";
263 push @$res, "$prefix$ctext/" if $c->{children
};
272 # test for command lines with api calls (or similar bash completion calls):
273 # example1: pveclient api get remote1 /cluster
274 sub extract_path_info
{
275 my ($uri_param) = @_;
279 my $test_path_properties = sub {
282 return if scalar(@$args) < 5;
283 return if $args->[1] ne 'api';
285 my $path = $args->[4];
286 if (my $method = $method_map->{$args->[2]}) {
287 $info = find_method_info
($path, $method, $uri_param, 1);
291 if (defined(my $cmd = $ARGV[0])) {
293 $test_path_properties->([$0, @ARGV]);
294 } elsif ($cmd eq 'bashcomplete') {
295 my $cmdline = substr($ENV{COMP_LINE
}, 0, $ENV{COMP_POINT
});
296 my $args = PVE
::APIClient
::Tools
::split_args
($cmdline);
297 $test_path_properties->($args);
304 sub get_vmid_resource
{
305 my ($conn, $vmid) = @_;
307 my $resources = $conn->get('api2/json/cluster/resources', {type
=> 'vm'});
310 for my $tmp (@$resources) {
311 if ($tmp->{vmid
} eq $vmid) {
317 if (!defined($resource)) {
318 die "\"$vmid\" not found
";
325 my ($conn, $node, $upid, $quiet) = @_;
327 my $path = "api2
/json/nodes
/$node/tasks/$upid/status";
333 my $path = "api2
/json/nodes
/$node/tasks/$upid/log";
334 my $task_log = $conn->get($path, {start => $last_line});
337 for my $li (@$task_log) {
338 if ($li->{t} eq 'no content') {
341 $printme .= $li->{t} . "\n";
342 $last_line = $li->{n};
345 if ($printme ne '') {
350 $task_status = $conn->get($path, {});
352 if ($task_status->{status} eq "stopped
") {
359 if ($task_status->{exitstatus} ne "OK
") {
360 die $task_status->{exitstatus};
363 return $task_status->{exitstatus};
366 sub configuration_directory {
368 my $home = $ENV{HOME} // '';
369 my $xdg = $ENV{XDG_CONFIG_HOME} // '';
371 my $subdir = "pveclient
";
373 return "$xdg/$subdir" if length($xdg);
375 return "$home/.config
/$subdir" if length($home);
377 die "neither XDG_CONFIG_HOME nor HOME environment variable set
\n";
380 my $ticket_cache_filename = "/.tickets
";
382 sub ticket_cache_lookup {
385 my $dir = configuration_directory();
386 my $filename = "$dir/$ticket_cache_filename";
389 eval { $data = from_json(PVE::APIClient::Tools::file_get_contents($filename)); };
392 my $ticket = $data->{$remote};
393 return undef if !defined($ticket);
396 my $max_age = 3600*2 - 60;
398 if ($ticket =~ m/:([a-fA-F0-9]{8})::/) {
401 my $age = $ctime - $ttime;
403 return $ticket if ($age > $min_age) && ($age < $max_age);
409 sub ticket_cache_update {
410 my ($remote, $ticket) = @_;
412 my $dir = configuration_directory();
413 my $filename = "$dir/$ticket_cache_filename";
419 my $raw = PVE::APIClient::Tools::file_get_contents($filename);
420 eval { $data = from_json($raw); };
423 $data->{$remote} = $ticket;
425 PVE::APIClient::Tools::file_set_contents($filename, to_json($data), 0600);
428 PVE::APIClient::Tools::lock_file($filename, undef, $code);