]>
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) = @_;
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 $format = PVE
::APIClient
::Tools
::extract_param
($param, 'format');
36 PVE
::APIClient
::Helpers
::set_output_format
($format);
38 my $config = PVE
::APIClient
::Config-
>load();
41 my $info = PVE
::APIClient
::Helpers
::find_method_info
($path, $method, $uri_param);
43 my $conn = PVE
::APIClient
::Config-
>remote_conn($config, $remote);
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
});
52 use base
qw(PVE::APIClient::CLIHandler);
55 return PVE
::APIClient
::PTY
::read_password
("Remote password: ")
61 if ($cmd && $cmd eq 'packagedepends') {
62 # experimental code to print required perl packages
64 my $dir = Cwd
::getcwd
;
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;
75 die "unable to find package for '$file'\n";
78 print join("\n", sort(keys %$packages)) . "\n";
83 my $path_properties = {};
84 my $path_returns = { type
=> 'null' };
86 my $api_path_property = {
87 description
=> "API path.",
90 my ($cmd, $pname, $cur, $args) = @_;
91 return PVE
::APIClient
::Helpers
::complete_api_path
($cur);
95 # dynamically update schema definition for direct API call
96 # like: pveclient api <get|set|create|delete|help> <remote> <path>
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};
103 $path_returns = $info->{returns
};
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;
110 my $format_result = sub {
113 my $format = PVE
::APIClient
::Helpers
::get_output_format
();
115 my $options = PVE
::APIClient
::CLIFormatter
::query_terminal_options
({});
117 PVE
::APIClient
::CLIFormatter
::print_api_result
($format, $data, $path_returns, undef, $options);
120 __PACKAGE__-
>register_method ({
121 name
=> 'pveclient_get',
122 path
=> 'pveclient_get',
124 description
=> "Call API GET on <api_path>.",
126 additionalProperties
=> 0,
127 properties
=> $path_properties,
129 returns
=> $path_returns,
133 return call_api_method
('GET', $param);
136 __PACKAGE__-
>register_method ({
137 name
=> 'pveclient_set',
138 path
=> 'pveclient_set',
140 description
=> "Call API PUT on <api_path>.",
142 additionalProperties
=> 0,
143 properties
=> $path_properties,
145 returns
=> $path_returns,
149 return call_api_method
('PUT', $param);
152 __PACKAGE__-
>register_method ({
153 name
=> 'pveclient_create',
154 path
=> 'pveclient_create',
156 description
=> "Call API POST on <api_path>.",
158 additionalProperties
=> 0,
159 properties
=> $path_properties,
161 returns
=> $path_returns,
165 return call_api_method
('PUSH', $param);
168 __PACKAGE__-
>register_method ({
169 name
=> 'pveclient_delete',
170 path
=> 'pveclient_delete',
172 description
=> "Call API DELETE on <api_path>.",
174 additionalProperties
=> 0,
175 properties
=> $path_properties,
177 returns
=> $path_returns,
181 return call_api_method
('DELETE', $param);
184 __PACKAGE__-
>register_method ({
185 name
=> 'pveclient_usage',
186 path
=> 'pveclient_usage',
188 description
=> "print API usage information for <api_path>.",
190 additionalProperties
=> 0,
192 api_path
=> $api_path_property,
194 description
=> "Verbose output format.",
199 description
=> "Including schema for returned data.",
204 description
=> "API command.",
206 enum
=> [ keys %$PVE::APIClient
::Helpers
::method_map
],
211 returns
=> { type
=> 'null' },
215 my $path = $param->{api_path
};
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};
222 my $info = PVE
::APIClient
::Helpers
::find_method_info
($path, $method, $uri_param, 1);
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');
232 print "USAGE: " . PVE
::APIClient
::RESTHandler
::getopt_usage
(
233 $info, $prefix, undef, $uri_param, 'short');
235 if ($param-> {returns
}) {
236 my $schema = to_json
($info->{returns
}, {utf8
=> 1, canonical
=> 1, pretty
=> 1 });
237 print "RETURNS: $schema\n";
242 if ($param->{command
}) {
243 die "no '$param->{command}' handler for '$path'\n";
245 die "no such resource '$path'\n"
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
,
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']],
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 ],
275 if ($cmd && $cmd eq 'printsynopsis') {
277 print __PACKAGE__-
>generate_asciidoc_synopsis();
282 __PACKAGE__-
>run_cli_handler();