]>
Commit | Line | Data |
---|---|---|
29505e2c DM |
1 | #!/usr/bin/perl |
2 | ||
b133a905 DM |
3 | package PVE::CLI::pveclient; |
4 | ||
29505e2c DM |
5 | use strict; |
6 | use warnings; | |
f5fcd826 | 7 | use Cwd 'abs_path'; |
565bbc73 | 8 | use lib '/usr/share/pve-client'; |
c5111270 | 9 | use lib '.'; |
29505e2c DM |
10 | use Data::Dumper; |
11 | ||
b133a905 | 12 | use PVE::JSONSchema qw(register_standard_option get_standard_option); |
565bbc73 DM |
13 | use PVE::CLIHandler; |
14 | ||
29505e2c DM |
15 | use PVE::APIClient::LWP; |
16 | use PVE::APIClient::Helpers; | |
a6dab5b8 | 17 | use PVE::APIClient::Commands::config; |
565bbc73 | 18 | use PVE::APIClient::Commands::remote; |
5b090843 | 19 | use PVE::APIClient::Commands::list; |
adf285bf | 20 | use PVE::APIClient::Commands::lxc; |
63c02d8c | 21 | use PVE::APIClient::Commands::help; |
2f964a75 | 22 | use PVE::APIClient::Commands::start; |
565bbc73 | 23 | |
29505e2c DM |
24 | use JSON; |
25 | ||
29505e2c | 26 | sub call_method { |
b133a905 | 27 | my ($remote, $path, $method, $params) = @_; |
29505e2c DM |
28 | |
29 | die "missing API path\n" if !defined($path); | |
30 | ||
31 | my $info = PVE::APIClient::Helpers::lookup_api_method($path, $method); | |
b133a905 | 32 | print Dumper($params); |
29505e2c DM |
33 | |
34 | die "implement me"; | |
35 | } | |
36 | ||
b133a905 | 37 | use base qw(PVE::CLIHandler); |
3a1bc22f | 38 | |
b133a905 | 39 | my $cmd = $ARGV[0]; |
dce6ecbc | 40 | |
b133a905 | 41 | if ($cmd && $cmd eq 'packagedepends') { |
f5fcd826 DM |
42 | # experimental code to print required perl packages |
43 | my $packages = {}; | |
44 | my $dir = Cwd::getcwd; | |
45 | ||
46 | foreach my $k (keys %INC) { | |
47 | my $file = abs_path($INC{$k}); | |
48 | next if $file =~ m/^\Q$dir\E/; | |
49 | my $res = `dpkg -S '$file'`; | |
50 | if ($res && $res =~ m/^(\S+): $file$/) { | |
51 | my $debian_package = $1; | |
52 | $debian_package =~ s/:amd64$//; | |
53 | $packages->{$debian_package} = 1; | |
54 | } else { | |
55 | die "unable to find package for '$file'\n"; | |
56 | } | |
57 | } | |
58 | print join("\n", sort(keys %$packages)) . "\n"; | |
b133a905 DM |
59 | |
60 | exit(0); | |
61 | } | |
62 | ||
63 | my $path_properties = {}; | |
64 | my $path_returns = { type => 'null' }; | |
65 | ||
66 | # dynamically update schema definition for direct API call | |
67 | # like: pveclient api <get|set|create|delete|help> <remote> <path> | |
68 | if (my $info = PVE::APIClient::Helpers::extract_path_info()) { | |
69 | $path_properties = $info->{parameters}->{properties}; | |
70 | $path_returns = $info->{returns}; | |
29505e2c DM |
71 | } |
72 | ||
b133a905 DM |
73 | $path_properties->{remote} = get_standard_option('pveclient-remote-name'); |
74 | $path_properties->{api_path} = { | |
75 | description => "API path.", | |
76 | type => 'string', | |
77 | completion => sub { | |
78 | my ($cmd, $pname, $cur, $args) = @_; | |
79 | return PVE::APIClient::Helpers::complete_api_path($cur); | |
80 | }, | |
81 | }; | |
82 | ||
83 | __PACKAGE__->register_method ({ | |
84 | name => 'pveclient_get', | |
85 | path => 'pveclient_get', | |
86 | method => 'GET', | |
87 | description => "call API GET on <path>.", | |
88 | parameters => { | |
89 | additionalProperties => 0, | |
90 | properties => $path_properties, | |
91 | }, | |
92 | returns => $path_returns, | |
93 | code => sub { | |
94 | my ($param) = @_; | |
95 | ||
96 | my $path = PVE::Tools::extract_param($param, 'api_path'); | |
97 | my $remote = PVE::Tools::extract_param($param, 'remote'); | |
98 | ||
99 | return call_method($remote, $path, 'GET', $param); | |
100 | }}); | |
101 | ||
102 | __PACKAGE__->register_method ({ | |
103 | name => 'pveclient_set', | |
104 | path => 'pveclient_set', | |
105 | method => 'PUT', | |
106 | description => "call API PUT on <path>.", | |
107 | parameters => { | |
108 | additionalProperties => 0, | |
109 | properties => $path_properties, | |
110 | }, | |
111 | returns => $path_returns, | |
112 | code => sub { | |
113 | my ($param) = @_; | |
114 | ||
115 | print Dumper($param); | |
116 | ||
117 | die "implement me"; | |
118 | ||
119 | }}); | |
120 | ||
121 | __PACKAGE__->register_method ({ | |
122 | name => 'pveclient_create', | |
123 | path => 'pveclient_create', | |
124 | method => 'PUSH', | |
125 | description => "call API PUSH on <path>.", | |
126 | parameters => { | |
127 | additionalProperties => 0, | |
128 | properties => $path_properties, | |
129 | }, | |
130 | returns => $path_returns, | |
131 | code => sub { | |
132 | my ($param) = @_; | |
133 | ||
134 | print Dumper($param); | |
135 | ||
136 | die "implement me"; | |
137 | ||
138 | }}); | |
139 | ||
140 | __PACKAGE__->register_method ({ | |
141 | name => 'pveclient_delete', | |
142 | path => 'pveclient_delete', | |
143 | method => 'DELETE', | |
144 | description => "call API DELETE on <path>.", | |
145 | parameters => { | |
146 | additionalProperties => 0, | |
147 | properties => $path_properties, | |
148 | }, | |
149 | returns => $path_returns, | |
150 | code => sub { | |
151 | my ($param) = @_; | |
152 | ||
153 | print Dumper($param); | |
154 | ||
155 | die "implement me"; | |
156 | ||
157 | }}); | |
158 | ||
159 | ||
160 | our $cmddef = { | |
161 | config => $PVE::APIClient::Commands::config::cmddef, | |
162 | list => $PVE::APIClient::Commands::list::cmddef, | |
163 | lxc => $PVE::APIClient::Commands::lxc::cmddef, | |
164 | remote => $PVE::APIClient::Commands::remote::cmddef, | |
2f964a75 RJ |
165 | |
166 | start => $PVE::APIClient::Commands::start::cmddef, | |
b133a905 DM |
167 | |
168 | api => { | |
169 | get => [ __PACKAGE__, 'pveclient_get', ['remote', 'api_path']], | |
170 | set => [ __PACKAGE__, 'pveclient_set', ['remote', 'api_path']], | |
171 | create => [ __PACKAGE__, 'pveclient_create', ['remote', 'api_path']], | |
172 | delete => [ __PACKAGE__, 'pveclient_delete', ['remote', 'api_path']], | |
173 | } | |
174 | }; | |
175 | ||
176 | ||
177 | __PACKAGE__->run_cli_handler(); |