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

Dominik Csapak d.csapak at proxmox.com
Thu Feb 1 16:36:50 CET 2024


makes it easier to add new ones

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

diff --git a/src/PMG/API2/RuleDB.pm b/src/PMG/API2/RuleDB.pm
index 1fddb32..928b690 100644
--- a/src/PMG/API2/RuleDB.pm
+++ b/src/PMG/API2/RuleDB.pm
@@ -162,30 +162,12 @@ __PACKAGE__->register_method({
     permissions => { check => [ 'admin' ] },
     parameters => {
 	additionalProperties => 0,
-	properties => {
+	properties => PMG::API2::Rules::get_rule_params({
 	    name => {
 		description => "Rule name",
 		type => 'string',
 	    },
-	    priority => {
-		description => "Rule priotity.",
-		type => 'integer',
-		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 {
diff --git a/src/PMG/API2/Rules.pm b/src/PMG/API2/Rules.pm
index 4f8c10b..f9e69e2 100644
--- a/src/PMG/API2/Rules.pm
+++ b/src/PMG/API2/Rules.pm
@@ -136,6 +136,37 @@ __PACKAGE__->register_method ({
 	return $data;
    }});
 
+my $rule_params = {
+    priority => {
+	description => "Rule priotity.",
+	type => 'integer',
+	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,
+    },
+};
+
+sub get_rule_params {
+    my ($base) = @_;
+    $base //= {};
+    return {
+	$base->%*,
+	$rule_params->%*
+    };
+}
+
+
 __PACKAGE__->register_method ({
     name => 'update_config',
     path => 'config',
@@ -146,7 +177,7 @@ __PACKAGE__->register_method ({
     permissions => { check => [ 'admin' ] },
     parameters => {
 	additionalProperties => 0,
-	properties => {
+	properties => get_rule_params({
 	    id => {
 		description => "Rule ID.",
 		type => 'integer',
@@ -156,26 +187,7 @@ __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',
-		minimum => 0,
-		maximum => 100,
-		optional => 1,
-	    },
-	},
+	}),
     },
     returns => { type => "null" },
     code => sub {
-- 
2.30.2





More information about the pmg-devel mailing list