[pmg-devel] [PATCH pmg-api 07/12] RuleCache: load rule/objectgroup attributes from database

Stoiko Ivanov s.ivanov at proxmox.com
Tue Feb 20 14:18:20 CET 2024


On Fri,  9 Feb 2024 13:54:31 +0100
Dominik Csapak <d.csapak at proxmox.com> wrote:

> so that we can use the 'and' and 'invert' flags set.
> 
> Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
> ---
>  src/PMG/RuleCache.pm | 25 +++++++++++++++++++++++++
>  1 file changed, 25 insertions(+)
> 
> diff --git a/src/PMG/RuleCache.pm b/src/PMG/RuleCache.pm
> index 4f7ebe7..0b62aeb 100644
> --- a/src/PMG/RuleCache.pm
> +++ b/src/PMG/RuleCache.pm
> @@ -67,6 +67,23 @@ sub new {
>  	    $self->{"$ruleid:what"} = { groups => [] };
>  	    $self->{"$ruleid:action"} = { groups => [] };
>  
> +	    my $attribute_sth = $dbh->prepare("SELECT * FROM Rule_Attributes WHERE Rule_ID = ?");
> +	    $attribute_sth->execute($ruleid);
> +
> +	    while (my $ref = $attribute_sth->fetchrow_hashref()) {
> +		$self->{"$ruleid:from"}->{and} = $ref->{value} if $ref->{name} eq 'from-and';
not sure if it's worth it, or makes this less readable ..., but
if ($ref-name =~ /(from|to|when|what)-(and|invert)/) {
    $self->{"$ruleid:$1"}->{$2} = $ref->{value)
}
might work here as well

> +		$self->{"$ruleid:from"}->{invert} = $ref->{value} if $ref->{name} eq 'from-invert';
> +
> +		$self->{"$ruleid:to"}->{and} = $ref->{value} if $ref->{name} eq 'to-and';
> +		$self->{"$ruleid:to"}->{invert} = $ref->{value} if $ref->{name} eq 'to-invert';
> +
> +		$self->{"$ruleid:when"}->{and} = $ref->{value} if $ref->{name} eq 'when-and';
> +		$self->{"$ruleid:when"}->{invert} = $ref->{value} if $ref->{name} eq 'when-invert';
> +
> +		$self->{"$ruleid:what"}->{and} = $ref->{value} if $ref->{name} eq 'what-and';
> +		$self->{"$ruleid:what"}->{invert} = $ref->{value} if $ref->{name} eq 'what-invert';
> +	    }
> +
>  	    my $sth1 = $dbh->prepare(
>  		"SELECT Objectgroup_ID, Grouptype FROM RuleGroup " .
>  		"where RuleGroup.Rule_ID = '$ruleid' " .
> @@ -114,6 +131,14 @@ sub new {
>  		    objects => $objects,
>  		};
>  
> +		my $objectgroup_sth = $dbh->prepare("SELECT * FROM Objectgroup_Attributes WHERE Objectgroup_ID = ?");
> +		$objectgroup_sth->execute($groupid);
> +
> +		while (my $ref = $objectgroup_sth->fetchrow_hashref()) {
> +		    $group->{and} = $ref->{value} if $ref->{name} eq 'and';
> +		    $group->{invert} = $ref->{value} if $ref->{name} eq 'invert';
> +		}
> +
>  		my $type = $type_map->{$gtype};
>  		push $self->{"$ruleid:$type"}->{groups}->@*, $group;
>  	    }





More information about the pmg-devel mailing list