]>
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 # dynamically update schema definition for direct API call
87 # like: pveclient api <get|set|create|delete|help> <remote> <path>
89 if (my $info = PVE
::APIClient
::Helpers
::extract_path_info
($uri_param)) {
90 foreach my $key (keys %{$info->{parameters
}->{properties
}}) {
91 next if defined($uri_param->{$key});
92 $path_properties->{$key} = $info->{parameters
}->{properties
}->{$key};
94 $path_returns = $info->{returns
};
97 $path_properties->{format
} = get_standard_option
('pve-output-format'),
98 $path_properties->{remote
} = get_standard_option
('pveclient-remote-name');
99 $path_properties->{api_path
} = {
100 description
=> "API path.",
103 my ($cmd, $pname, $cur, $args) = @_;
104 return PVE
::APIClient
::Helpers
::complete_api_path
($cur);
109 my $format_result = sub {
112 my $format = PVE
::APIClient
::Helpers
::get_output_format
();
114 my $options = PVE
::APIClient
::CLIFormatter
::query_terminal_options
({});
116 PVE
::APIClient
::CLIFormatter
::print_api_result
($format, $data, $path_returns, undef, $options);
119 __PACKAGE__-
>register_method ({
120 name
=> 'pveclient_get',
121 path
=> 'pveclient_get',
123 description
=> "Call API GET on <api_path>.",
125 additionalProperties
=> 0,
126 properties
=> $path_properties,
128 returns
=> $path_returns,
132 return call_api_method
('GET', $param);
135 __PACKAGE__-
>register_method ({
136 name
=> 'pveclient_set',
137 path
=> 'pveclient_set',
139 description
=> "Call API PUT on <api_path>.",
141 additionalProperties
=> 0,
142 properties
=> $path_properties,
144 returns
=> $path_returns,
148 return call_api_method
('PUT', $param);
151 __PACKAGE__-
>register_method ({
152 name
=> 'pveclient_create',
153 path
=> 'pveclient_create',
155 description
=> "Call API POST on <api_path>.",
157 additionalProperties
=> 0,
158 properties
=> $path_properties,
160 returns
=> $path_returns,
164 return call_api_method
('PUSH', $param);
167 __PACKAGE__-
>register_method ({
168 name
=> 'pveclient_delete',
169 path
=> 'pveclient_delete',
171 description
=> "Call API DELETE on <api_path>.",
173 additionalProperties
=> 0,
174 properties
=> $path_properties,
176 returns
=> $path_returns,
180 return call_api_method
('DELETE', $param);
185 config
=> $PVE::APIClient
::Commands
::config
::cmddef
,
186 list
=> $PVE::APIClient
::Commands
::list
::cmddef
,
187 lxc
=> $PVE::APIClient
::Commands
::lxc
::cmddef
,
188 remote
=> $PVE::APIClient
::Commands
::remote
::cmddef
,
190 resume
=> [ 'PVE::APIClient::Commands::GuestStatus', 'resume', ['remote', 'vmid']],
191 shutdown => [ 'PVE::APIClient::Commands::GuestStatus', 'shutdown', ['remote', 'vmid']],
192 spice
=> [ 'PVE::APIClient::Commands::GuestStatus', 'spice', ['remote', 'vmid']],
193 start
=> [ 'PVE::APIClient::Commands::GuestStatus', 'start', ['remote', 'vmid']],
194 stop
=> [ 'PVE::APIClient::Commands::GuestStatus', 'stop', ['remote', 'vmid']],
195 suspend
=> [ 'PVE::APIClient::Commands::GuestStatus', 'suspend', ['remote', 'vmid']],
198 get
=> [ __PACKAGE__
, 'pveclient_get', ['remote', 'api_path'], {}, $format_result ],
199 set
=> [ __PACKAGE__
, 'pveclient_set', ['remote', 'api_path'], {}, $format_result ],
200 create
=> [ __PACKAGE__
, 'pveclient_create', ['remote', 'api_path'], {}, $format_result ],
201 delete => [ __PACKAGE__
, 'pveclient_delete', ['remote', 'api_path'], {}, $format_result ],
206 if ($cmd && $cmd eq 'printsynopsis') {
208 print __PACKAGE__-
>generate_asciidoc_synopsis();
213 __PACKAGE__-
>run_cli_handler();