[pmg-devel] [PATCH pmg-api 04/12] api: refactor rule parameters

Dominik Csapak d.csapak at proxmox.com
Fri Feb 9 13:54:28 CET 2024


makes it easier to add new ones

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 src/PMG/API2/RuleDB.pm | 20 ++++++--------------
 src/PMG/API2/Rules.pm  | 41 +++++++++++++++++++++++++++--------------
 2 files changed, 33 insertions(+), 28 deletions(-)

diff --git a/src/PMG/API2/RuleDB.pm b/src/PMG/API2/RuleDB.pm
index 1fddb32..064e72f 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 {
@@ -196,6 +184,10 @@ __PACKAGE__->register_method({
 	my $rule = PMG::RuleDB::Rule->new (
 	    $param->{name}, $param->{priority}, $param->{active}, $param->{direction});
 
+	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