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;
44 my $__real_remove_formats; $__real_remove_formats = sub {
45 my ($properties) = @_;
47 foreach my $pname (keys %$properties) {
48 if (my $d = $properties->{$pname}) {
49 if (defined(my $format = $d->{format
})) {
51 $__real_remove_formats->($format);
52 } elsif (!PVE
::APIClient
::JSONSchema
::get_format
($format)) {
53 # simply remove unknown format definitions
61 my $remove_unknown_formats; $remove_unknown_formats = sub {
64 my $class = ref($tree);
67 if ($class eq 'ARRAY') {
68 foreach my $el (@$tree) {
69 $remove_unknown_formats->($el);
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;
79 if ($tree->{children
}) {
80 $remove_unknown_formats->($tree->{children
});
86 sub get_api_definition
{
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);
93 $remove_unknown_formats->($pve_api_definition);
96 return $pve_api_definition;
99 my $map_path_to_info = sub {
100 my ($child_list, $stack, $uri_param) = @_;
102 while (defined(my $comp = shift @$stack)) {
103 foreach my $child (@$child_list) {
104 my $text = $child->{text
};
106 if ($text eq $comp) {
108 } elsif ($text =~ m/^\{(\S+)\}$/) {
110 $uri_param->{$1} = $comp;
112 next; # text next child
114 if ($child->{leaf
} || !scalar(@$stack)) {
117 $child_list = $child->{children
};
118 last; # test next path component
125 sub find_method_info
{
126 my ($path, $method, $uri_param, $noerr) = @_;
130 my $stack = [ grep { length($_) > 0 } split('\/+' , $path)]; # skip empty fragments
132 my $api = get_api_definition
();
134 my $child = scalar(@$stack) ?
$map_path_to_info->($api->{children
}, $stack, $uri_param) : $api;
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";
141 return $child->{info
}->{$method};
144 sub complete_api_call_options
{
145 my ($cmd, $prop, $prev, $cur, $args) = @_;
147 my $print_result = sub {
149 print "$p\n" if $p =~ m/^$cur/;
153 my $print_parameter_completion = sub {
155 my $d = $prop->{$pname};
156 if ($d->{completion
}) {
157 my $vt = ref($d->{completion
});
159 my $res = $d->{completion
}->($cmd, $pname, $cur, $args);
160 &$print_result(@$res);
162 } elsif ($d->{type
} eq 'boolean') {
163 &$print_result('0', '1');
164 } elsif ($d->{enum
}) {
165 &$print_result(@{$d->{enum
}});
169 my @option_list = ();
170 foreach my $key (keys %$prop) {
171 push @option_list, "--$key";
175 &$print_result(@option_list);
179 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
181 &$print_parameter_completion($pname);
185 &$print_result(@option_list);
188 sub merge_api_definition_properties
{
189 my ($path, $method, $properties) = @_;
191 my $info = PVE
::APIClient
::Helpers
::find_method_info
($path, $method);
193 return { %{$info->{parameters
}->{properties
}}, %$properties };
196 sub complete_api_path
{
199 my $api = get_api_definition
(); # make sure API data is loaded
203 my ($dir, $rest) = $text =~ m
|^(?
:(.*)/)?(?:([^/]*))?
$|;
206 if (!defined($dir)) {
210 my $stack = [ grep { length($_) > 0 } split('\/+' , $dir)]; # skip empty fragments
211 $info = $map_path_to_info->($api->{children
}, $stack, {});
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/";
224 push @$res, "$prefix$rest";
225 push @$res, "$prefix$rest/";
227 } elsif ($ctext =~ m/^\Q$rest/) {
228 push @$res, "$prefix$ctext";
229 push @$res, "$prefix$ctext/" if $c->{children
};
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) = @_;
245 my $test_path_properties = sub {
248 return if scalar(@$args) < 5;
249 return if $args->[1] ne 'api';
251 my $path = $args->[4];
252 if (my $method = $method_map->{$args->[2]}) {
253 $info = find_method_info
($path, $method, $uri_param, 1);
257 if (defined(my $cmd = $ARGV[0])) {
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);
270 sub get_vmid_resource
{
271 my ($conn, $vmid) = @_;
273 my $resources = $conn->get('api2/json/cluster/resources', {type
=> 'vm'});
276 for my $tmp (@$resources) {
277 if ($tmp->{vmid
} eq $vmid) {
283 if (!defined($resource)) {
284 die "\"$vmid\" not found
";
291 my ($conn, $node, $upid, $quiet) = @_;
293 my $path = "api2
/json/nodes
/$node/tasks/$upid/status";
299 my $path = "api2
/json/nodes
/$node/tasks/$upid/log";
300 my $task_log = $conn->get($path, {start => $last_line});
303 for my $li (@$task_log) {
304 if ($li->{t} eq 'no content') {
307 $printme .= $li->{t} . "\n";
308 $last_line = $li->{n};
311 if ($printme ne '') {
316 $task_status = $conn->get($path, {});
318 if ($task_status->{status} eq "stopped
") {
325 if ($task_status->{exitstatus} ne "OK
") {
326 die $task_status->{exitstatus};
329 return $task_status->{exitstatus};
332 sub configuration_directory {
334 my $home = $ENV{HOME} // '';
335 my $xdg = $ENV{XDG_CONFIG_HOME} // '';
337 my $subdir = "pveclient
";
339 return "$xdg/$subdir" if length($xdg);
341 return "$home/.config
/$subdir" if length($home);
343 die "neither XDG_CONFIG_HOME nor HOME environment variable set
\n";
346 my $ticket_cache_filename = "/.tickets
";
348 sub ticket_cache_lookup {
351 my $dir = configuration_directory();
352 my $filename = "$dir/$ticket_cache_filename";
355 eval { $data = from_json(PVE::APIClient::Tools::file_get_contents($filename)); };
358 my $ticket = $data->{$remote};
359 return undef if !defined($ticket);
362 my $max_age = 3600*2 - 60;
364 if ($ticket =~ m/:([a-fA-F0-9]{8})::/) {
367 my $age = $ctime - $ttime;
369 return $ticket if ($age > $min_age) && ($age < $max_age);
375 sub ticket_cache_update {
376 my ($remote, $ticket) = @_;
378 my $dir = configuration_directory();
379 my $filename = "$dir/$ticket_cache_filename";
385 my $raw = PVE::APIClient::Tools::file_get_contents($filename);
386 eval { $data = from_json($raw); };
389 $data->{$remote} = $ticket;
391 PVE::APIClient::Tools::file_set_contents($filename, to_json($data), 0600);
394 PVE::APIClient::Tools::lock_file($filename, undef, $code);