]> git.proxmox.com Git - pve-client.git/blob - pveclient
f78770b5d4ce5f50bdf60622e8992ef057356b18
[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::CLIHandler;
12
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;
21
22 use JSON;
23
24 sub call_api_method {
25 my ($method, $param) = @_;
26
27 my $path = PVE::APIClient::Tools::extract_param($param, 'api_path');
28 die "missing API path\n" if !defined($path);
29
30 my $remote = PVE::APIClient::Tools::extract_param($param, 'remote');
31 die "missing remote\n" if !defined($remote);
32
33 my $format = PVE::APIClient::Tools::extract_param($param, 'format');
34 PVE::APIClient::Helpers::set_output_format($format);
35
36 my $config = PVE::APIClient::Config->load();
37
38 # test if api path exists
39 my $info = PVE::APIClient::Helpers::lookup_api_method($path, $method);
40
41 my $conn = PVE::APIClient::Config->remote_conn($config, $remote);
42
43 my $res = $conn->call($method, "api2/json/$path", $param);
44 die "undefined result" if !defined($res);
45 die "undefined result data" if !exists($res->{data});
46
47 return $res->{data};
48 }
49
50 use base qw(PVE::APIClient::CLIHandler);
51
52 my $cmd = $ARGV[0];
53
54 if ($cmd && $cmd eq 'packagedepends') {
55 # experimental code to print required perl packages
56 my $packages = {};
57 my $dir = Cwd::getcwd;
58
59 foreach my $k (keys %INC) {
60 my $file = abs_path($INC{$k});
61 next if $file =~ m/^\Q$dir\E/;
62 my $res = `dpkg -S '$file'`;
63 if ($res && $res =~ m/^(\S+): $file$/) {
64 my $debian_package = $1;
65 $debian_package =~ s/:amd64$//;
66 $packages->{$debian_package} = 1;
67 } else {
68 die "unable to find package for '$file'\n";
69 }
70 }
71 print join("\n", sort(keys %$packages)) . "\n";
72
73 exit(0);
74 }
75
76 my $path_properties = {};
77 my $path_returns = { type => 'null' };
78
79 # dynamically update schema definition for direct API call
80 # like: pveclient api <get|set|create|delete|help> <remote> <path>
81 if (my $info = PVE::APIClient::Helpers::extract_path_info()) {
82 $path_properties = $info->{parameters}->{properties};
83 $path_returns = $info->{returns};
84 }
85
86 $path_properties->{format} = get_standard_option('pveclient-output-format'),
87 $path_properties->{remote} = get_standard_option('pveclient-remote-name');
88 $path_properties->{api_path} = {
89 description => "API path.",
90 type => 'string',
91 completion => sub {
92 my ($cmd, $pname, $cur, $args) = @_;
93 return PVE::APIClient::Helpers::complete_api_path($cur);
94 },
95 };
96
97
98 my $format_result = sub {
99 my ($data) = @_;
100
101 PVE::APIClient::Helpers::print_result($data, $path_returns);
102 };
103
104 __PACKAGE__->register_method ({
105 name => 'pveclient_get',
106 path => 'pveclient_get',
107 method => 'GET',
108 description => "call API GET on <path>.",
109 parameters => {
110 additionalProperties => 0,
111 properties => $path_properties,
112 },
113 returns => $path_returns,
114 code => sub {
115 my ($param) = @_;
116
117 return call_api_method('GET', $param);
118 }});
119
120 __PACKAGE__->register_method ({
121 name => 'pveclient_set',
122 path => 'pveclient_set',
123 method => 'PUT',
124 description => "call API PUT on <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('PUT', $param);
134 }});
135
136 __PACKAGE__->register_method ({
137 name => 'pveclient_create',
138 path => 'pveclient_create',
139 method => 'PUSH',
140 description => "call API PUSH on <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('PUSH', $param);
150 }});
151
152 __PACKAGE__->register_method ({
153 name => 'pveclient_delete',
154 path => 'pveclient_delete',
155 method => 'DELETE',
156 description => "call API DELETE on <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('DELETE', $param);
166 }});
167
168
169 our $cmddef = {
170 config => $PVE::APIClient::Commands::config::cmddef,
171 list => $PVE::APIClient::Commands::list::cmddef,
172 lxc => $PVE::APIClient::Commands::lxc::cmddef,
173 remote => $PVE::APIClient::Commands::remote::cmddef,
174
175 spice => [ 'PVE::APIClient::Commands::GuestStatus', 'spice', ['remote', 'vmid']],
176 start => [ 'PVE::APIClient::Commands::GuestStatus', 'start', ['remote', 'vmid']],
177 stop => [ 'PVE::APIClient::Commands::GuestStatus', 'stop', ['remote', 'vmid']],
178
179 api => {
180 get => [ __PACKAGE__, 'pveclient_get', ['remote', 'api_path'], {}, $format_result ],
181 set => [ __PACKAGE__, 'pveclient_set', ['remote', 'api_path'], {}, $format_result ],
182 create => [ __PACKAGE__, 'pveclient_create', ['remote', 'api_path'], {}, $format_result ],
183 delete => [ __PACKAGE__, 'pveclient_delete', ['remote', 'api_path'], {}, $format_result ],
184 },
185 };
186
187
188 __PACKAGE__->run_cli_handler();