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