[pmg-devel] [PATCH pmg-api] pmgdb: dump: replace active flag with rules enum

Stoiko Ivanov s.ivanov at proxmox.com
Tue Feb 27 15:22:03 CET 2024


this addresses the odd/unintuitive behavior of `pmgdb dump --active 0`
which currently prints all rules, why printing only the inactive ones
might be expected.

Signed-off-by: Stoiko Ivanov <s.ivanov at proxmox.com>
---
tested by running all 4 variants (active, inactive, all, no rule argument)
and diffing the resulting output)
 src/PMG/CLI/pmgdb.pm | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/src/PMG/CLI/pmgdb.pm b/src/PMG/CLI/pmgdb.pm
index 0ac3f82..cc9d000 100644
--- a/src/PMG/CLI/pmgdb.pm
+++ b/src/PMG/CLI/pmgdb.pm
@@ -39,11 +39,13 @@ sub print_objects {
 }
 
 sub print_rule {
-    my ($ruledb, $rule, $active_only) = @_;
+    my ($ruledb, $rule, $rule_status) = @_;
 
     $ruledb->load_rule_attributes($rule);
 
-    return if !$rule->{active} && $active_only;
+    return if !$rule->{active} && $rule_status eq 'active';
+    return if $rule->{active} && $rule_status eq 'inactive';
+
     my $direction = {
 	0 => 'in',
 	1 => 'out',
@@ -114,11 +116,12 @@ __PACKAGE__->register_method ({
     parameters => {
 	additionalProperties => 0,
 	properties => {
-	    active => {
-		type => 'boolean',
-		description => "Print only active rules",
+	    rules => {
+		description => "Which rules should be printed",
+		type => 'string',
+		enum => [qw(all active inactive)],
+		default => 'all',
 		optional => 1,
-		default => 0,
 	    },
 	},
     },
@@ -126,13 +129,14 @@ __PACKAGE__->register_method ({
     code => sub {
 	my ($param) = @_;
 
+	my $rule_status = $param->{rules} // '';
 	my $dbh = PMG::DBTools::open_ruledb("Proxmox_ruledb");
 	my $ruledb = PMG::RuleDB->new($dbh);
 
 	my $rules = $ruledb->load_rules();
 
 	foreach my $rule (@$rules) {
-	    print_rule($ruledb, $rule, $param->{active});
+	    print_rule($ruledb, $rule, $rule_status);
 	}
 
 	$ruledb->close();
-- 
2.39.2





More information about the pmg-devel mailing list