]> git.proxmox.com Git - pve-manager.git/blob - PVE/API2/BackupInfo.pm
fix #2609 api: backupinfo: add non job specific endpoint
[pve-manager.git] / PVE / API2 / BackupInfo.pm
1 package PVE::API2::BackupInfo;
2
3 use strict;
4 use warnings;
5 use Digest::SHA;
6
7 use PVE::SafeSyslog;
8 use PVE::Tools qw(extract_param);
9 use PVE::Cluster qw(cfs_lock_file cfs_read_file cfs_write_file);
10 use PVE::RESTHandler;
11 use PVE::RPCEnvironment;
12 use PVE::JSONSchema;
13 use PVE::Storage;
14 use PVE::Exception qw(raise_param_exc);
15 use PVE::VZDump;
16 use PVE::VZDump::Common;
17
18 use base qw(PVE::RESTHandler);
19
20 sub map_job_vmids {
21 my ($job_included_guests, $included_vmids) = @_;
22
23 for my $node_vmids (values %{$job_included_guests}) {
24 for my $vmid (@{$node_vmids}) {
25 $included_vmids->{$vmid} = 1;
26 }
27 }
28
29 return $included_vmids;
30 }
31
32 sub get_included_vmids {
33 my $included_vmids = {};
34 my $vzconf = cfs_read_file('vzdump.cron');
35
36 my $all_jobs = $vzconf->{jobs} || [];
37
38 for my $job (@$all_jobs) {
39 my $job_included_guests = PVE::VZDump::get_included_guests($job);
40 $included_vmids = map_job_vmids($job_included_guests, $included_vmids);
41 }
42
43 return $included_vmids;
44 }
45
46 __PACKAGE__->register_method({
47 name => 'get_backupinfo',
48 path => '',
49 method => 'GET',
50 protected => 1,
51 description => "Stub, waits for future use.",
52 parameters => {
53 additionalProperties => 0,
54 properties => {},
55 },
56 returns => {
57 type => 'string',
58 description => 'Shows stub message',
59 },
60 code => sub {
61 return "Stub endpoint. There is nothing here yet.";
62 }});
63
64 __PACKAGE__->register_method({
65 name => 'get_guests_not_in_backup',
66 path => 'not_backed_up',
67 method => 'GET',
68 protected => 1,
69 description => "Shows all guests which are not covered by any backup job.",
70 permissions => {
71 check => ['perm', '/', ['Sys.Audit']],
72 },
73 parameters => {
74 additionalProperties => 0,
75 properties => {},
76 },
77 returns => {
78 type => 'array',
79 description => 'Contains the guest objects.',
80 items => {
81 type => 'object',
82 properties => {
83 vmid => {
84 type => 'integer',
85 description => 'VMID of the guest.',
86 },
87 name => {
88 type => 'string',
89 description => 'Name of the guest',
90 optional => 1,
91 },
92 type => {
93 type => 'string',
94 description => 'Type of the guest.',
95 enum => ['qemu', 'lxc'],
96 },
97 },
98 },
99 },
100 code => sub {
101 my $rpcenv = PVE::RPCEnvironment::get();
102 my $user = $rpcenv->get_user();
103 my $rrd = PVE::Cluster::rrd_dump();
104 my $included_vmids = get_included_vmids();
105 my $vmlist = PVE::Cluster::get_vmlist();
106 my @vmids = ( keys %{$vmlist->{ids}} );
107
108 # remove VMIDs to which the user has no permission to not leak infos
109 # like the guest name
110 my @allowed_vmids = grep {
111 $rpcenv->check($user, "/vms/$_", [ 'VM.Audit' ], 1);
112 } @vmids;
113
114 my $result = [];
115
116 for my $vmid (@allowed_vmids) {
117
118 next if $included_vmids->{$vmid};
119
120 my $type = $vmlist->{ids}->{$vmid}->{type};
121 my $node = $vmlist->{ids}->{$vmid}->{node};
122
123 my $conf;
124 my $name = "";
125
126 if ($type eq 'qemu') {
127 $conf = PVE::QemuConfig->load_config($vmid, $node);
128 $name = $conf->{name};
129 } elsif ($type eq 'lxc') {
130 $conf = PVE::LXC::Config->load_config($vmid, $node);
131 $name = $conf->{hostname};
132 } else {
133 die "VMID $vmid is neither Qemu nor LXC guest\n";
134 }
135
136 push @{$result}, {
137 vmid => int($vmid),
138 name => $name,
139 type => $type,
140 };
141 }
142
143 return $result;
144 }});
145 1;