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 sub lookup_api_method
{
112 my ($path, $method, $noerr) = @_;
114 get_api_definition
(); # make sure API data is loaded
116 my $info = $pve_api_path_hash->{$path};
119 return undef if $noerr;
120 die "unable to find API info for path '$path'\n";
123 my $data = $info->{info
}->{$method};
126 return undef if $noerr;
127 die "unable to find API method '$method' for path '$path'\n";
133 sub complete_api_call_options
{
134 my ($cmd, $prop, $prev, $cur, $args) = @_;
136 my $print_result = sub {
138 print "$p\n" if $p =~ m/^$cur/;
142 my $print_parameter_completion = sub {
144 my $d = $prop->{$pname};
145 if ($d->{completion
}) {
146 my $vt = ref($d->{completion
});
148 my $res = $d->{completion
}->($cmd, $pname, $cur, $args);
149 &$print_result(@$res);
151 } elsif ($d->{type
} eq 'boolean') {
152 &$print_result('0', '1');
153 } elsif ($d->{enum
}) {
154 &$print_result(@{$d->{enum
}});
158 my @option_list = ();
159 foreach my $key (keys %$prop) {
160 push @option_list, "--$key";
164 &$print_result(@option_list);
168 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
170 &$print_parameter_completion($pname);
174 &$print_result(@option_list);
177 sub complete_api_path
{
180 get_api_definition
(); # make sure API data is loaded
184 my ($dir, $rest) = $text =~ m
|^(?
:(.*)/)?(?:([^/]*))?
$|;
187 if (!defined($dir)) {
189 $info = { children
=> $pve_api_definition };
191 $info = $pve_api_path_hash->{"/$dir"};
196 if (my $children = $info->{children
}) {
197 foreach my $c (@$children) {
198 if ($c->{path
} =~ m!\Q$dir/$rest!) {
199 push @$res, $c->{path
};
200 push @$res, "$c->{path}/" if $c->{children
};
208 # test for command lines with api calls (or similar bash completion calls):
209 # example1: pveclient api get remote1 /cluster
210 sub extract_path_info
{
214 my $test_path_properties = sub {
217 return if scalar(@$args) < 5;
218 return if $args->[1] ne 'api';
220 my $path = $args->[4];
221 if (my $method = $method_map->{$args->[2]}) {
222 $info = lookup_api_method
($path, $method, 1);
226 if (defined(my $cmd = $ARGV[0])) {
228 $test_path_properties->([$0, @ARGV]);
229 } elsif ($cmd eq 'bashcomplete') {
230 my $cmdline = substr($ENV{COMP_LINE
}, 0, $ENV{COMP_POINT
});
231 my $args = PVE
::APIClient
::Tools
::split_args
($cmdline);
232 $test_path_properties->($args);
239 sub get_vmid_resource
{
240 my ($conn, $vmid) = @_;
242 my $resources = $conn->get('api2/json/cluster/resources', {type
=> 'vm'});
245 for my $tmp (@$resources) {
246 if ($tmp->{vmid
} eq $vmid) {
252 if (!defined($resource)) {
253 die "\"$vmid\" not found
";
260 my ($conn, $node, $upid) = @_;
262 my $path = "api2
/json/nodes
/$node/tasks/$upid/status";
266 $task_status = $conn->get($path, {});
268 if ($task_status->{status} eq "stopped
") {
275 return $task_status->{exitstatus};
278 sub configuration_directory {
280 my $home = $ENV{HOME} // '';
281 my $xdg = $ENV{XDG_CONFIG_HOME} // '';
283 my $subdir = "pveclient
";
285 return "$xdg/$subdir" if length($xdg);
287 return "$home/.config
/$subdir" if length($home);
289 die "neither XDG_CONFIG_HOME nor HOME environment variable set
\n";
292 my $ticket_cache_filename = "/.tickets
";
294 sub ticket_cache_lookup {
297 my $dir = configuration_directory();
298 my $filename = "$dir/$ticket_cache_filename";
301 eval { $data = from_json(PVE::APIClient::Tools::file_get_contents($filename)); };
304 my $ticket = $data->{$remote};
305 return undef if !defined($ticket);
308 my $max_age = 3600*2 - 60;
310 if ($ticket =~ m/:([a-fA-F0-9]{8})::/) {
313 my $age = $ctime - $ttime;
315 return $ticket if ($age > $min_age) && ($age < $max_age);
321 sub ticket_cache_update {
322 my ($remote, $ticket) = @_;
324 my $dir = configuration_directory();
325 my $filename = "$dir/$ticket_cache_filename";
331 my $raw = PVE::APIClient::Tools::file_get_contents($filename);
332 eval { $data = from_json($raw); };
335 $data->{$remote} = $ticket;
337 PVE::APIClient::Tools::file_set_contents($filename, to_json($data), 0600);
340 PVE::APIClient::Tools::lock_file($filename, undef, $code);