1 package PVE
::APIClient
::Helpers
;
8 use File
::Path
qw(make_path);
10 use PVE
::APIClient
::Exception
qw(raise);
13 use HTTP
::Status
qw(:constants);
15 my $pve_api_definition;
16 my $pve_api_path_hash;
18 my $pve_api_definition_fn = "/usr/share/pve-client/pve-api-definition.dat";
27 my $build_pve_api_path_hash;
28 $build_pve_api_path_hash = sub {
31 my $class = ref($tree);
32 return $tree if !$class;
34 if ($class eq 'ARRAY') {
35 foreach my $el (@$tree) {
36 $build_pve_api_path_hash->($el);
38 } elsif ($class eq 'HASH') {
39 if (defined($tree->{leaf
}) && defined(my $path = $tree->{path
})) {
40 $pve_api_path_hash->{$path} = $tree;
42 foreach my $k (keys %$tree) {
43 $build_pve_api_path_hash->($tree->{$k});
48 my $default_output_format = 'text';
49 my $client_output_format = $default_output_format;
51 sub set_output_format
{
54 if (!defined($format)) {
55 $client_output_format = $default_output_format;
57 $client_output_format = $format;
61 sub get_output_format
{
62 return $client_output_format;
66 my ($data, $result_schema) = @_;
68 my $format = get_output_format
();
70 return if $result_schema->{type
} eq 'null';
72 # TODO: implement different output formats ($format)
74 if ($format eq 'json') {
75 print to_json
($data, {utf8
=> 1, allow_nonref
=> 1, canonical
=> 1, pretty
=> 1 });
76 } elsif ($format eq 'text') {
77 my $type = $result_schema->{type
};
78 if ($type eq 'object') {
80 } elsif ($type eq 'array') {
81 my $item_type = $result_schema->{items
}->{type
};
82 if ($item_type eq 'object') {
84 } elsif ($item_type eq 'array') {
87 foreach my $el (@$data) {
95 die "internal error: unknown output format"; # should not happen
99 sub get_api_definition
{
101 if (!defined($pve_api_definition)) {
102 open(my $fh, '<', $pve_api_definition_fn) ||
103 die "unable to open '$pve_api_definition_fn' - $!\n";
104 $pve_api_definition = Storable
::fd_retrieve
($fh);
105 $build_pve_api_path_hash->($pve_api_definition);
108 return $pve_api_definition;
111 my $map_path_to_info = sub {
112 my ($child_list, $stack, $uri_param) = @_;
114 while (defined(my $comp = shift @$stack)) {
115 foreach my $child (@$child_list) {
116 my $text = $child->{text
};
118 if ($text eq $comp) {
120 } elsif ($text =~ m/^\{(\S+)\}$/) {
122 $uri_param->{$1} = $comp;
124 next; # text next child
126 if ($child->{leaf
} || !scalar(@$stack)) {
129 $child_list = $child->{children
};
130 last; # test next path component
137 sub find_method_info
{
138 my ($path, $method, $uri_param, $noerr) = @_;
142 my $stack = [ grep { length($_) > 0 } split('\/+' , $path)]; # skip empty fragments
144 my $child = $map_path_to_info->(get_api_definition
(), $stack, $uri_param);
146 if (!($child && $child->{info
} && $child->{info
}->{$method})) {
147 return undef if $noerr;
148 die "unable to find API method '$method' for path '$path'\n";
151 return $child->{info
}->{$method};
154 sub complete_api_call_options
{
155 my ($cmd, $prop, $prev, $cur, $args) = @_;
157 my $print_result = sub {
159 print "$p\n" if $p =~ m/^$cur/;
163 my $print_parameter_completion = sub {
165 my $d = $prop->{$pname};
166 if ($d->{completion
}) {
167 my $vt = ref($d->{completion
});
169 my $res = $d->{completion
}->($cmd, $pname, $cur, $args);
170 &$print_result(@$res);
172 } elsif ($d->{type
} eq 'boolean') {
173 &$print_result('0', '1');
174 } elsif ($d->{enum
}) {
175 &$print_result(@{$d->{enum
}});
179 my @option_list = ();
180 foreach my $key (keys %$prop) {
181 push @option_list, "--$key";
185 &$print_result(@option_list);
189 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
191 &$print_parameter_completion($pname);
195 &$print_result(@option_list);
198 sub complete_api_path
{
201 get_api_definition
(); # make sure API data is loaded
205 my ($dir, $rest) = $text =~ m
|^(?
:(.*)/)?(?:([^/]*))?
$|;
208 if (!defined($dir)) {
210 $info = { children
=> $pve_api_definition };
212 my $stack = [ grep { length($_) > 0 } split('\/+' , $dir)]; # skip empty fragments
213 $info = $map_path_to_info->($pve_api_definition, $stack, {});
218 my $prefix = length($dir) ?
"/$dir/" : '/';
219 if (my $children = $info->{children
}) {
220 foreach my $c (@$children) {
221 my $ctext = $c->{text
};
222 if ($ctext =~ m/^\{(\S+)\}$/) {
223 push @$res, "$prefix$ctext";
224 push @$res, "$prefix$ctext/";
226 push @$res, "$prefix$rest";
227 push @$res, "$prefix$rest/";
229 } elsif ($ctext =~ m/^\Q$rest/) {
230 push @$res, "$prefix$ctext";
231 push @$res, "$prefix$ctext/" if $c->{children
};
240 # test for command lines with api calls (or similar bash completion calls):
241 # example1: pveclient api get remote1 /cluster
242 sub extract_path_info
{
243 my ($uri_param) = @_;
247 my $test_path_properties = sub {
250 return if scalar(@$args) < 5;
251 return if $args->[1] ne 'api';
253 my $path = $args->[4];
254 if (my $method = $method_map->{$args->[2]}) {
255 $info = find_method_info
($path, $method, $uri_param, 1);
259 if (defined(my $cmd = $ARGV[0])) {
261 $test_path_properties->([$0, @ARGV]);
262 } elsif ($cmd eq 'bashcomplete') {
263 my $cmdline = substr($ENV{COMP_LINE
}, 0, $ENV{COMP_POINT
});
264 my $args = PVE
::APIClient
::Tools
::split_args
($cmdline);
265 $test_path_properties->($args);
272 sub get_vmid_resource
{
273 my ($conn, $vmid) = @_;
275 my $resources = $conn->get('api2/json/cluster/resources', {type
=> 'vm'});
278 for my $tmp (@$resources) {
279 if ($tmp->{vmid
} eq $vmid) {
285 if (!defined($resource)) {
286 die "\"$vmid\" not found
";
293 my ($conn, $node, $upid) = @_;
295 my $path = "api2
/json/nodes
/$node/tasks/$upid/status";
299 $task_status = $conn->get($path, {});
301 if ($task_status->{status} eq "stopped
") {
308 return $task_status->{exitstatus};
311 sub configuration_directory {
313 my $home = $ENV{HOME} // '';
314 my $xdg = $ENV{XDG_CONFIG_HOME} // '';
316 my $subdir = "pveclient
";
318 return "$xdg/$subdir" if length($xdg);
320 return "$home/.config
/$subdir" if length($home);
322 die "neither XDG_CONFIG_HOME nor HOME environment variable set
\n";
325 my $ticket_cache_filename = "/.tickets
";
327 sub ticket_cache_lookup {
330 my $dir = configuration_directory();
331 my $filename = "$dir/$ticket_cache_filename";
334 eval { $data = from_json(PVE::APIClient::Tools::file_get_contents($filename)); };
337 my $ticket = $data->{$remote};
338 return undef if !defined($ticket);
341 my $max_age = 3600*2 - 60;
343 if ($ticket =~ m/:([a-fA-F0-9]{8})::/) {
346 my $age = $ctime - $ttime;
348 return $ticket if ($age > $min_age) && ($age < $max_age);
354 sub ticket_cache_update {
355 my ($remote, $ticket) = @_;
357 my $dir = configuration_directory();
358 my $filename = "$dir/$ticket_cache_filename";
364 my $raw = PVE::APIClient::Tools::file_get_contents($filename);
365 eval { $data = from_json($raw); };
368 $data->{$remote} = $ticket;
370 PVE::APIClient::Tools::file_set_contents($filename, to_json($data), 0600);
373 PVE::APIClient::Tools::lock_file($filename, undef, $code);