[pmg-devel] [PATCH pmg-api v2 02/10] api: refactor rule parameters

Dominik Csapak d.csapak at proxmox.com
Wed Feb 21 13:24:28 CET 2024


makes it easier to add new ones

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
changes from v1:
* don't give active/direction on initial Rule->new

 src/PMG/API2/RuleDB.pm | 23 +++++++----------------
 src/PMG/API2/Rules.pm  | 41 +++++++++++++++++++++++++++--------------
 2 files changed, 34 insertions(+), 30 deletions(-)

diff --git a/src/PMG/API2/RuleDB.pm b/src/PMG/API2/RuleDB.pm
index 1fddb32..6d27b14 100644
--- a/src/PMG/API2/RuleDB.pm
+++ b/src/PMG/API2/RuleDB.pm
@@ -162,7 +162,7 @@ __PACKAGE__->register_method({
     permissions => { check => [ 'admin' ] },
     parameters => {
 	additionalProperties => 0,
-	properties => {
+	properties => PMG::API2::Rules::get_rule_params({
 	    name => {
 		description => "Rule name",
 		type => 'string',
@@ -173,19 +173,7 @@ __PACKAGE__->register_method({
 		minimum => 0,
 		maximum => 100,
 	    },
-	    direction => {
-		description => "Rule direction. Value `0` matches incoming mails, value `1` matches outgoing mails, and value `2` matches both directions.",
-		type => 'integer',
-		minimum => 0,
-		maximum => 2,
-		optional => 1,
-	    },
-	    active => {
-		description => "Flag to activate rule.",
-		type => 'boolean',
-		optional => 1,
-	    },
-	},
+	}),
     },
     returns => { type => 'integer' },
     code => sub {
@@ -193,8 +181,11 @@ __PACKAGE__->register_method({
 
 	my $rdb = PMG::RuleDB->new();
 
-	my $rule = PMG::RuleDB::Rule->new (
-	    $param->{name}, $param->{priority}, $param->{active}, $param->{direction});
+	my $rule = PMG::RuleDB::Rule->new ($param->{name}, $param->{priority});
+
+	for my $key (keys PMG::API2::Rules::get_rule_params()->%*) {
+	    $rule->{$key} = $param->{$key} if defined($param->{$key});
+	}
 
 	return $rdb->save_rule($rule);
     }});
diff --git a/src/PMG/API2/Rules.pm b/src/PMG/API2/Rules.pm
index 4f8c10b..c48370f 100644
--- a/src/PMG/API2/Rules.pm
+++ b/src/PMG/API2/Rules.pm
@@ -136,6 +136,31 @@ __PACKAGE__->register_method ({
 	return $data;
    }});
 
+my $rule_params = {
+    direction => {
+	description => "Rule direction. Value `0` matches incoming mails, value `1` matches outgoing mails, and value `2` matches both directions.",
+	type => 'integer',
+	minimum => 0,
+	maximum => 2,
+	optional => 1,
+    },
+    active => {
+	description => "Flag to activate rule.",
+	type => 'boolean',
+	optional => 1,
+    },
+};
+
+sub get_rule_params {
+    my ($base) = @_;
+    $base //= {};
+    return {
+	$base->%*,
+	$rule_params->%*
+    };
+}
+
+
 __PACKAGE__->register_method ({
     name => 'update_config',
     path => 'config',
@@ -146,7 +171,7 @@ __PACKAGE__->register_method ({
     permissions => { check => [ 'admin' ] },
     parameters => {
 	additionalProperties => 0,
-	properties => {
+	properties => get_rule_params({
 	    id => {
 		description => "Rule ID.",
 		type => 'integer',
@@ -156,18 +181,6 @@ __PACKAGE__->register_method ({
 		type => 'string',
 		optional => 1,
 	    },
-	    active => {
-		description => "Flag to activate rule.",
-		type => 'boolean',
-		optional => 1,
-	    },
-	    direction => {
-		description => "Rule direction. Value `0` matches incoming mails, value `1` matches outgoing mails, and value `2` matches both directions.",
-		type => 'integer',
-		minimum => 0,
-		maximum => 2,
-		optional => 1,
-	    },
 	    priority => {
 		description => "Rule priotity.",
 		type => 'integer',
@@ -175,7 +188,7 @@ __PACKAGE__->register_method ({
 		maximum => 100,
 		optional => 1,
 	    },
-	},
+	}),
     },
     returns => { type => "null" },
     code => sub {
-- 
2.30.2





More information about the pmg-devel mailing list