1 package PVE
::APIClient
::Helpers
;
8 use PVE
::APIClient
::Exception
qw(raise);
11 use HTTP
::Status
qw(:constants);
13 my $pve_api_definition;
14 my $pve_api_path_hash;
16 my $pve_api_definition_fn = "/usr/share/pve-client/pve-api-definition.dat";
25 my $build_pve_api_path_hash;
26 $build_pve_api_path_hash = sub {
29 my $class = ref($tree);
30 return $tree if !$class;
32 if ($class eq 'ARRAY') {
33 foreach my $el (@$tree) {
34 $build_pve_api_path_hash->($el);
36 } elsif ($class eq 'HASH') {
37 if (defined($tree->{leaf
}) && defined(my $path = $tree->{path
})) {
38 $pve_api_path_hash->{$path} = $tree;
40 foreach my $k (keys %$tree) {
41 $build_pve_api_path_hash->($tree->{$k});
46 my $default_output_format = 'table';
47 my $client_output_format = $default_output_format;
49 sub set_output_format
{
52 if (!defined($format)) {
53 $client_output_format = $default_output_format;
55 $client_output_format = $format;
59 sub get_output_format
{
60 return $client_output_format;
63 sub get_api_definition
{
65 if (!defined($pve_api_definition)) {
66 open(my $fh, '<', $pve_api_definition_fn) ||
67 die "unable to open '$pve_api_definition_fn' - $!\n";
68 $pve_api_definition = Storable
::fd_retrieve
($fh);
69 $build_pve_api_path_hash->($pve_api_definition);
72 return $pve_api_definition;
75 sub lookup_api_method
{
76 my ($path, $method, $noerr) = @_;
78 get_api_definition
(); # make sure API data is loaded
80 my $info = $pve_api_path_hash->{$path};
83 return undef if $noerr;
84 die "unable to find API info for path '$path'\n";
87 my $data = $info->{info
}->{$method};
90 return undef if $noerr;
91 die "unable to find API method '$method' for path '$path'\n";
97 sub complete_api_call_options
{
98 my ($cmd, $prop, $prev, $cur, $args) = @_;
100 my $print_result = sub {
102 print "$p\n" if $p =~ m/^$cur/;
106 my $print_parameter_completion = sub {
108 my $d = $prop->{$pname};
109 if ($d->{completion
}) {
110 my $vt = ref($d->{completion
});
112 my $res = $d->{completion
}->($cmd, $pname, $cur, $args);
113 &$print_result(@$res);
115 } elsif ($d->{type
} eq 'boolean') {
116 &$print_result('0', '1');
117 } elsif ($d->{enum
}) {
118 &$print_result(@{$d->{enum
}});
122 my @option_list = ();
123 foreach my $key (keys %$prop) {
124 push @option_list, "--$key";
128 &$print_result(@option_list);
132 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
134 &$print_parameter_completion($pname);
138 &$print_result(@option_list);
141 sub complete_api_path
{
144 get_api_definition
(); # make sure API data is loaded
148 my ($dir, $rest) = $text =~ m
|^(?
:(.*)/)?(?:([^/]*))?
$|;
151 if (!defined($dir)) {
153 $info = { children
=> $pve_api_definition };
155 $info = $pve_api_path_hash->{"/$dir"};
160 if (my $children = $info->{children
}) {
161 foreach my $c (@$children) {
162 if ($c->{path
} =~ m!\Q$dir/$rest!) {
163 push @$res, $c->{path
};
164 push @$res, "$c->{path}/" if $c->{children
};
172 # test for command lines with api calls (or similar bash completion calls):
173 # example1: pveclient api get remote1 /cluster
174 sub extract_path_info
{
178 my $test_path_properties = sub {
181 return if scalar(@$args) < 5;
182 return if $args->[1] ne 'api';
184 my $path = $args->[4];
185 if (my $method = $method_map->{$args->[2]}) {
186 $info = lookup_api_method
($path, $method, 1);
190 if (defined(my $cmd = $ARGV[0])) {
192 $test_path_properties->([$0, @ARGV]);
193 } elsif ($cmd eq 'bashcomplete') {
194 my $cmdline = substr($ENV{COMP_LINE
}, 0, $ENV{COMP_POINT
});
195 my $args = PVE
::APIClient
::Tools
::split_args
($cmdline);
196 $test_path_properties->($args);
203 sub get_vmid_resource
{
204 my ($conn, $vmid) = @_;
206 my $resources = $conn->get('api2/json/cluster/resources', {type
=> 'vm'});
209 for my $tmp (@$resources) {
210 if ($tmp->{vmid
} eq $vmid) {
216 if (!defined($resource)) {
217 die "\"$vmid\" not found
";
224 my ($conn, $node, $upid) = @_;
226 my $path = "api2
/json/nodes
/$node/tasks/$upid/status";
230 $task_status = $conn->get($path, {});
232 if ($task_status->{status} eq "stopped
") {
239 return $task_status->{exitstatus};