]> git.proxmox.com Git - pve-client.git/blame - pveclient
fix copyright date
[pve-client.git] / pveclient
CommitLineData
29505e2c
DM
1#!/usr/bin/perl
2
b133a905
DM
3package PVE::CLI::pveclient;
4
29505e2c
DM
5use strict;
6use warnings;
f5fcd826 7use Cwd 'abs_path';
29505e2c
DM
8use Data::Dumper;
9
c9138c03 10use PVE::APIClient::JSONSchema qw(register_standard_option get_standard_option);
4aba8d31 11use PVE::APIClient::RESTHandler;
5101e472 12use PVE::APIClient::CLIFormatter;
c9138c03 13use PVE::APIClient::CLIHandler;
4a7e63b8 14use PVE::APIClient::PTY;
565bbc73 15
29505e2c
DM
16use PVE::APIClient::LWP;
17use PVE::APIClient::Helpers;
b0495d82 18use PVE::APIClient::Config;
a6dab5b8 19use PVE::APIClient::Commands::config;
565bbc73 20use PVE::APIClient::Commands::remote;
5b090843 21use PVE::APIClient::Commands::list;
adf285bf 22use PVE::APIClient::Commands::lxc;
89335fb1 23use PVE::APIClient::Commands::GuestStatus;
565bbc73 24
29505e2c
DM
25use JSON;
26
61ad3df5 27sub call_api_method {
4aba8d31 28 my ($method, $param) = @_;
29505e2c 29
61ad3df5 30 my $path = PVE::APIClient::Tools::extract_param($param, 'api_path');
29505e2c
DM
31 die "missing API path\n" if !defined($path);
32
4aba8d31
DM
33 my $stdopts = PVE::APIClient::RESTHandler::extract_standard_output_properties($param);
34 PVE::APIClient::CLIFormatter::query_terminal_options($stdopts);
35
61ad3df5
DM
36 my $remote = PVE::APIClient::Tools::extract_param($param, 'remote');
37 die "missing remote\n" if !defined($remote);
38
b0495d82
DM
39 my $config = PVE::APIClient::Config->load();
40
520f543e
DM
41 my $uri_param = {};
42 my $info = PVE::APIClient::Helpers::find_method_info($path, $method, $uri_param);
29505e2c 43
b0495d82
DM
44 my $conn = PVE::APIClient::Config->remote_conn($config, $remote);
45
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});
49
4aba8d31
DM
50 my $data = $res->{data};
51
52 PVE::APIClient::CLIFormatter::print_api_result($data, $info->{returns}, undef, $stdopts);
29505e2c
DM
53}
54
c9138c03 55use base qw(PVE::APIClient::CLIHandler);
3a1bc22f 56
4a7e63b8 57sub read_password {
4fbacba6 58 return PVE::APIClient::PTY::read_password("Remote password: ")
4a7e63b8
DM
59}
60
61
b133a905 62my $cmd = $ARGV[0];
dce6ecbc 63
b133a905 64if ($cmd && $cmd eq 'packagedepends') {
f5fcd826
DM
65 # experimental code to print required perl packages
66 my $packages = {};
67 my $dir = Cwd::getcwd;
68
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;
77 } else {
78 die "unable to find package for '$file'\n";
79 }
80 }
81 print join("\n", sort(keys %$packages)) . "\n";
b133a905
DM
82
83 exit(0);
84}
85
86my $path_properties = {};
b133a905 87
55d17e7e
DM
88my $api_path_property = {
89 description => "API path.",
90 type => 'string',
91 completion => sub {
92 my ($cmd, $pname, $cur, $args) = @_;
93 return PVE::APIClient::Helpers::complete_api_path($cur);
94 },
95};
96
b133a905
DM
97# dynamically update schema definition for direct API call
98# like: pveclient api <get|set|create|delete|help> <remote> <path>
520f543e
DM
99my $uri_param = {};
100if (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};
104 }
29505e2c
DM
105}
106
b133a905 107$path_properties->{remote} = get_standard_option('pveclient-remote-name');
55d17e7e 108$path_properties->{api_path} = $api_path_property;
61ad3df5 109
b133a905
DM
110__PACKAGE__->register_method ({
111 name => 'pveclient_get',
112 path => 'pveclient_get',
113 method => 'GET',
4fbacba6 114 description => "Call API GET on <api_path>.",
b133a905
DM
115 parameters => {
116 additionalProperties => 0,
4aba8d31 117 properties => PVE::APIClient::RESTHandler::add_standard_output_properties($path_properties),
b133a905 118 },
4aba8d31 119 returns => { type => 'null' },
b133a905 120 code => sub {
4aba8d31
DM
121 my ($param) = @_;
122
123 call_api_method('GET', $param);
b133a905 124
4aba8d31 125 return undef;
b133a905
DM
126 }});
127
128__PACKAGE__->register_method ({
129 name => 'pveclient_set',
130 path => 'pveclient_set',
131 method => 'PUT',
4fbacba6 132 description => "Call API PUT on <api_path>.",
b133a905
DM
133 parameters => {
134 additionalProperties => 0,
4aba8d31 135 properties => PVE::APIClient::RESTHandler::add_standard_output_properties($path_properties),
b133a905 136 },
4aba8d31 137 returns => { type => 'null' },
b133a905 138 code => sub {
4aba8d31 139 my ($param) = @_;
b133a905 140
4aba8d31
DM
141 call_api_method('PUT', $param);
142
143 return undef;
b133a905
DM
144 }});
145
146__PACKAGE__->register_method ({
147 name => 'pveclient_create',
148 path => 'pveclient_create',
faa47c20
DM
149 method => 'POST',
150 description => "Call API POST on <api_path>.",
b133a905
DM
151 parameters => {
152 additionalProperties => 0,
4aba8d31 153 properties => PVE::APIClient::RESTHandler::add_standard_output_properties($path_properties),
b133a905 154 },
4aba8d31 155 returns => { type => 'null' },
b133a905 156 code => sub {
4aba8d31
DM
157 my ($param) = @_;
158
159 call_api_method('PUSH', $param);
b133a905 160
4aba8d31 161 return undef;
b133a905
DM
162 }});
163
164__PACKAGE__->register_method ({
165 name => 'pveclient_delete',
166 path => 'pveclient_delete',
167 method => 'DELETE',
4fbacba6 168 description => "Call API DELETE on <api_path>.",
b133a905
DM
169 parameters => {
170 additionalProperties => 0,
4aba8d31 171 properties => PVE::APIClient::RESTHandler::add_standard_output_properties($path_properties),
b133a905 172 },
4aba8d31 173 returns => { type => 'null' },
b133a905 174 code => sub {
4aba8d31 175 my ($param) = @_;
b133a905 176
4aba8d31
DM
177 call_api_method('DELETE', $param);
178
179 return undef;
b133a905
DM
180 }});
181
55d17e7e
DM
182__PACKAGE__->register_method ({
183 name => 'pveclient_usage',
184 path => 'pveclient_usage',
185 method => 'GET',
186 description => "print API usage information for <api_path>.",
187 parameters => {
188 additionalProperties => 0,
189 properties => {
190 api_path => $api_path_property,
191 verbose => {
192 description => "Verbose output format.",
193 type => 'boolean',
194 optional => 1,
195 },
196 returns => {
197 description => "Including schema for returned data.",
198 type => 'boolean',
199 optional => 1,
200 },
201 command => {
202 description => "API command.",
203 type => 'string',
204 enum => [ keys %$PVE::APIClient::Helpers::method_map ],
205 optional => 1,
206 },
207 },
208 },
209 returns => { type => 'null' },
210 code => sub {
211 my ($param) = @_;
212
213 my $path = $param->{api_path};
214
215 my $found = 0;
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};
219 my $uri_param = {};
220 my $info = PVE::APIClient::Helpers::find_method_info($path, $method, $uri_param, 1);
221 next if !$info;
222 $found = 1;
223
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');
228
229 } else {
230 print "USAGE: " . PVE::APIClient::RESTHandler::getopt_usage(
231 $info, $prefix, undef, $uri_param, 'short');
232 }
233 if ($param-> {returns}) {
234 my $schema = to_json($info->{returns}, {utf8 => 1, canonical => 1, pretty => 1 });
235 print "RETURNS: $schema\n";
236 }
237 }
238
239 if (!$found) {
240 if ($param->{command}) {
241 die "no '$param->{command}' handler for '$path'\n";
242 } else {
243 die "no such resource '$path'\n"
244 }
245 }
246
247 return undef;
248 }});
b133a905
DM
249
250our $cmddef = {
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,
4cedebf6 255
42de9764
RJ
256 resume => [ 'PVE::APIClient::Commands::GuestStatus', 'resume', ['remote', 'vmid']],
257 shutdown => [ 'PVE::APIClient::Commands::GuestStatus', 'shutdown', ['remote', 'vmid']],
4cedebf6 258 spice => [ 'PVE::APIClient::Commands::GuestStatus', 'spice', ['remote', 'vmid']],
89335fb1
DM
259 start => [ 'PVE::APIClient::Commands::GuestStatus', 'start', ['remote', 'vmid']],
260 stop => [ 'PVE::APIClient::Commands::GuestStatus', 'stop', ['remote', 'vmid']],
42de9764 261 suspend => [ 'PVE::APIClient::Commands::GuestStatus', 'suspend', ['remote', 'vmid']],
b133a905
DM
262
263 api => {
55d17e7e 264 usage => [ __PACKAGE__, 'pveclient_usage', ['api_path']],
4aba8d31
DM
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']],
b0495d82 269 },
b133a905
DM
270};
271
272
2767c2b9
DM
273if ($cmd && $cmd eq 'printsynopsis') {
274
275 print __PACKAGE__->generate_asciidoc_synopsis();
276
277 exit(0);
278}
279
b133a905 280__PACKAGE__->run_cli_handler();