[pve-devel] [RFC manager 4/5] fix #2609 api: backupsummary: add non job specific endpoint

Aaron Lauterer a.lauterer at proxmox.com
Mon Apr 6 16:24:25 CEST 2020


Adds a new api endpoint at cluster/backupsummary for cluster wide backup
stuff. This is necessary because cluster/backup expects a backup job ID
at the next level and thus other endpoints are hard to impossible to
implement under that hierarchy.

The only api endpoint available for now is the `included_status` which
returns a list of all guests and wheter they are included in a backup
job or not backed up by any.

The top level index endpoint is left unsused for now to be available for
a more generic summary endpoint in the future.

Signed-off-by: Aaron Lauterer <a.lauterer at proxmox.com>
---

Feedback regarding the naming is more than welcome, also if it's a good
idea to leave the cluster/backupsummary root/index path empty for now.

I do think that it would better be used for a more generic summary
endpoint in the future.

 PVE/API2/BackupSummary.pm | 143 ++++++++++++++++++++++++++++++++++++++
 PVE/API2/Cluster.pm       |   6 ++
 PVE/API2/Makefile         |   1 +
 3 files changed, 150 insertions(+)
 create mode 100644 PVE/API2/BackupSummary.pm

diff --git a/PVE/API2/BackupSummary.pm b/PVE/API2/BackupSummary.pm
new file mode 100644
index 00000000..7aa55744
--- /dev/null
+++ b/PVE/API2/BackupSummary.pm
@@ -0,0 +1,143 @@
+package PVE::API2::BackupSummary;
+
+use strict;
+use warnings;
+use Digest::SHA;
+
+use PVE::SafeSyslog;
+use PVE::Tools qw(extract_param);
+use PVE::Cluster qw(cfs_lock_file cfs_read_file cfs_write_file);
+use PVE::RESTHandler;
+use PVE::RPCEnvironment;
+use PVE::JSONSchema;
+use PVE::Storage;
+use PVE::Exception qw(raise_param_exc);
+use PVE::VZDump;
+use PVE::VZDump::Common;
+
+use Data::Dumper;
+
+use base qw(PVE::RESTHandler);
+
+sub get_included_vmids() {
+    my $included_vmids = {};
+    my $vzconf = cfs_read_file('vzdump.cron');
+
+    my $all_jobs = $vzconf->{jobs} || [];
+
+    foreach my $job (@$all_jobs) {
+	my ($local_vmids, $cluster_vmids) = PVE::VZDump->get_included_guests($job);
+	my @vmids = ( @$local_vmids, @$cluster_vmids );
+
+	foreach my $vmid (@vmids) {
+	    $included_vmids->{$vmid} = 1;
+	}
+    }
+
+    return $included_vmids;
+}
+
+__PACKAGE__->register_method({
+    name => 'get_guest_backup_status',
+    path => 'included_status',
+    method => 'GET',
+    protected => 1,
+    description => "Shows all guests and whether they are part of any backup job.",
+    permissions => {
+	check => ['perm', '/', ['Sys.Audit']],
+    },
+    parameters => {
+    	additionalProperties => 0,
+	properties => {},
+    },
+    returns => {
+	type => 'object',
+	description => 'contains array with data and flag if VMs might be invisible due to permissions',
+	properties => {
+	    not_all_permissions => {
+		type => 'boolean',
+		optional => 1,
+		description => 'Wheter the user is missing permissions to view all guests.',
+	    },
+	    guests => {
+		type => 'array',
+		items => {
+		    type => 'object',
+		    properties => {
+			vmid => {
+			    type => 'integer',
+			    description => 'VMID of the guest.',
+			},
+			name => {
+			    type => 'string',
+			    description => 'Name of the guest',
+			    optional => 1,
+			},
+			type => {
+			    type => 'string',
+			    description => 'Type of the guest.',
+			    enum => ['qemu', 'lxc'],
+			},
+			backed_up => {
+			    type => 'boolean',
+			    description => 'Whether the guest is backed up.',
+			},
+		    },
+		},
+	    },
+	},
+    },
+    code => sub {
+	my $rpcenv = PVE::RPCEnvironment::get();
+	my $user = $rpcenv->get_user();
+	my $rrd = PVE::Cluster::rrd_dump();
+	my $included_vmids = get_included_vmids();
+	my $vmlist = PVE::Cluster::get_vmlist();
+	my @vmids = ( keys %{$vmlist->{ids}} );
+
+	# remove VMIDs to which the user has no permission to not leak infos
+	# like the guest name
+	my $not_all_permissions = 0;
+	@vmids = grep {
+		my $allowed = $rpcenv->check($user, "/vms/$_", [ 'VM.Backup' ], 1);
+		$not_all_permissions = 1 if !$allowed;
+		$allowed;
+	} @vmids;
+
+	my $result = {
+	    not_all_permissions => $not_all_permissions,
+	    guests => [],
+	};
+
+	foreach (@vmids) {
+	    my $vmid = $_;
+
+	    my $guest = {
+		vmid => $vmid + 0,
+	    };
+
+	    my $type = $vmlist->{ids}->{$vmid}->{type};
+	    my $node = $vmlist->{ids}->{$vmid}->{node};
+
+	    my $conf;
+	    my $name = "";
+
+	    if ($type eq 'qemu') {
+		$conf = PVE::QemuConfig->load_config($vmid, $node);
+		$name = $conf->{name};
+	    } elsif ($type eq 'lxc') {
+		$conf = PVE::LXC::Config->load_config($vmid, $node);
+		$name = $conf->{hostname};
+	    } else {
+		die "VMID $vmid is neither Qemu nor LXC guest\n";
+	    }
+
+	    $guest->{name} = $name;
+	    $guest->{type} = $type;
+	    $guest->{backed_up} = $included_vmids->{$vmid} // 0;
+
+	    push @{$result->{guests}}, $guest;
+	}
+	return $result;
+    }});
+1;
diff --git a/PVE/API2/Cluster.pm b/PVE/API2/Cluster.pm
index c802d440..4efdbec3 100644
--- a/PVE/API2/Cluster.pm
+++ b/PVE/API2/Cluster.pm
@@ -22,6 +22,7 @@ use PVE::Tools qw(extract_param);
 
 use PVE::API2::ACMEAccount;
 use PVE::API2::Backup;
+use PVE::API2::BackupSummary;
 use PVE::API2::Cluster::Ceph;
 use PVE::API2::ClusterConfig;
 use PVE::API2::Firewall::Cluster;
@@ -56,6 +57,11 @@ __PACKAGE__->register_method ({
     path => 'backup',
 });
 
+__PACKAGE__->register_method ({
+    subclass => "PVE::API2::BackupSummary",
+    path => 'backupsummary',
+});
+
 __PACKAGE__->register_method ({
     subclass => "PVE::API2::HAConfig",
     path => 'ha',
diff --git a/PVE/API2/Makefile b/PVE/API2/Makefile
index 8554efa1..8de47880 100644
--- a/PVE/API2/Makefile
+++ b/PVE/API2/Makefile
@@ -10,6 +10,7 @@ PERLSOURCE = 			\
 	Subscription.pm		\
 	VZDump.pm		\
 	Backup.pm		\
+	BackupSummary.pm	\
 	Cluster.pm		\
 	HAConfig.pm		\
 	Nodes.pm		\
-- 
2.20.1





More information about the pve-devel mailing list