]>
git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Helpers.pm
1 package PVE
::APIClient
::Helpers
;
8 use PVE
::APIClient
::Exception
qw(raise);
12 use HTTP
::Status
qw(:constants);
14 my $pve_api_definition;
15 my $pve_api_path_hash;
17 my $pve_api_definition_fn = "/usr/share/pve-client/pve-api-definition.js";
19 my $build_pve_api_path_hash;
20 $build_pve_api_path_hash = sub {
23 my $class = ref($tree);
24 return $tree if !$class;
26 if ($class eq 'ARRAY') {
27 foreach my $el (@$tree) {
28 $build_pve_api_path_hash->($el);
30 } elsif ($class eq 'HASH') {
31 if (defined($tree->{leaf
}) && defined(my $path = $tree->{path
})) {
32 $pve_api_path_hash->{$path} = $tree;
34 foreach my $k (keys %$tree) {
35 $build_pve_api_path_hash->($tree->{$k});
40 sub get_api_definition
{
42 if (!defined($pve_api_definition)) {
44 open(my $fh, '<', $pve_api_definition_fn) ||
45 die "unable to open '$pve_api_definition_fn' - $!\n";
46 my $json_text = <$fh>;
47 $pve_api_definition = decode_json
($json_text);
49 $build_pve_api_path_hash->($pve_api_definition);
53 return $pve_api_definition;
56 sub lookup_api_method
{
57 my ($path, $method) = @_;
59 get_api_definition
(); # make sure API data is loaded
61 my $info = $pve_api_path_hash->{$path} ||
62 die "unable to find API info for path '$path'\n";
64 my $data = $info->{info
}->{$method} ||
65 die "unable to find API method '$method' for path '$path'\n";
70 # Getopt wrapper - copied from PVE::JSONSchema::get_options
71 # a way to parse command line parameters, using a
72 # schema to configure Getopt::Long
74 my ($schema, $args, $arg_param, $fixed_param, $pwcallback, $param_mapping_hash) = @_;
76 if (!$schema || !$schema->{properties
}) {
77 raise
("too many arguments\n", code
=> HTTP_BAD_REQUEST
)
78 if scalar(@$args) != 0;
83 if ($arg_param && !ref($arg_param)) {
84 my $pd = $schema->{properties
}->{$arg_param};
85 die "expected list format $pd->{format}"
86 if !($pd && $pd->{format
} && $pd->{format
} =~ m/-list/);
87 $list_param = $arg_param;
92 foreach my $prop (keys %{$schema->{properties
}}) {
93 my $pd = $schema->{properties
}->{$prop};
94 next if $list_param && $prop eq $list_param;
95 next if defined($fixed_param->{$prop});
97 my $mapping = $param_mapping_hash->{$prop};
98 if ($mapping && $mapping->{interactive
}) {
99 # interactive parameters such as passwords: make the argument
100 # optional and call the mapping function afterwards.
101 push @getopt, "$prop:s";
102 push @interactive, [$prop, $mapping->{func
}];
103 } elsif ($prop eq 'password' && $pwcallback) {
104 # we do not accept plain password on input line, instead
105 # we turn this into a boolean option and ask for password below
106 # using $pwcallback() (for security reasons).
107 push @getopt, "$prop";
108 } elsif ($pd->{type
} eq 'boolean') {
109 push @getopt, "$prop:s";
111 if ($pd->{format
} && $pd->{format
} =~ m/-a?list/) {
112 push @getopt, "$prop=s@";
114 push @getopt, "$prop=s";
119 Getopt
::Long
::Configure
('prefix_pattern=(--|-)');
122 raise
("unable to parse option\n", code
=> HTTP_BAD_REQUEST
)
123 if !Getopt
::Long
::GetOptionsFromArray
($args, $opts, @getopt);
127 $opts->{$list_param} = $args;
129 } elsif (ref($arg_param)) {
130 foreach my $arg_name (@$arg_param) {
131 if ($opts->{'extra-args'}) {
132 raise
("internal error: extra-args must be the last argument\n", code
=> HTTP_BAD_REQUEST
);
134 if ($arg_name eq 'extra-args') {
135 $opts->{'extra-args'} = $args;
139 raise
("not enough arguments\n", code
=> HTTP_BAD_REQUEST
) if !@$args;
140 $opts->{$arg_name} = shift @$args;
142 raise
("too many arguments\n", code
=> HTTP_BAD_REQUEST
) if @$args;
144 raise
("too many arguments\n", code
=> HTTP_BAD_REQUEST
)
145 if scalar(@$args) != 0;
149 if (my $pd = $schema->{properties
}->{password
}) {
150 if ($pd->{type
} ne 'boolean' && $pwcallback) {
151 if ($opts->{password
} || !$pd->{optional
}) {
152 $opts->{password
} = &$pwcallback();
157 foreach my $entry (@interactive) {
158 my ($opt, $func) = @$entry;
159 my $pd = $schema->{properties
}->{$opt};
160 my $value = $opts->{$opt};
161 if (defined($value) || !$pd->{optional
}) {
162 $opts->{$opt} = $func->($value);
166 # decode after Getopt as we are not sure how well it handles unicode
167 foreach my $p (keys %$opts) {
168 if (!ref($opts->{$p})) {
169 $opts->{$p} = decode
('locale', $opts->{$p});
170 } elsif (ref($opts->{$p}) eq 'ARRAY') {
172 foreach my $v (@{$opts->{$p}}) {
173 push @$tmp, decode
('locale', $v);
176 } elsif (ref($opts->{$p}) eq 'SCALAR') {
177 $opts->{$p} = decode
('locale', $$opts->{$p});
179 raise
("decoding options failed, unknown reference\n", code
=> HTTP_BAD_REQUEST
);
183 foreach my $p (keys %$opts) {
184 if (my $pd = $schema->{properties
}->{$p}) {
185 if ($pd->{type
} eq 'boolean') {
186 if ($opts->{$p} eq '') {
188 } elsif (defined(my $bool = parse_boolean
($opts->{$p}))) {
191 raise
("unable to parse boolean option\n", code
=> HTTP_BAD_REQUEST
);
193 } elsif ($pd->{format
}) {
195 if ($pd->{format
} =~ m/-list/) {
196 # allow --vmid 100 --vmid 101 and --vmid 100,101
197 # allow --dow mon --dow fri and --dow mon,fri
198 $opts->{$p} = join(",", @{$opts->{$p}}) if ref($opts->{$p}) eq 'ARRAY';
199 } elsif ($pd->{format
} =~ m/-alist/) {
200 # we encode array as \0 separated strings
201 # Note: CGI.pm also use this encoding
202 if (scalar(@{$opts->{$p}}) != 1) {
203 $opts->{$p} = join("\0", @{$opts->{$p}});
205 # st that split_list knows it is \0 terminated
206 my $v = $opts->{$p}->[0];
207 $opts->{$p} = "$v\0";
214 foreach my $p (keys %$fixed_param) {
215 $opts->{$p} = $fixed_param->{$p};