]>
git.proxmox.com Git - pve-client.git/blob - pveclient
a7e41efea3f740743d09d5741d64085e62c2ae3d
3 package PVE
::CLI
::pveclient
;
10 use PVE
::APIClient
::JSONSchema
qw(register_standard_option get_standard_option);
11 use PVE
::APIClient
::CLIHandler
;
13 use PVE
::APIClient
::LWP
;
14 use PVE
::APIClient
::Helpers
;
15 use PVE
::APIClient
::Config
;
16 use PVE
::APIClient
::Commands
::config
;
17 use PVE
::APIClient
::Commands
::remote
;
18 use PVE
::APIClient
::Commands
::list
;
19 use PVE
::APIClient
::Commands
::lxc
;
20 use PVE
::APIClient
::Commands
::GuestStatus
;
25 my ($remote, $path, $method, $param) = @_;
27 die "missing API path\n" if !defined($path);
29 my $config = PVE
::APIClient
::Config-
>load();
31 # test if api path exists
32 my $info = PVE
::APIClient
::Helpers
::lookup_api_method
($path, $method);
34 my $conn = PVE
::APIClient
::Config-
>remote_conn($config, $remote);
36 my $res = $conn->call($method, "api2/json/$path", $param);
37 die "undefined result" if !defined($res);
38 die "undefined result data" if !exists($res->{data
});
43 use base
qw(PVE::APIClient::CLIHandler);
47 if ($cmd && $cmd eq 'packagedepends') {
48 # experimental code to print required perl packages
50 my $dir = Cwd
::getcwd
;
52 foreach my $k (keys %INC) {
53 my $file = abs_path
($INC{$k});
54 next if $file =~ m/^\Q$dir\E/;
55 my $res = `dpkg -S '$file'`;
56 if ($res && $res =~ m/^(\S+): $file$/) {
57 my $debian_package = $1;
58 $debian_package =~ s/:amd64$//;
59 $packages->{$debian_package} = 1;
61 die "unable to find package for '$file'\n";
64 print join("\n", sort(keys %$packages)) . "\n";
69 my $path_properties = {};
70 my $path_returns = { type
=> 'null' };
72 # dynamically update schema definition for direct API call
73 # like: pveclient api <get|set|create|delete|help> <remote> <path>
74 if (my $info = PVE
::APIClient
::Helpers
::extract_path_info
()) {
75 $path_properties = $info->{parameters
}->{properties
};
76 $path_returns = $info->{returns
};
79 $path_properties->{remote
} = get_standard_option
('pveclient-remote-name');
80 $path_properties->{api_path
} = {
81 description
=> "API path.",
84 my ($cmd, $pname, $cur, $args) = @_;
85 return PVE
::APIClient
::Helpers
::complete_api_path
($cur);
89 my $format_result = sub {
90 my ($data, $format) = @_;
92 return if $path_returns->{type
} eq 'null';
94 # TODO: implement different output formats ($format)
95 print to_json
($data, {utf8
=> 1, allow_nonref
=> 1, canonical
=> 1, pretty
=> 1 });
98 __PACKAGE__-
>register_method ({
99 name
=> 'pveclient_get',
100 path
=> 'pveclient_get',
102 description
=> "call API GET on <path>.",
104 additionalProperties
=> 0,
105 properties
=> $path_properties,
107 returns
=> $path_returns,
111 my $path = PVE
::Tools
::extract_param
($param, 'api_path');
112 my $remote = PVE
::Tools
::extract_param
($param, 'remote');
114 return call_method
($remote, $path, 'GET', $param);
117 __PACKAGE__-
>register_method ({
118 name
=> 'pveclient_set',
119 path
=> 'pveclient_set',
121 description
=> "call API PUT on <path>.",
123 additionalProperties
=> 0,
124 properties
=> $path_properties,
126 returns
=> $path_returns,
130 print Dumper
($param);
136 __PACKAGE__-
>register_method ({
137 name
=> 'pveclient_create',
138 path
=> 'pveclient_create',
140 description
=> "call API PUSH on <path>.",
142 additionalProperties
=> 0,
143 properties
=> $path_properties,
145 returns
=> $path_returns,
149 print Dumper
($param);
155 __PACKAGE__-
>register_method ({
156 name
=> 'pveclient_delete',
157 path
=> 'pveclient_delete',
159 description
=> "call API DELETE on <path>.",
161 additionalProperties
=> 0,
162 properties
=> $path_properties,
164 returns
=> $path_returns,
168 print Dumper
($param);
176 config
=> $PVE::APIClient
::Commands
::config
::cmddef
,
177 list
=> $PVE::APIClient
::Commands
::list
::cmddef
,
178 lxc
=> $PVE::APIClient
::Commands
::lxc
::cmddef
,
179 remote
=> $PVE::APIClient
::Commands
::remote
::cmddef
,
181 spice
=> [ 'PVE::APIClient::Commands::GuestStatus', 'spice', ['remote', 'vmid']],
182 start
=> [ 'PVE::APIClient::Commands::GuestStatus', 'start', ['remote', 'vmid']],
183 stop
=> [ 'PVE::APIClient::Commands::GuestStatus', 'stop', ['remote', 'vmid']],
186 get
=> [ __PACKAGE__
, 'pveclient_get', ['remote', 'api_path'], {}, $format_result ],
187 set
=> [ __PACKAGE__
, 'pveclient_set', ['remote', 'api_path'], {}, $format_result ],
188 create
=> [ __PACKAGE__
, 'pveclient_create', ['remote', 'api_path'], {}, $format_result ],
189 delete => [ __PACKAGE__
, 'pveclient_delete', ['remote', 'api_path'], {}, $format_result ],
194 __PACKAGE__-
>run_cli_handler();