+# test for command lines with api calls (or similar bash completion calls):
+# example1: pveclient api get remote1 /cluster
+sub extract_path_info {
+ my ($uri_param) = @_;
+
+ my $info;
+
+ my $test_path_properties = sub {
+ my ($args) = @_;
+
+ return if scalar(@$args) < 5;
+ return if $args->[1] ne 'api';
+
+ my $path = $args->[4];
+ if (my $method = $method_map->{$args->[2]}) {
+ $info = find_method_info($path, $method, $uri_param, 1);
+ }
+ };
+
+ if (defined(my $cmd = $ARGV[0])) {
+ if ($cmd eq 'api') {
+ $test_path_properties->([$0, @ARGV]);
+ } elsif ($cmd eq 'bashcomplete') {
+ my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT});
+ my $args = PVE::APIClient::Tools::split_args($cmdline);
+ $test_path_properties->($args);
+ }
+ }
+
+ return $info;
+}
+
+sub get_vmid_resource {
+ my ($conn, $vmid) = @_;
+
+ my $resources = $conn->get('api2/json/cluster/resources', {type => 'vm'});
+
+ my $resource;
+ for my $tmp (@$resources) {
+ if ($tmp->{vmid} eq $vmid) {
+ $resource = $tmp;
+ last;
+ }
+ }
+
+ if (!defined($resource)) {
+ die "\"$vmid\" not found";
+ }
+
+ return $resource;
+}
+
+sub poll_task {
+ my ($conn, $node, $upid) = @_;
+
+ my $path = "api2/json/nodes/$node/tasks/$upid/status";
+
+ my $task_status;
+ while(1) {
+ $task_status = $conn->get($path, {});
+
+ if ($task_status->{status} eq "stopped") {
+ last;
+ }
+
+ sleep(10);
+ }
+
+ return $task_status->{exitstatus};
+}
+
+sub configuration_directory {
+
+ my $home = $ENV{HOME} // '';
+ my $xdg = $ENV{XDG_CONFIG_HOME} // '';
+
+ my $subdir = "pveclient";
+
+ return "$xdg/$subdir" if length($xdg);
+
+ return "$home/.config/$subdir" if length($home);
+
+ die "neither XDG_CONFIG_HOME nor HOME environment variable set\n";
+}
+
+my $ticket_cache_filename = "/.tickets";
+
+sub ticket_cache_lookup {
+ my ($remote) = @_;
+
+ my $dir = configuration_directory();
+ my $filename = "$dir/$ticket_cache_filename";
+
+ my $data = {};
+ eval { $data = from_json(PVE::APIClient::Tools::file_get_contents($filename)); };
+ # ignore errors
+
+ my $ticket = $data->{$remote};
+ return undef if !defined($ticket);
+
+ my $min_age = - 60;
+ my $max_age = 3600*2 - 60;
+
+ if ($ticket =~ m/:([a-fA-F0-9]{8})::/) {
+ my $ttime = hex($1);
+ my $ctime = time();
+ my $age = $ctime - $ttime;
+
+ return $ticket if ($age > $min_age) && ($age < $max_age);
+ }
+
+ return undef;
+}
+
+sub ticket_cache_update {
+ my ($remote, $ticket) = @_;
+
+ my $dir = configuration_directory();
+ my $filename = "$dir/$ticket_cache_filename";
+
+ my $code = sub {
+ make_path($dir);
+ my $data = {};
+ if (-f $filename) {
+ my $raw = PVE::APIClient::Tools::file_get_contents($filename);
+ eval { $data = from_json($raw); };
+ # ignore errors
+ }
+ $data->{$remote} = $ticket;
+
+ PVE::APIClient::Tools::file_set_contents($filename, to_json($data), 0600);
+ };
+
+ PVE::APIClient::Tools::lock_file($filename, undef, $code);
+ die $@ if $@;
+}
+
+