3 package PVE
::CLI
::pveclient
;
10 use PVE
::APIClient
::JSONSchema
qw(register_standard_option get_standard_option);
11 use PVE
::APIClient
::RESTHandler
;
12 use PVE
::APIClient
::CLIFormatter
;
13 use PVE
::APIClient
::CLIHandler
;
14 use PVE
::APIClient
::PTY
;
16 use PVE
::APIClient
::LWP
;
17 use PVE
::APIClient
::Helpers
;
18 use PVE
::APIClient
::Config
;
19 use PVE
::APIClient
::Commands
::config
;
20 use PVE
::APIClient
::Commands
::remote
;
21 use PVE
::APIClient
::Commands
::list
;
22 use PVE
::APIClient
::Commands
::lxc
;
23 use PVE
::APIClient
::Commands
::GuestStatus
;
28 my ($method, $param) = @_;
30 my $path = PVE
::APIClient
::Tools
::extract_param
($param, 'api_path');
31 die "missing API path\n" if !defined($path);
33 my $stdopts = PVE
::APIClient
::RESTHandler
::extract_standard_output_properties
($param);
34 PVE
::APIClient
::CLIFormatter
::query_terminal_options
($stdopts);
36 my $remote = PVE
::APIClient
::Tools
::extract_param
($param, 'remote');
37 die "missing remote\n" if !defined($remote);
39 my $config = PVE
::APIClient
::Config-
>load();
42 my $info = PVE
::APIClient
::Helpers
::find_method_info
($path, $method, $uri_param);
44 my $conn = PVE
::APIClient
::Config-
>remote_conn($config, $remote);
46 my $res = $conn->call($method, "api2/json/$path", $param);
47 die "undefined result" if !defined($res);
48 die "undefined result data" if !exists($res->{data
});
50 my $data = $res->{data
};
52 PVE
::APIClient
::CLIFormatter
::print_api_result
($data, $info->{returns
}, undef, $stdopts);
55 use base
qw(PVE::APIClient::CLIHandler);
58 return PVE
::APIClient
::PTY
::read_password
("Remote password: ")
64 if ($cmd && $cmd eq 'packagedepends') {
65 # experimental code to print required perl packages
67 my $dir = Cwd
::getcwd
;
69 foreach my $k (keys %INC) {
70 my $file = abs_path
($INC{$k});
71 next if $file =~ m/^\Q$dir\E/;
72 my $res = `dpkg -S '$file'`;
73 if ($res && $res =~ m/^(\S+): $file$/) {
74 my $debian_package = $1;
75 $debian_package =~ s/:amd64$//;
76 $packages->{$debian_package} = 1;
78 die "unable to find package for '$file'\n";
81 print join("\n", sort(keys %$packages)) . "\n";
86 my $path_properties = {};
88 my $api_path_property = {
89 description
=> "API path.",
92 my ($cmd, $pname, $cur, $args) = @_;
93 return PVE
::APIClient
::Helpers
::complete_api_path
($cur);
97 # dynamically update schema definition for direct API call
98 # like: pveclient api <get|set|create|delete|help> <remote> <path>
100 if (my $info = PVE
::APIClient
::Helpers
::extract_path_info
($uri_param)) {
101 foreach my $key (keys %{$info->{parameters
}->{properties
}}) {
102 next if defined($uri_param->{$key});
103 $path_properties->{$key} = $info->{parameters
}->{properties
}->{$key};
107 $path_properties->{remote
} = get_standard_option
('pveclient-remote-name');
108 $path_properties->{api_path
} = $api_path_property;
110 __PACKAGE__-
>register_method ({
111 name
=> 'pveclient_get',
112 path
=> 'pveclient_get',
114 description
=> "Call API GET on <api_path>.",
116 additionalProperties
=> 0,
117 properties
=> PVE
::APIClient
::RESTHandler
::add_standard_output_properties
($path_properties),
119 returns
=> { type
=> 'null' },
123 call_api_method
('GET', $param);
128 __PACKAGE__-
>register_method ({
129 name
=> 'pveclient_set',
130 path
=> 'pveclient_set',
132 description
=> "Call API PUT on <api_path>.",
134 additionalProperties
=> 0,
135 properties
=> PVE
::APIClient
::RESTHandler
::add_standard_output_properties
($path_properties),
137 returns
=> { type
=> 'null' },
141 call_api_method
('PUT', $param);
146 __PACKAGE__-
>register_method ({
147 name
=> 'pveclient_create',
148 path
=> 'pveclient_create',
150 description
=> "Call API POST on <api_path>.",
152 additionalProperties
=> 0,
153 properties
=> PVE
::APIClient
::RESTHandler
::add_standard_output_properties
($path_properties),
155 returns
=> { type
=> 'null' },
159 call_api_method
('PUSH', $param);
164 __PACKAGE__-
>register_method ({
165 name
=> 'pveclient_delete',
166 path
=> 'pveclient_delete',
168 description
=> "Call API DELETE on <api_path>.",
170 additionalProperties
=> 0,
171 properties
=> PVE
::APIClient
::RESTHandler
::add_standard_output_properties
($path_properties),
173 returns
=> { type
=> 'null' },
177 call_api_method
('DELETE', $param);
182 __PACKAGE__-
>register_method ({
183 name
=> 'pveclient_usage',
184 path
=> 'pveclient_usage',
186 description
=> "print API usage information for <api_path>.",
188 additionalProperties
=> 0,
190 api_path
=> $api_path_property,
192 description
=> "Verbose output format.",
197 description
=> "Including schema for returned data.",
202 description
=> "API command.",
204 enum
=> [ keys %$PVE::APIClient
::Helpers
::method_map
],
209 returns
=> { type
=> 'null' },
213 my $path = $param->{api_path
};
216 foreach my $cmd (qw(get set create delete)) {
217 next if $param->{command
} && $cmd ne $param->{command
};
218 my $method = $PVE::APIClient
::Helpers
::method_map-
>{$cmd};
220 my $info = PVE
::APIClient
::Helpers
::find_method_info
($path, $method, $uri_param, 1);
224 my $prefix = "pveclient api $cmd <remote> $path";
225 if ($param->{verbose
}) {
226 print PVE
::APIClient
::RESTHandler
::getopt_usage
(
227 $info, $prefix, undef, $uri_param, 'full');
230 print "USAGE: " . PVE
::APIClient
::RESTHandler
::getopt_usage
(
231 $info, $prefix, undef, $uri_param, 'short');
233 if ($param-> {returns
}) {
234 my $schema = to_json
($info->{returns
}, {utf8
=> 1, canonical
=> 1, pretty
=> 1 });
235 print "RETURNS: $schema\n";
240 if ($param->{command
}) {
241 die "no '$param->{command}' handler for '$path'\n";
243 die "no such resource '$path'\n"
251 config
=> $PVE::APIClient
::Commands
::config
::cmddef
,
252 list
=> $PVE::APIClient
::Commands
::list
::cmddef
,
253 lxc
=> $PVE::APIClient
::Commands
::lxc
::cmddef
,
254 remote
=> $PVE::APIClient
::Commands
::remote
::cmddef
,
256 resume
=> [ 'PVE::APIClient::Commands::GuestStatus', 'resume', ['remote', 'vmid']],
257 shutdown => [ 'PVE::APIClient::Commands::GuestStatus', 'shutdown', ['remote', 'vmid']],
258 spice
=> [ 'PVE::APIClient::Commands::GuestStatus', 'spice', ['remote', 'vmid']],
259 start
=> [ 'PVE::APIClient::Commands::GuestStatus', 'start', ['remote', 'vmid']],
260 stop
=> [ 'PVE::APIClient::Commands::GuestStatus', 'stop', ['remote', 'vmid']],
261 suspend
=> [ 'PVE::APIClient::Commands::GuestStatus', 'suspend', ['remote', 'vmid']],
264 usage
=> [ __PACKAGE__
, 'pveclient_usage', ['api_path']],
265 get
=> [ __PACKAGE__
, 'pveclient_get', ['remote', 'api_path']],
266 set
=> [ __PACKAGE__
, 'pveclient_set', ['remote', 'api_path']],
267 create
=> [ __PACKAGE__
, 'pveclient_create', ['remote', 'api_path']],
268 delete => [ __PACKAGE__
, 'pveclient_delete', ['remote', 'api_path']],
273 if ($cmd && $cmd eq 'printsynopsis') {
275 print __PACKAGE__-
>generate_asciidoc_synopsis();
280 __PACKAGE__-
>run_cli_handler();