[pmg-devel] [PATCH pmg-api v2 05/10] RuleCache: load rule/objectgroup attributes from database

Dominik Csapak d.csapak at proxmox.com
Wed Feb 21 13:24:31 CET 2024


so that we can use the 'and' and 'invert' flags set.

This also adds the attributes to the digest of the rule cache so the
cluster sync is triggered when the attributes change.

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
changes from v1:
* use regex to get the attributes instead of hardcoding each one (for the rules)
* add them to the digest so rule changes get synced

 src/PMG/RuleCache.pm | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/src/PMG/RuleCache.pm b/src/PMG/RuleCache.pm
index cd56342..4bde2e7 100644
--- a/src/PMG/RuleCache.pm
+++ b/src/PMG/RuleCache.pm
@@ -67,6 +67,21 @@ sub new {
 	    $self->{"$ruleid:what"} = { groups => [] };
 	    $self->{"$ruleid:action"} = { groups => [] };
 
+	    my $attribute_sth = $dbh->prepare("SELECT * FROM Rule_Attributes WHERE Rule_ID = ? ORDER BY Name");
+	    $attribute_sth->execute($ruleid);
+
+	    my $rule_attributes = [];
+	    while (my $ref = $attribute_sth->fetchrow_hashref()) {
+		if ($ref->{name} =~ m/^(from|to|when|what)-(and|invert)$/) {
+		    my $type = $1;
+		    my $prop = $2;
+		    my $value = $ref->{value};
+		    $self->{"${ruleid}:${type}"}->{$prop} = $value;
+
+		    $sha1->add("${ruleid}:${type}-${prop}=${value}|");
+		}
+	    }
+
 	    my $sth1 = $dbh->prepare(
 		"SELECT Objectgroup_ID, Grouptype FROM RuleGroup " .
 		"where RuleGroup.Rule_ID = '$ruleid' " .
@@ -114,6 +129,15 @@ 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';
+		}
+		$sha1->add (join(',', $groupid, $group->{and} // 0, $group->{invert} // 0), "|");
+
 		my $type = $type_map->{$gtype};
 		push $self->{"$ruleid:$type"}->{groups}->@*, $group;
 	    }
-- 
2.30.2





More information about the pmg-devel mailing list