From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <d.csapak@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 71C9561102
 for <pve-devel@lists.proxmox.com>; Wed,  2 Dec 2020 10:21:50 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 84B9D191AB
 for <pve-devel@lists.proxmox.com>; Wed,  2 Dec 2020 10:21:19 +0100 (CET)
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))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id 9343719066
 for <pve-devel@lists.proxmox.com>; Wed,  2 Dec 2020 10:21:15 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 5C51844883
 for <pve-devel@lists.proxmox.com>; Wed,  2 Dec 2020 10:21:15 +0100 (CET)
From: Dominik Csapak <d.csapak@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Wed,  2 Dec 2020 10:21:08 +0100
Message-Id: <20201202092113.15911-6-d.csapak@proxmox.com>
X-Mailer: git-send-email 2.20.1
In-Reply-To: <20201202092113.15911-1-d.csapak@proxmox.com>
References: <20201202092113.15911-1-d.csapak@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.299 Adjusted score from AWL reputation of From: address
 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. [metricserver.pm, plugin.pm]
Subject: [pve-devel] [PATCH manager 3/7] status/plugin: extend with
 add/update/delete hooks
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE 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: Wed, 02 Dec 2020 09:21:50 -0000

like we do in it for the storage section configs

we will need this to store the token for influxdbs http api

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 PVE/API2/Cluster/MetricServer.pm | 39 +++++++++++++++++++++++++++-----
 PVE/Status/Plugin.pm             | 24 ++++++++++++++++++++
 2 files changed, 57 insertions(+), 6 deletions(-)

diff --git a/PVE/API2/Cluster/MetricServer.pm b/PVE/API2/Cluster/MetricServer.pm
index ec3c7b75..a6b99d4f 100644
--- a/PVE/API2/Cluster/MetricServer.pm
+++ b/PVE/API2/Cluster/MetricServer.pm
@@ -3,7 +3,7 @@ package PVE::API2::Cluster::MetricServer;
 use warnings;
 use strict;
 
-use PVE::Tools qw(extract_param);
+use PVE::Tools qw(extract_param extract_sensitive_params);
 use PVE::Exception qw(raise_perm_exc raise_param_exc);
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::RPCEnvironment;
@@ -152,6 +152,8 @@ __PACKAGE__->register_method ({
 	my $plugin = PVE::Status::Plugin->lookup($type);
 	my $id = extract_param($param, 'id');
 
+	my $sensitive_params = extract_sensitive_params($param, ['token'], []);
+
 	PVE::Cluster::cfs_lock_file('status.cfg', undef, sub {
 	    my $cfg = PVE::Cluster::cfs_read_file('status.cfg');
 
@@ -160,10 +162,20 @@ __PACKAGE__->register_method ({
 
 	    my $opts = $plugin->check_config($id, $param, 1, 1);
 
-	    $plugin->test_connection($opts);
-
 	    $cfg->{ids}->{$id} = $opts;
 
+	    $plugin->on_add_hook($id, $opts, $sensitive_params);
+
+	    eval {
+		$plugin->test_connection($opts, $id);
+	    };
+
+	    if (my $err = $@) {
+		eval { $plugin->on_delete_hook($id, $opts) };
+		warn "$@\n" if $@;
+		die $err;
+	    }
+
 	    PVE::Cluster::cfs_write_file('status.cfg', $cfg);
 	});
 	die $@ if $@;
@@ -190,6 +202,12 @@ __PACKAGE__->register_method ({
 	my $digest = extract_param($param, 'digest');
 	my $delete = extract_param($param, 'delete');
 
+	if ($delete) {
+	    $delete = [PVE::Tools::split_list($delete)];
+	}
+
+	my $sensitive_params = extract_sensitive_params($param, ['token'], $delete);
+
 	PVE::Cluster::cfs_lock_file('status.cfg', undef, sub {
 	    my $cfg = PVE::Cluster::cfs_read_file('status.cfg');
 
@@ -201,15 +219,13 @@ __PACKAGE__->register_method ({
 	    my $plugin = PVE::Status::Plugin->lookup($data->{type});
 	    my $opts = $plugin->check_config($id, $param, 0, 1);
 
-	    $plugin->test_connection($opts);
-
 	    for my $k (keys %$opts) {
 		$data->{$k} = $opts->{$k};
 	    }
 
 	    if ($delete) {
 		my $options = $plugin->private()->{options}->{$data->{type}};
-		for my $k (PVE::Tools::split_list($delete)) {
+		for my $k (@$delete) {
 		    my $d = $options->{$k} || die "no such option '$k'\n";
 		    die "unable to delete required option '$k'\n" if !$d->{optional};
 		    die "unable to delete fixed option '$k'\n" if $d->{fixed};
@@ -220,6 +236,10 @@ __PACKAGE__->register_method ({
 		}
 	    }
 
+	    $plugin->on_update_hook($id, $data, $sensitive_params);
+
+	    $plugin->test_connection($opts, $id);
+
 	    PVE::Cluster::cfs_write_file('status.cfg', $cfg);
 	});
 	die $@ if $@;
@@ -253,6 +273,13 @@ __PACKAGE__->register_method ({
 	    my $cfg = PVE::Cluster::cfs_read_file('status.cfg');
 
 	    my $id = $param->{id};
+
+	    my $plugin_cfg = $cfg->{ids}->{$id};
+
+	    my $plugin = PVE::Status::Plugin->lookup($plugin_cfg->{type});
+
+	    $plugin->on_delete_hook($id, $plugin_cfg);
+
 	    delete $cfg->{ids}->{$id};
 	    PVE::Cluster::cfs_write_file('status.cfg', $cfg);
 	});
diff --git a/PVE/Status/Plugin.pm b/PVE/Status/Plugin.pm
index 2fa223ca..fae7a0f0 100644
--- a/PVE/Status/Plugin.pm
+++ b/PVE/Status/Plugin.pm
@@ -153,4 +153,28 @@ sub update_storage_status {
     die "please implement inside plugin";
 }
 
+sub on_add_hook {
+    my ($class, $id, $opts, $sensitive_opts) = @_;
+
+    # implement in subclass
+
+    return undef;
+}
+
+sub on_update_hook {
+    my ($class, $id, $opts, $sensitive_opts) = @_;
+
+    # implement in subclass
+
+    return undef;
+}
+
+sub on_delete_hook {
+    my ($class, $id, $opts) = @_;
+
+    # implement in subclass
+
+    return undef;
+}
+
 1;
-- 
2.20.1