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;
64 my ($data, $result_schema) = @_;
66 my $format = get_output_format
();
68 return if $result_schema->{type
} eq 'null';
70 # TODO: implement different output formats ($format)
72 if ($format eq 'json') {
73 print to_json
($data, {utf8
=> 1, allow_nonref
=> 1, canonical
=> 1, pretty
=> 1 });
74 } elsif ($format eq 'table') {
75 my $type = $result_schema->{type
};
76 if ($type eq 'object') {
78 } elsif ($type eq 'array') {
79 my $item_type = $result_schema->{items
}->{type
};
80 if ($item_type eq 'object') {
82 } elsif ($item_type eq 'array') {
85 foreach my $el (@$data) {
93 die "internal error: unknown output format"; # should not happen
97 sub get_api_definition
{
99 if (!defined($pve_api_definition)) {
100 open(my $fh, '<', $pve_api_definition_fn) ||
101 die "unable to open '$pve_api_definition_fn' - $!\n";
102 $pve_api_definition = Storable
::fd_retrieve
($fh);
103 $build_pve_api_path_hash->($pve_api_definition);
106 return $pve_api_definition;
109 sub lookup_api_method
{
110 my ($path, $method, $noerr) = @_;
112 get_api_definition
(); # make sure API data is loaded
114 my $info = $pve_api_path_hash->{$path};
117 return undef if $noerr;
118 die "unable to find API info for path '$path'\n";
121 my $data = $info->{info
}->{$method};
124 return undef if $noerr;
125 die "unable to find API method '$method' for path '$path'\n";
131 sub complete_api_call_options
{
132 my ($cmd, $prop, $prev, $cur, $args) = @_;
134 my $print_result = sub {
136 print "$p\n" if $p =~ m/^$cur/;
140 my $print_parameter_completion = sub {
142 my $d = $prop->{$pname};
143 if ($d->{completion
}) {
144 my $vt = ref($d->{completion
});
146 my $res = $d->{completion
}->($cmd, $pname, $cur, $args);
147 &$print_result(@$res);
149 } elsif ($d->{type
} eq 'boolean') {
150 &$print_result('0', '1');
151 } elsif ($d->{enum
}) {
152 &$print_result(@{$d->{enum
}});
156 my @option_list = ();
157 foreach my $key (keys %$prop) {
158 push @option_list, "--$key";
162 &$print_result(@option_list);
166 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
168 &$print_parameter_completion($pname);
172 &$print_result(@option_list);
175 sub complete_api_path
{
178 get_api_definition
(); # make sure API data is loaded
182 my ($dir, $rest) = $text =~ m
|^(?
:(.*)/)?(?:([^/]*))?
$|;
185 if (!defined($dir)) {
187 $info = { children
=> $pve_api_definition };
189 $info = $pve_api_path_hash->{"/$dir"};
194 if (my $children = $info->{children
}) {
195 foreach my $c (@$children) {
196 if ($c->{path
} =~ m!\Q$dir/$rest!) {
197 push @$res, $c->{path
};
198 push @$res, "$c->{path}/" if $c->{children
};
206 # test for command lines with api calls (or similar bash completion calls):
207 # example1: pveclient api get remote1 /cluster
208 sub extract_path_info
{
212 my $test_path_properties = sub {
215 return if scalar(@$args) < 5;
216 return if $args->[1] ne 'api';
218 my $path = $args->[4];
219 if (my $method = $method_map->{$args->[2]}) {
220 $info = lookup_api_method
($path, $method, 1);
224 if (defined(my $cmd = $ARGV[0])) {
226 $test_path_properties->([$0, @ARGV]);
227 } elsif ($cmd eq 'bashcomplete') {
228 my $cmdline = substr($ENV{COMP_LINE
}, 0, $ENV{COMP_POINT
});
229 my $args = PVE
::APIClient
::Tools
::split_args
($cmdline);
230 $test_path_properties->($args);
237 sub get_vmid_resource
{
238 my ($conn, $vmid) = @_;
240 my $resources = $conn->get('api2/json/cluster/resources', {type
=> 'vm'});
243 for my $tmp (@$resources) {
244 if ($tmp->{vmid
} eq $vmid) {
250 if (!defined($resource)) {
251 die "\"$vmid\" not found
";
258 my ($conn, $node, $upid) = @_;
260 my $path = "api2
/json/nodes
/$node/tasks/$upid/status";
264 $task_status = $conn->get($path, {});
266 if ($task_status->{status} eq "stopped
") {
273 return $task_status->{exitstatus};