1 package PVE
::APIClient
::Helpers
;
8 use File
::Path
qw(make_path);
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
;
17 use HTTP
::Status
qw(:constants);
19 my $pve_api_definition;
21 my $pve_api_definition_fn = "/usr/share/pve-client/pve-api-definition.dat";
30 my $__real_remove_formats; $__real_remove_formats = sub {
31 my ($properties) = @_;
33 foreach my $pname (keys %$properties) {
34 if (my $d = $properties->{$pname}) {
35 if (defined(my $format = $d->{format
})) {
37 $__real_remove_formats->($format);
38 } elsif (!PVE
::APIClient
::JSONSchema
::get_format
($format)) {
39 # simply remove unknown format definitions
47 my $remove_unknown_formats; $remove_unknown_formats = sub {
50 my $class = ref($tree);
53 if ($class eq 'ARRAY') {
54 foreach my $el (@$tree) {
55 $remove_unknown_formats->($el);
57 } elsif ($class eq 'HASH') {
58 if (my $info = $tree->{info
}) {
59 for my $method (qw(GET PUT PUSH DELETE)) {
60 next if !$info->{$method};
61 my $properties = $info->{$method}->{parameters
}->{properties
};
62 $__real_remove_formats->($properties) if $properties;
65 if ($tree->{children
}) {
66 $remove_unknown_formats->($tree->{children
});
72 sub get_api_definition
{
74 if (!defined($pve_api_definition)) {
75 open(my $fh, '<', $pve_api_definition_fn) ||
76 die "unable to open '$pve_api_definition_fn' - $!\n";
77 $pve_api_definition = Storable
::fd_retrieve
($fh);
79 $remove_unknown_formats->($pve_api_definition);
82 return $pve_api_definition;
85 my $map_path_to_info = sub {
86 my ($child_list, $stack, $uri_param) = @_;
88 while (defined(my $comp = shift @$stack)) {
89 foreach my $child (@$child_list) {
90 my $text = $child->{text
};
94 } elsif ($text =~ m/^\{(\S+)\}$/) {
96 $uri_param->{$1} = $comp;
98 next; # text next child
100 if ($child->{leaf
} || !scalar(@$stack)) {
103 $child_list = $child->{children
};
104 last; # test next path component
111 sub find_method_info
{
112 my ($path, $method, $uri_param, $noerr) = @_;
116 my $stack = [ grep { length($_) > 0 } split('\/+' , $path)]; # skip empty fragments
118 my $api = get_api_definition
();
120 my $child = scalar(@$stack) ?
$map_path_to_info->($api->{children
}, $stack, $uri_param) : $api;
122 if (!($child && $child->{info
} && $child->{info
}->{$method})) {
123 return undef if $noerr;
124 die "unable to find API method '$method' for path '$path'\n";
127 return $child->{info
}->{$method};
130 sub complete_api_call_options
{
131 my ($cmd, $prop, $prev, $cur, $args) = @_;
133 my $print_result = sub {
135 print "$p\n" if $p =~ m/^$cur/;
139 my $print_parameter_completion = sub {
141 my $d = $prop->{$pname};
142 if ($d->{completion
}) {
143 my $vt = ref($d->{completion
});
145 my $res = $d->{completion
}->($cmd, $pname, $cur, $args);
146 &$print_result(@$res);
148 } elsif ($d->{type
} eq 'boolean') {
149 &$print_result('0', '1');
150 } elsif ($d->{enum
}) {
151 &$print_result(@{$d->{enum
}});
155 my @option_list = ();
156 foreach my $key (keys %$prop) {
157 push @option_list, "--$key";
161 &$print_result(@option_list);
165 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
167 &$print_parameter_completion($pname);
171 &$print_result(@option_list);
174 sub merge_api_definition_properties
{
175 my ($path, $method, $properties) = @_;
177 my $info = PVE
::APIClient
::Helpers
::find_method_info
($path, $method);
179 return { %{$info->{parameters
}->{properties
}}, %$properties };
182 sub complete_api_path
{
185 my $api = get_api_definition
(); # make sure API data is loaded
189 my ($dir, $rest) = $text =~ m
|^(?
:(.*)/)?(?:([^/]*))?
$|;
192 if (!defined($dir)) {
196 my $stack = [ grep { length($_) > 0 } split('\/+' , $dir)]; # skip empty fragments
197 $info = $map_path_to_info->($api->{children
}, $stack, {});
202 my $prefix = length($dir) ?
"/$dir/" : '/';
203 if (my $children = $info->{children
}) {
204 foreach my $c (@$children) {
205 my $ctext = $c->{text
};
206 push @$res, "${prefix}$ctext" if $ctext =~ m/^\Q$rest/;
207 if ($ctext =~ m/^\{(\S+)\}$/) {
208 if (length($rest) && $rest ne $ctext) {
209 push @$res, "$prefix$rest";
216 if (scalar(@$res) == 1) {
217 $res = [$res->[0], "$res->[0]/"];
223 # test for command lines with api calls (or similar bash completion calls):
224 # example1: pveclient api get remote1 /cluster
225 sub extract_path_info
{
226 my ($uri_param) = @_;
230 my $test_path_properties = sub {
233 return if scalar(@$args) < 5;
234 return if $args->[1] ne 'api';
236 my $path = $args->[4];
237 if (my $method = $method_map->{$args->[2]}) {
238 $info = find_method_info
($path, $method, $uri_param, 1);
242 if (defined(my $cmd = $ARGV[0])) {
244 $test_path_properties->([$0, @ARGV]);
245 } elsif ($cmd eq 'bashcomplete') {
246 my $cmdline = substr($ENV{COMP_LINE
}, 0, $ENV{COMP_POINT
});
247 my $args = PVE
::APIClient
::Tools
::split_args
($cmdline);
248 $test_path_properties->($args);
255 sub get_vmid_resource
{
256 my ($conn, $vmid) = @_;
258 my $resources = $conn->get('api2/json/cluster/resources', {type
=> 'vm'});
261 for my $tmp (@$resources) {
262 if ($tmp->{vmid
} eq $vmid) {
268 if (!defined($resource)) {
269 die "\"$vmid\" not found
";
276 my ($conn, $node, $upid, $quiet) = @_;
278 my $path = "api2
/json/nodes
/$node/tasks/$upid/status";
284 my $path = "api2
/json/nodes
/$node/tasks/$upid/log";
285 my $task_log = $conn->get($path, {start => $last_line});
288 for my $li (@$task_log) {
289 if ($li->{t} eq 'no content') {
292 $printme .= $li->{t} . "\n";
293 $last_line = $li->{n};
296 if ($printme ne '') {
301 $task_status = $conn->get($path, {});
303 if ($task_status->{status} eq "stopped
") {
310 if ($task_status->{exitstatus} ne "OK
") {
311 die $task_status->{exitstatus};
314 return $task_status->{exitstatus};
317 sub configuration_directory {
319 my $home = $ENV{HOME} // '';
320 my $xdg = $ENV{XDG_CONFIG_HOME} // '';
322 my $subdir = "pveclient
";
324 return "$xdg/$subdir" if length($xdg);
326 return "$home/.config
/$subdir" if length($home);
328 die "neither XDG_CONFIG_HOME nor HOME environment variable set
\n";
331 my $ticket_cache_filename = "/.tickets
";
333 sub ticket_cache_lookup {
336 my $dir = configuration_directory();
337 my $filename = "$dir/$ticket_cache_filename";
340 eval { $data = from_json(PVE::APIClient::Tools::file_get_contents($filename)); };
343 my $ticket = $data->{$remote};
344 return undef if !defined($ticket);
347 my $max_age = 3600*2 - 60;
349 if ($ticket =~ m/:([a-fA-F0-9]{8})::/) {
352 my $age = $ctime - $ttime;
354 return $ticket if ($age > $min_age) && ($age < $max_age);
360 sub ticket_cache_update {
361 my ($remote, $ticket) = @_;
363 my $dir = configuration_directory();
364 my $filename = "$dir/$ticket_cache_filename";
370 my $raw = PVE::APIClient::Tools::file_get_contents($filename);
371 eval { $data = from_json($raw); };
374 $data->{$remote} = $ticket;
376 PVE::APIClient::Tools::file_set_contents($filename, to_json($data), 0600);
379 PVE::APIClient::Tools::lock_file($filename, undef, $code);
383 sub extract_even_elements {
387 return [ grep { ($ind++ % 2) == 0 } @$list ];
390 sub print_ordered_result {
391 my ($property_list, $data, $result_schema, $options) = @_;
393 my $param_order = extract_even_elements($property_list);
395 PVE::APIClient::CLIFormatter::query_terminal_options($options);
396 PVE::APIClient::CLIFormatter::print_api_result($data, $result_schema, $param_order, $options);