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