]>
git.proxmox.com Git - pve-client.git/blob - pveclient
3 package PVE
::CLI
::pveclient
;
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
;
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
;
27 my ($method, $param, $options) = @_;
29 my $path = PVE
::APIClient
::Tools
::extract_param
($param, 'api_path');
30 die "missing API path\n" if !defined($path);
32 my $remote = PVE
::APIClient
::Tools
::extract_param
($param, 'remote');
33 die "missing remote\n" if !defined($remote);
35 my $config = PVE
::APIClient
::Config-
>load();
38 my $info = PVE
::APIClient
::Helpers
::find_method_info
($path, $method, $uri_param);
40 my $conn = PVE
::APIClient
::Config-
>remote_conn($config, $remote);
42 my $res = $conn->call($method, "api2/json/$path", $param);
43 die "undefined result" if !defined($res);
44 die "undefined result data" if !exists($res->{data
});
49 use base
qw(PVE::APIClient::CLIHandler);
52 return PVE
::APIClient
::PTY
::read_password
("Remote password: ")
58 if ($cmd && $cmd eq 'packagedepends') {
59 # experimental code to print required perl packages
61 my $dir = Cwd
::getcwd
;
63 foreach my $k (keys %INC) {
64 my $file = abs_path
($INC{$k});
65 next if $file =~ m/^\Q$dir\E/;
66 my $res = `dpkg -S '$file'`;
67 if ($res && $res =~ m/^(\S+): $file$/) {
68 my $debian_package = $1;
69 $debian_package =~ s/:amd64$//;
70 $packages->{$debian_package} = 1;
72 die "unable to find package for '$file'\n";
75 print join("\n", sort(keys %$packages)) . "\n";
80 my $path_properties = {};
81 my $path_returns = { type
=> 'null' };
83 my $api_path_property = {
84 description
=> "API path.",
87 my ($cmd, $pname, $cur, $args) = @_;
88 return PVE
::APIClient
::Helpers
::complete_api_path
($cur);
92 # dynamically update schema definition for direct API call
93 # like: pveclient api <get|set|create|delete|help> <remote> <path>
95 if (my $info = PVE
::APIClient
::Helpers
::extract_path_info
($uri_param)) {
96 foreach my $key (keys %{$info->{parameters
}->{properties
}}) {
97 next if defined($uri_param->{$key});
98 $path_properties->{$key} = $info->{parameters
}->{properties
}->{$key};
100 $path_returns = $info->{returns
};
103 $path_properties->{remote
} = get_standard_option
('pveclient-remote-name');
104 $path_properties->{api_path
} = $api_path_property;
106 my $format_result = sub {
107 my ($data, $schema, $options) = @_;
109 # NOTE; we need to use $path_returns instead of $schema
110 PVE
::APIClient
::CLIFormatter
::print_api_result
($data, $path_returns, undef, $options);
113 __PACKAGE__-
>register_method ({
114 name
=> 'pveclient_get',
115 path
=> 'pveclient_get',
117 description
=> "Call API GET on <api_path>.",
119 additionalProperties
=> 0,
120 properties
=> $path_properties,
122 returns
=> $path_returns,
124 my ($param, $options) = @_;
126 return call_api_method
('GET', $param, $options);
129 __PACKAGE__-
>register_method ({
130 name
=> 'pveclient_set',
131 path
=> 'pveclient_set',
133 description
=> "Call API PUT on <api_path>.",
135 additionalProperties
=> 0,
136 properties
=> $path_properties,
138 returns
=> $path_returns,
140 my ($param, $options) = @_;
142 return call_api_method
('PUT', $param, $options);
145 __PACKAGE__-
>register_method ({
146 name
=> 'pveclient_create',
147 path
=> 'pveclient_create',
149 description
=> "Call API POST on <api_path>.",
151 additionalProperties
=> 0,
152 properties
=> $path_properties,
154 returns
=> $path_returns,
156 my ($param, $options) = @_;
158 return call_api_method
('PUSH', $param, $options);
161 __PACKAGE__-
>register_method ({
162 name
=> 'pveclient_delete',
163 path
=> 'pveclient_delete',
165 description
=> "Call API DELETE on <api_path>.",
167 additionalProperties
=> 0,
168 properties
=> $path_properties,
170 returns
=> $path_returns,
172 my ($param, $options) = @_;
174 return call_api_method
('DELETE', $param, $options);
177 __PACKAGE__-
>register_method ({
178 name
=> 'pveclient_usage',
179 path
=> 'pveclient_usage',
181 description
=> "print API usage information for <api_path>.",
183 additionalProperties
=> 0,
185 api_path
=> $api_path_property,
187 description
=> "Verbose output format.",
192 description
=> "Including schema for returned data.",
197 description
=> "API command.",
199 enum
=> [ keys %$PVE::APIClient
::Helpers
::method_map
],
204 returns
=> { type
=> 'null' },
208 my $path = $param->{api_path
};
211 foreach my $cmd (qw(get set create delete)) {
212 next if $param->{command
} && $cmd ne $param->{command
};
213 my $method = $PVE::APIClient
::Helpers
::method_map-
>{$cmd};
215 my $info = PVE
::APIClient
::Helpers
::find_method_info
($path, $method, $uri_param, 1);
219 my $prefix = "pveclient api $cmd <remote> $path";
220 if ($param->{verbose
}) {
221 print PVE
::APIClient
::RESTHandler
::getopt_usage
(
222 $info, $prefix, undef, $uri_param, 'full');
225 print "USAGE: " . PVE
::APIClient
::RESTHandler
::getopt_usage
(
226 $info, $prefix, undef, $uri_param, 'short');
228 if ($param-> {returns
}) {
229 my $schema = to_json
($info->{returns
}, {utf8
=> 1, canonical
=> 1, pretty
=> 1 });
230 print "RETURNS: $schema\n";
235 if ($param->{command
}) {
236 die "no '$param->{command}' handler for '$path'\n";
238 die "no such resource '$path'\n"
246 config
=> $PVE::APIClient
::Commands
::config
::cmddef
,
247 list
=> $PVE::APIClient
::Commands
::list
::cmddef
,
248 lxc
=> $PVE::APIClient
::Commands
::lxc
::cmddef
,
249 remote
=> $PVE::APIClient
::Commands
::remote
::cmddef
,
251 resume
=> [ 'PVE::APIClient::Commands::GuestStatus', 'resume', ['remote', 'vmid']],
252 shutdown => [ 'PVE::APIClient::Commands::GuestStatus', 'shutdown', ['remote', 'vmid']],
253 spice
=> [ 'PVE::APIClient::Commands::GuestStatus', 'spice', ['remote', 'vmid']],
254 start
=> [ 'PVE::APIClient::Commands::GuestStatus', 'start', ['remote', 'vmid']],
255 stop
=> [ 'PVE::APIClient::Commands::GuestStatus', 'stop', ['remote', 'vmid']],
256 suspend
=> [ 'PVE::APIClient::Commands::GuestStatus', 'suspend', ['remote', 'vmid']],
259 usage
=> [ __PACKAGE__
, 'pveclient_usage', ['api_path']],
260 get
=> [ __PACKAGE__
, 'pveclient_get', ['remote', 'api_path'], {}, $format_result ],
261 set
=> [ __PACKAGE__
, 'pveclient_set', ['remote', 'api_path'], {}, $format_result ],
262 create
=> [ __PACKAGE__
, 'pveclient_create', ['remote', 'api_path'], {}, $format_result ],
263 delete => [ __PACKAGE__
, 'pveclient_delete', ['remote', 'api_path'], {}, $format_result ],
268 if ($cmd && $cmd eq 'printsynopsis') {
270 print __PACKAGE__-
>generate_asciidoc_synopsis();
275 __PACKAGE__-
>run_cli_handler();