]> git.proxmox.com Git - pmg-api.git/commitdiff
api: refactor rule parameters
authorDominik Csapak <d.csapak@proxmox.com>
Wed, 21 Feb 2024 12:24:28 +0000 (13:24 +0100)
committerStoiko Ivanov <s.ivanov@proxmox.com>
Wed, 21 Feb 2024 18:30:13 +0000 (19:30 +0100)
makes it easier to add new ones

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
src/PMG/API2/RuleDB.pm
src/PMG/API2/Rules.pm

index 1fddb32da92d0ea979af53e302ce700efc9c500d..6d27b147e14671211018497a1340042a71c0d682 100644 (file)
@@ -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);
     }});
index 4f8c10b34f108924be0bd37b3d6813cf78c38573..c48370fb4b603a732f37728933bfe9a9a56b90f9 100644 (file)
@@ -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 {