[pmg-devel] [PATCH pmg-api 04/12] api: refactor rule parameters
Stoiko Ivanov
s.ivanov at proxmox.com
Tue Feb 20 12:49:31 CET 2024
looks good to me - one nit inline:
On Fri, 9 Feb 2024 13:54:28 +0100
Dominik Csapak <d.csapak at proxmox.com> wrote:
> 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});
you could remove active and direction here, as they get set
>
> + for my $key (keys PMG::API2::Rules::get_rule_params()->%*) {
> + $rule->{$key} = $param->{$key} if defined($param->{$key});
> + }
> +
here
(also PMG::RuleDB::Rule::new could probably be made shorter (and the
get/set methods could add an explicit 'return'
but as said - only a nit and not really related.
> 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 {
More information about the pmg-devel
mailing list