]>
git.proxmox.com Git - pve-manager.git/blob - PVE/API2.pm
14 use base
qw(PVE::RESTHandler);
17 use PVE
::API2
::Cluster
;
20 use PVE
::API2
::AccessControl
;
21 use PVE
::API2
::Storage
::Config
;
23 __PACKAGE__-
>register_method ({
24 subclass
=> "PVE::API2::Cluster",
28 __PACKAGE__-
>register_method ({
29 subclass
=> "PVE::API2::Nodes",
33 __PACKAGE__-
>register_method ({
34 subclass
=> "PVE::API2::Storage::Config",
38 __PACKAGE__-
>register_method ({
39 subclass
=> "PVE::API2::AccessControl",
43 __PACKAGE__-
>register_method ({
44 subclass
=> "PVE::API2::Pool",
48 __PACKAGE__-
>register_method ({
52 permissions
=> { user
=> 'all' },
53 description
=> "Directory index.",
55 additionalProperties
=> 0,
63 subdir
=> { type
=> 'string' },
66 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
69 my ($resp, $param) = @_;
71 my $res = [ { subdir
=> 'version' } ];
73 my $ma = PVE
::API2-
>method_attributes();
75 foreach my $info (@$ma) {
76 next if !$info->{subclass
};
78 my $subpath = $info->{match_re
}->[0];
80 push @$res, { subdir
=> $subpath };
86 __PACKAGE__-
>register_method ({
90 permissions
=> { user
=> 'all' },
91 description
=> "API version details. The result also includes the global datacenter confguration.",
93 additionalProperties
=> 0,
99 version
=> { type
=> 'string' },
100 release
=> { type
=> 'string' },
101 repoid
=> { type
=> 'string' },
105 my ($resp, $param) = @_;
107 my $res = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
109 my $vi = PVE
::pvecfg
::version_info
();
110 foreach my $k (qw(version release repoid)) {
111 $res->{$k} = $vi->{$k};
117 # register result formaters
119 my $prepare_response_data = sub {
120 my ($format, $res) = @_;
124 data
=> $res->{data
},
126 if (scalar(keys %{$res->{errors
}})) {
128 $new->{errors
} = $res->{errors
};
131 if ($format eq 'extjs' || $format eq 'htmljs') {
132 # HACK: extjs wants 'success' property instead of useful HTTP status codes
133 if (HTTP
::Status
::is_error
($res->{status
})) {
135 $new->{message
} = $res->{message
} || status_message
($res->{status
});
136 $new->{status
} = $res->{status
} || 200;
137 $res->{message
} = undef;
138 $res->{status
} = 200;
140 $new->{success
} = $success;
143 if ($success && $res->{total
}) {
144 $new->{total
} = $res->{total
};
147 if ($success && $res->{changes
}) {
148 $new->{changes
} = $res->{changes
};
154 PVE
::HTTPServer
::register_formater
('json', sub {
155 my ($res, $data, $param, $path, $auth) = @_;
159 my $ct = 'application/json;charset=UTF-8';
161 &$prepare_response_data('json', $res);
163 my $raw = to_json
($res->{data
}, {utf8
=> 1, allow_nonref
=> 1});
165 return ($raw, $ct, $nocomp);
169 PVE
::HTTPServer
::register_formater
('extjs', sub {
170 my ($res, $data, $param, $path, $auth) = @_;
174 my $ct = 'application/json;charset=UTF-8';
176 &$prepare_response_data('extjs', $res);
178 my $raw = to_json
($res->{data
}, {utf8
=> 1, allow_nonref
=> 1});
180 return ($raw, $ct, $nocomp);
183 PVE
::HTTPServer
::register_formater
('htmljs', sub {
184 my ($res, $data, $param, $path, $auth) = @_;
188 # we use this for extjs file upload forms
190 my $ct = 'text/html;charset=UTF-8';
192 &$prepare_response_data('htmljs', $res);
194 my $raw = encode_entities
(to_json
($res->{data
}, {allow_nonref
=> 1}));
196 return ($raw, $ct, $nocomp);
200 PVE
::HTTPServer
::register_formater
('spiceconfig', sub {
201 my ($res, $data, $param, $path, $auth) = @_;
205 my $ct = 'application/x-virt-viewer;charset=UTF-8';
207 &$prepare_response_data('spiceconfig', $res);
209 $data = $res->{data
};
213 if ($data && ref($data) && ref($data->{data
})) {
214 $raw = "[virt-viewer]\n";
215 while (my ($key, $value) = each %{$data->{data
}}) {
216 $raw .= "$key=$value\n" if defined($value);
220 return ($raw, $ct, $nocomp);
223 PVE
::HTTPServer
::register_formater
('png', sub {
224 my ($res, $data, $param, $path, $auth) = @_;
228 my $ct = 'image/png';
230 &$prepare_response_data('png', $res);
232 $data = $res->{data
};
234 # fixme: better to revove that whole png thing ?
239 if ($data && ref($data) && ref($data->{data
}) &&
240 $data->{data
}->{filename
} && defined($data->{data
}->{image
})) {
241 $filename = $data->{data
}->{filename
};
242 $raw = $data->{data
}->{image
};
245 return ($raw, $ct, $nocomp);
248 PVE
::HTTPServer
::register_formater
('html', sub {
249 my ($res, $data, $param, $path, $auth) = @_;
253 my $ct = 'text/html;charset=UTF-8';
255 &$prepare_response_data('html', $res);
257 $data = $res->{data
};
259 my $info = $res->{info
};
261 my $raw = "<html><body>";
262 if (!HTTP
::Status
::is_success
($res->{status
})) {
263 my $msg = $res->{message
} || '';
264 $raw .= "<h1>ERROR $res->{status} $msg</h1>";
266 my $lnk = PVE
::JSONSchema
::method_get_child_link
($info);
268 if ($lnk && $data && $data->{data
} && HTTP
::Status
::is_success
($res->{status
})) {
270 my $href = $lnk->{href
};
271 if ($href =~ m/^\{(\S+)\}$/) {
273 $path =~ s/\/+$//; # remove trailing slash
274 foreach my $elem (sort {$a->{$prop} cmp $b->{$prop}} @{$data->{data
}}) {
277 if (defined(my $value = $elem->{$prop})) {
279 if (scalar(keys %$elem) > 1) {
280 my $tv = to_json
($elem, {allow_nonref
=> 1, canonical
=> 1});
281 $raw .= "<a href='$path/$value'>$value</a> <pre>$tv</pre><br>";
283 $raw .= "<a href='$path/$value'>$value</a><br>";
291 $raw .= encode_entities
(to_json
($data, {allow_nonref
=> 1, pretty
=> 1}));
294 $raw .= "</body></html>";
296 return ($raw, $ct, $nocomp);