all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Leo Nunner <l.nunner@proxmox.com>
To: pmg-devel@lists.proxmox.com
Subject: [pmg-devel] [PATCH pmg-api 6/8] feature: match groups: parse field into objects
Date: Fri,  7 Apr 2023 15:42:52 +0200	[thread overview]
Message-ID: <20230407134258.199691-7-l.nunner@proxmox.com> (raw)
In-Reply-To: <20230407134258.199691-1-l.nunner@proxmox.com>

Parse the MatchGroup value from the databsae into the 'and' property of
our abstraction objects. We are skipping this step for 'action' objects.
A function was added to update the MatchGroup value for a specific
object-rule relation.

Signed-off-by: Leo Nunner <l.nunner@proxmox.com>
---
 src/PMG/API2/ObjectGroupHelpers.pm |  1 +
 src/PMG/RuleCache.pm               |  4 +++-
 src/PMG/RuleDB.pm                  | 21 ++++++++++++++++++++-
 3 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/src/PMG/API2/ObjectGroupHelpers.pm b/src/PMG/API2/ObjectGroupHelpers.pm
index c3e6448..9bedce2 100644
--- a/src/PMG/API2/ObjectGroupHelpers.pm
+++ b/src/PMG/API2/ObjectGroupHelpers.pm
@@ -51,6 +51,7 @@ sub format_object_group {
 	    name => $og->{name},
 	    info => $og->{info},
 	    negate => $og->{negate},
+	    'and' => $og->{and},
 	};
     }
     return $res;
diff --git a/src/PMG/RuleCache.pm b/src/PMG/RuleCache.pm
index 04e35da..9a531ef 100644
--- a/src/PMG/RuleCache.pm
+++ b/src/PMG/RuleCache.pm
@@ -56,7 +56,7 @@ sub new {
 	    my ($from, $to, $when, $what, $action);
 
 	    my $sth1 = $dbh->prepare(
-		"SELECT Objectgroup_ID, Grouptype, Negate FROM RuleGroup " .
+		"SELECT Objectgroup_ID, Grouptype, Negate, MatchGroup FROM RuleGroup " .
 		"where RuleGroup.Rule_ID = '$ruleid' " .
 		"ORDER BY Grouptype, Objectgroup_ID");
 
@@ -65,6 +65,7 @@ sub new {
 		my $gtype = $ref1->{grouptype};
 		my $groupid = $ref1->{objectgroup_id};
 		my $negate = $ref1->{negate};
+		my $and = $ref1->{matchgroup};
 
 		# emtyp groups differ from non-existent groups!
 
@@ -93,6 +94,7 @@ sub new {
 
 		    if ($gtype != 4) { # it doesn't make any sense to negate actions
 			$obj->{negate} = $negate;
+			$obj->{and} = $and;
 		    }
 
 		    if ($gtype == 0) {      #from
diff --git a/src/PMG/RuleDB.pm b/src/PMG/RuleDB.pm
index 98beda3..dc2be20 100644
--- a/src/PMG/RuleDB.pm
+++ b/src/PMG/RuleDB.pm
@@ -107,7 +107,7 @@ sub load_groups {
 
     my $sth = $self->{dbh}->prepare(
 	"SELECT RuleGroup.Grouptype, Objectgroup.ID, " .
-	"Objectgroup.Name, Objectgroup.Info, Negate " .
+	"Objectgroup.Name, Objectgroup.Info, Negate, MatchGroup " .
 	"FROM Rulegroup, Objectgroup " .
 	"WHERE Rulegroup.Rule_ID = ? and " .
 	"Rulegroup.Objectgroup_ID = Objectgroup.ID " .
@@ -125,6 +125,7 @@ sub load_groups {
 
 	if ($ref->{'grouptype'} != 4) { # this doesn't make any sense for actions
 	    $og->{negate} = $ref->{negate};
+	    $og->{and} = $ref->{matchgroup};
 	}
 
 	if ($ref->{'grouptype'} == 0) {      #from
@@ -797,6 +798,24 @@ sub rule_set_group_setting_negate {
     return 1;
 }
 
+sub rule_set_group_setting_matchgroup {
+    my ($self, $value, $ruleid, $groupid, $gtype_str) = @_;
+
+    my $gtype = $grouptype_hash->{$gtype_str} //
+	die "unknown group type '$gtype_str'\n";
+
+    defined($ruleid) || die "undefined rule id: ERROR";
+    defined($groupid) || die "undefined group id: ERROR";
+    defined($gtype) || die "undefined group type: ERROR";
+
+    my $sth = $self->{dbh}->prepare("UPDATE RuleGroup SET MatchGroup = ? " .
+		     "WHERE Objectgroup_ID = ? and Rule_ID = ? and Grouptype = ?");
+
+    $sth->execute($value, $groupid, $ruleid, $gtype);
+
+    return 1;
+}
+
 sub load_rule {
     my ($self, $id) = @_;
 
-- 
2.30.2





  parent reply	other threads:[~2023-04-07 13:43 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-07 13:42 [pmg-devel] [PATCH pmg-api/gui/docs, proxmox-widget-toolkit] Extend rule system Leo Nunner
2023-04-07 13:42 ` [pmg-devel] [PATCH pmg-api 1/8] feature: negation: add field to database Leo Nunner
2023-04-07 13:42 ` [pmg-devel] [PATCH pmg-api 2/8] feature: negation: parse negation value into objects Leo Nunner
2023-04-07 13:42 ` [pmg-devel] [PATCH pmg-api 3/8] feature: negation: expand/implement API endpoints Leo Nunner
2023-04-07 13:42 ` [pmg-devel] [PATCH pmg-api 4/8] feature: negation: implement matching logic Leo Nunner
2023-04-11  7:35   ` Leo Nunner
2023-04-07 13:42 ` [pmg-devel] [PATCH pmg-api 5/8] feature: match groups: add field to database Leo Nunner
2023-04-07 13:42 ` Leo Nunner [this message]
2023-04-07 13:42 ` [pmg-devel] [PATCH pmg-api 7/8] feature: match groups: update API endpoints Leo Nunner
2023-04-07 13:42 ` [pmg-devel] [PATCH pmg-api 8/8] feature: match groups: implement matching logic Leo Nunner
2023-04-07 13:42 ` [pmg-devel] [PATCH pmg-gui 1/2] feature: negate objects inside rules Leo Nunner
2023-04-07 13:42 ` [pmg-devel] [PATCH pmg-gui 2/2] feature: introduce logical 'and' for rules Leo Nunner
2023-04-07 13:42 ` [pmg-devel] [PATCH pmg-docs] docs: document negation and match groups Leo Nunner
2023-04-07 13:42 ` [pmg-devel] [PATCH widget-toolkit] dark-mode: fix colour of default tree icons Leo Nunner
2023-04-11  9:52 ` [pmg-devel] [PATCH pmg-api/gui/docs, proxmox-widget-toolkit] Extend rule system Thomas Lamprecht
2023-04-11 11:04   ` Leo Nunner
2023-04-11 11:19     ` Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230407134258.199691-7-l.nunner@proxmox.com \
    --to=l.nunner@proxmox.com \
    --cc=pmg-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal