From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <a.lauterer@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id AA78F5B4FB
 for <pve-devel@pve.proxmox.com>; Tue,  7 Jul 2020 11:56:53 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id A60EB263B9
 for <pve-devel@pve.proxmox.com>; Tue,  7 Jul 2020 11:56:23 +0200 (CEST)
Received-SPF: pass (proxmox.com: 212.186.127.180 is authorized to use
 'a.lauterer@proxmox.com' in 'mfrom' identity (mechanism 'mx' matched))
 receiver=firstgate.proxmox.com; identity=mailfrom;
 envelope-from="a.lauterer@proxmox.com"; helo=proxmox-new.maurer-it.com;
 client-ip=212.186.127.180
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [212.186.127.180])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id B8E8926387
 for <pve-devel@pve.proxmox.com>; Tue,  7 Jul 2020 11:56:19 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 63C8B44302
 for <pve-devel@pve.proxmox.com>; Tue,  7 Jul 2020 11:49:03 +0200 (CEST)
From: Aaron Lauterer <a.lauterer@proxmox.com>
To: pve-devel@pve.proxmox.com
Date: Tue,  7 Jul 2020 11:49:01 +0200
Message-Id: <20200707094902.24712-5-a.lauterer@proxmox.com>
X-Mailer: git-send-email 2.20.1
In-Reply-To: <20200707094902.24712-1-a.lauterer@proxmox.com>
References: <20200707094902.24712-1-a.lauterer@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RCVD_IN_DNSWL_MED        -2.3 Sender listed at https://www.dnswl.org/,
 medium trust
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [nodes.pm, haconfig.pm, subscription.pm, vzdump.pm, cluster.pm,
 backupinfo.pm, backup.pm]
X-Mailman-Approved-At: Tue, 07 Jul 2020 12:27:43 +0200
Subject: [pve-devel] [PATCH v4 manager 4/5] fix #2609 api: backupinfo: add
 non job specific endpoint
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: PVE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Tue, 07 Jul 2020 09:56:53 -0000

Adds a new api endpoint at cluster/backupinfo 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 `not_backed_up` which
returns a list of all guests which are not covered by any backup job.

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@proxmox.com>
---

v2 -> v4: (v3 was skipped to align version number to the rest of the
	   series)
* renamed from `backupsummary` to `backupinfo`
* changed endpoint from `included_status` to `not_backed_up` as we only
  need to show guests not covered by any job instead of all guests and
  their status
* incorporated a few code style changes such as creating the object to
  be pushed to the return array at the push operation instead of way
  before

v1->v2:
* incorporated feedback for the other API endpoint that is also valid
  here
    * changed double negative for permissions `not_all_permissions` to
      `permissions_for_all`
* adapted to latest changes in return value of `get_included_guests`
    * created two new method which merge and flatten the info from all
      backup jobs
* renamed some vars to more descriptive names
* return VMIDs as int
* reworded some API descriptions
* added stub endpoint for root endpoint

 PVE/API2/BackupInfo.pm | 145 +++++++++++++++++++++++++++++++++++++++++
 PVE/API2/Cluster.pm    |   6 ++
 PVE/API2/Makefile      |   1 +
 3 files changed, 152 insertions(+)
 create mode 100644 PVE/API2/BackupInfo.pm

diff --git a/PVE/API2/BackupInfo.pm b/PVE/API2/BackupInfo.pm
new file mode 100644
index 00000000..909a5de1
--- /dev/null
+++ b/PVE/API2/BackupInfo.pm
@@ -0,0 +1,145 @@
+package PVE::API2::BackupInfo;
+
+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 base qw(PVE::RESTHandler);
+
+sub map_job_vmids {
+    my ($job_included_guests, $included_vmids) = @_;
+
+    for my $node_vmids (values %{$job_included_guests}) {
+	for my $vmid (@{$node_vmids}) {
+	    $included_vmids->{$vmid} = 1;
+	}
+    }
+
+    return $included_vmids;
+}
+
+sub get_included_vmids {
+    my $included_vmids = {};
+    my $vzconf = cfs_read_file('vzdump.cron');
+
+    my $all_jobs = $vzconf->{jobs} || [];
+
+    for my $job (@$all_jobs) {
+	my $job_included_guests = PVE::VZDump::get_included_guests($job);
+	$included_vmids = map_job_vmids($job_included_guests, $included_vmids);
+    }
+
+    return $included_vmids;
+}
+
+__PACKAGE__->register_method({
+    name => 'get_backupinfo',
+    path => '',
+    method => 'GET',
+    protected => 1,
+    description => "Stub, waits for future use.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {},
+    },
+    returns => {
+       type => 'string',
+       description => 'Shows stub message',
+    },
+    code => sub {
+       return "Stub endpoint. There is nothing here yet.";
+    }});
+
+__PACKAGE__->register_method({
+    name => 'get_guests_not_in_backup',
+    path => 'not_backed_up',
+    method => 'GET',
+    protected => 1,
+    description => "Shows all guests which are not covered by any backup job.",
+    permissions => {
+	check => ['perm', '/', ['Sys.Audit']],
+    },
+    parameters => {
+    	additionalProperties => 0,
+	properties => {},
+    },
+    returns => {
+	type => 'array',
+	description => 'Contains the guest objects.',
+	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'],
+		},
+	    },
+	},
+    },
+    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 @allowed_vmids = grep {
+		$rpcenv->check($user, "/vms/$_", [ 'VM.Audit' ], 1);
+	} @vmids;
+
+	my $result = [];
+
+	for my $vmid (@allowed_vmids) {
+
+	    next if $included_vmids->{$vmid};
+
+	    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";
+	    }
+
+	    push @{$result}, {
+		vmid => int($vmid),
+		name => $name,
+		type => $type,
+	    };
+	}
+
+	return $result;
+    }});
+1;
diff --git a/PVE/API2/Cluster.pm b/PVE/API2/Cluster.pm
index 062ca849..e768cbc6 100644
--- a/PVE/API2/Cluster.pm
+++ b/PVE/API2/Cluster.pm
@@ -23,6 +23,7 @@ use PVE::Tools qw(extract_param);
 use PVE::API2::ACMEAccount;
 use PVE::API2::ACMEPlugin;
 use PVE::API2::Backup;
+use PVE::API2::BackupInfo;
 use PVE::API2::Cluster::Ceph;
 use PVE::API2::ClusterConfig;
 use PVE::API2::Firewall::Cluster;
@@ -57,6 +58,11 @@ __PACKAGE__->register_method ({
     path => 'backup',
 });
 
+__PACKAGE__->register_method ({
+    subclass => "PVE::API2::BackupInfo",
+    path => 'backupinfo',
+});
+
 __PACKAGE__->register_method ({
     subclass => "PVE::API2::HAConfig",
     path => 'ha',
diff --git a/PVE/API2/Makefile b/PVE/API2/Makefile
index 28ecc070..bc5ccc36 100644
--- a/PVE/API2/Makefile
+++ b/PVE/API2/Makefile
@@ -10,6 +10,7 @@ PERLSOURCE = 			\
 	Subscription.pm		\
 	VZDump.pm		\
 	Backup.pm		\
+	BackupInfo.pm		\
 	Cluster.pm		\
 	HAConfig.pm		\
 	Nodes.pm		\
-- 
2.20.1