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
79 my $__real_remove_formats; $__real_remove_formats = sub {
80 my ($properties) = @_;
82 foreach my $pname (keys %$properties) {
83 if (my $d = $properties->{$pname}) {
84 if (defined(my $format = $d->{format
})) {
86 $__real_remove_formats->($format);
87 } elsif (!PVE
::APIClient
::JSONSchema
::get_format
($format)) {
88 # simply remove unknown format definitions
96 my $remove_unknown_formats; $remove_unknown_formats = sub {
99 my $class = ref($tree);
102 if ($class eq 'ARRAY') {
103 foreach my $el (@$tree) {
104 $remove_unknown_formats->($el);
106 } elsif ($class eq 'HASH') {
107 if (my $info = $tree->{info
}) {
108 for my $method (qw(GET PUT PUSH DELETE)) {
109 next if !$info->{$method};
110 my $properties = $info->{$method}->{parameters
}->{properties
};
111 $__real_remove_formats->($properties) if $properties;
114 if ($tree->{children
}) {
115 $remove_unknown_formats->($tree->{children
});
121 sub get_api_definition
{
123 if (!defined($pve_api_definition)) {
124 open(my $fh, '<', $pve_api_definition_fn) ||
125 die "unable to open '$pve_api_definition_fn' - $!\n";
126 $pve_api_definition = Storable
::fd_retrieve
($fh);
128 $remove_unknown_formats->($pve_api_definition);
131 return $pve_api_definition;
134 my $map_path_to_info = sub {
135 my ($child_list, $stack, $uri_param) = @_;
137 while (defined(my $comp = shift @$stack)) {
138 foreach my $child (@$child_list) {
139 my $text = $child->{text
};
141 if ($text eq $comp) {
143 } elsif ($text =~ m/^\{(\S+)\}$/) {
145 $uri_param->{$1} = $comp;
147 next; # text next child
149 if ($child->{leaf
} || !scalar(@$stack)) {
152 $child_list = $child->{children
};
153 last; # test next path component
160 sub find_method_info
{
161 my ($path, $method, $uri_param, $noerr) = @_;
165 my $stack = [ grep { length($_) > 0 } split('\/+' , $path)]; # skip empty fragments
167 my $child = $map_path_to_info->(get_api_definition
(), $stack, $uri_param);
169 if (!($child && $child->{info
} && $child->{info
}->{$method})) {
170 return undef if $noerr;
171 die "unable to find API method '$method' for path '$path'\n";
174 return $child->{info
}->{$method};
177 sub complete_api_call_options
{
178 my ($cmd, $prop, $prev, $cur, $args) = @_;
180 my $print_result = sub {
182 print "$p\n" if $p =~ m/^$cur/;
186 my $print_parameter_completion = sub {
188 my $d = $prop->{$pname};
189 if ($d->{completion
}) {
190 my $vt = ref($d->{completion
});
192 my $res = $d->{completion
}->($cmd, $pname, $cur, $args);
193 &$print_result(@$res);
195 } elsif ($d->{type
} eq 'boolean') {
196 &$print_result('0', '1');
197 } elsif ($d->{enum
}) {
198 &$print_result(@{$d->{enum
}});
202 my @option_list = ();
203 foreach my $key (keys %$prop) {
204 push @option_list, "--$key";
208 &$print_result(@option_list);
212 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
214 &$print_parameter_completion($pname);
218 &$print_result(@option_list);
221 sub merge_api_definition_properties
{
222 my ($path, $method, $properties) = @_;
224 my $info = PVE
::APIClient
::Helpers
::find_method_info
($path, $method);
226 return { %{$info->{parameters
}->{properties
}}, %$properties };
229 sub complete_api_path
{
232 get_api_definition
(); # make sure API data is loaded
236 my ($dir, $rest) = $text =~ m
|^(?
:(.*)/)?(?:([^/]*))?
$|;
239 if (!defined($dir)) {
241 $info = { children
=> $pve_api_definition };
243 my $stack = [ grep { length($_) > 0 } split('\/+' , $dir)]; # skip empty fragments
244 $info = $map_path_to_info->($pve_api_definition, $stack, {});
249 my $prefix = length($dir) ?
"/$dir/" : '/';
250 if (my $children = $info->{children
}) {
251 foreach my $c (@$children) {
252 my $ctext = $c->{text
};
253 if ($ctext =~ m/^\{(\S+)\}$/) {
254 push @$res, "$prefix$ctext";
255 push @$res, "$prefix$ctext/";
257 push @$res, "$prefix$rest";
258 push @$res, "$prefix$rest/";
260 } elsif ($ctext =~ m/^\Q$rest/) {
261 push @$res, "$prefix$ctext";
262 push @$res, "$prefix$ctext/" if $c->{children
};
271 # test for command lines with api calls (or similar bash completion calls):
272 # example1: pveclient api get remote1 /cluster
273 sub extract_path_info
{
274 my ($uri_param) = @_;
278 my $test_path_properties = sub {
281 return if scalar(@$args) < 5;
282 return if $args->[1] ne 'api';
284 my $path = $args->[4];
285 if (my $method = $method_map->{$args->[2]}) {
286 $info = find_method_info
($path, $method, $uri_param, 1);
290 if (defined(my $cmd = $ARGV[0])) {
292 $test_path_properties->([$0, @ARGV]);
293 } elsif ($cmd eq 'bashcomplete') {
294 my $cmdline = substr($ENV{COMP_LINE
}, 0, $ENV{COMP_POINT
});
295 my $args = PVE
::APIClient
::Tools
::split_args
($cmdline);
296 $test_path_properties->($args);
303 sub get_vmid_resource
{
304 my ($conn, $vmid) = @_;
306 my $resources = $conn->get('api2/json/cluster/resources', {type
=> 'vm'});
309 for my $tmp (@$resources) {
310 if ($tmp->{vmid
} eq $vmid) {
316 if (!defined($resource)) {
317 die "\"$vmid\" not found
";
324 my ($conn, $node, $upid, $quiet) = @_;
326 my $path = "api2
/json/nodes
/$node/tasks/$upid/status";
332 my $path = "api2
/json/nodes
/$node/tasks/$upid/log";
333 my $task_log = $conn->get($path, {start => $last_line});
336 for my $li (@$task_log) {
337 if ($li->{t} eq 'no content') {
340 $printme .= $li->{t} . "\n";
341 $last_line = $li->{n};
344 if ($printme ne '') {
349 $task_status = $conn->get($path, {});
351 if ($task_status->{status} eq "stopped
") {
358 return $task_status->{exitstatus};
361 sub configuration_directory {
363 my $home = $ENV{HOME} // '';
364 my $xdg = $ENV{XDG_CONFIG_HOME} // '';
366 my $subdir = "pveclient
";
368 return "$xdg/$subdir" if length($xdg);
370 return "$home/.config
/$subdir" if length($home);
372 die "neither XDG_CONFIG_HOME nor HOME environment variable set
\n";
375 my $ticket_cache_filename = "/.tickets
";
377 sub ticket_cache_lookup {
380 my $dir = configuration_directory();
381 my $filename = "$dir/$ticket_cache_filename";
384 eval { $data = from_json(PVE::APIClient::Tools::file_get_contents($filename)); };
387 my $ticket = $data->{$remote};
388 return undef if !defined($ticket);
391 my $max_age = 3600*2 - 60;
393 if ($ticket =~ m/:([a-fA-F0-9]{8})::/) {
396 my $age = $ctime - $ttime;
398 return $ticket if ($age > $min_age) && ($age < $max_age);
404 sub ticket_cache_update {
405 my ($remote, $ticket) = @_;
407 my $dir = configuration_directory();
408 my $filename = "$dir/$ticket_cache_filename";
414 my $raw = PVE::APIClient::Tools::file_get_contents($filename);
415 eval { $data = from_json($raw); };
418 $data->{$remote} = $ticket;
420 PVE::APIClient::Tools::file_set_contents($filename, to_json($data), 0600);
423 PVE::APIClient::Tools::lock_file($filename, undef, $code);