From 36ba15ab4e981082a9c137b505aa65ba84c1eabb Mon Sep 17 00:00:00 2001 From: Philippe Caseiro Date: Wed, 7 Nov 2018 17:09:51 +0100 Subject: [PATCH] Moving UCI client to the structure avoid pass UCI object to each method --- uci_firewall_rules.go | 18 +++++++++++------- uci_firewall_rules_test.go | 14 +++++++------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/uci_firewall_rules.go b/uci_firewall_rules.go index 643a0df..8210221 100644 --- a/uci_firewall_rules.go +++ b/uci_firewall_rules.go @@ -6,6 +6,7 @@ import ( // UCIFirewallRule is the description of an Wireless interface (cf Openwrt doc) on top of an Wireless Device type UCIFirewallRule struct { + UCI *UCI Name string Index int Src string @@ -16,12 +17,13 @@ type UCIFirewallRule struct { } // NewUCIFirewallRule builds a new UCIFirewallRule instance -func NewUCIFirewallRule() *UCIFirewallRule { - return &UCIFirewallRule{} +func NewUCIFirewallRule(uci *UCI) *UCIFirewallRule { + return &UCIFirewallRule{UCI: uci} } // Create add a new firewall rule in UCI Configuration -func (fw *UCIFirewallRule) Create(uci *UCI) *Action { +func (fw *UCIFirewallRule) Create() *Action { + uci := fw.UCI confPrefix := fmt.Sprintf("firewall.@rule[%d]", fw.Index) conf := make(map[string][]string) @@ -63,7 +65,8 @@ func (fw *UCIFirewallRule) Save(uci *UCI) *Action { } // Delete remove wifi interface from UCI Configuration -func (fw *UCIFirewallRule) Delete(uci *UCI) *Action { +func (fw *UCIFirewallRule) Delete() *Action { + uci := fw.UCI toDelete := fmt.Sprintf("firewall.@rule[%d]", fw.Index) del := uci.Delete(toDelete) if del.ReturnCode != 0 { @@ -73,9 +76,10 @@ func (fw *UCIFirewallRule) Delete(uci *UCI) *Action { } // Update add a new entry for wifi interface in UCI Configuration -func (fw *UCIFirewallRule) Update(uci *UCI) *Action { - fw.Delete(uci) - create := fw.Create(uci) +func (fw *UCIFirewallRule) Update() *Action { + uci := fw.UCI + fw.Delete() + create := fw.Create() if create.ReturnCode != 0 { return create } diff --git a/uci_firewall_rules_test.go b/uci_firewall_rules_test.go index b52a84d..00e9475 100644 --- a/uci_firewall_rules_test.go +++ b/uci_firewall_rules_test.go @@ -18,7 +18,7 @@ func TestFWRuleCreate(t *testing.T) { exec := createMockExecutor("", "", 0) uci := NewUCIWithExecutor(exec, "") - rule := NewUCIFirewallRule() + rule := NewUCIFirewallRule(uci) rule.Name = ruleName rule.Index = ruleIndex rule.Src = ruleSrc @@ -27,7 +27,7 @@ func TestFWRuleCreate(t *testing.T) { rule.DestPort = ruleDestPort rule.SourcePort = ruleSourcePort - if rule.Create(uci).ReturnCode != 0 { + if rule.Create().ReturnCode != 0 { t.Fatalf("UCIFirewallRule.Create() failed !") } } @@ -36,7 +36,7 @@ func TestFWRuleUpdate(t *testing.T) { exec := createMockExecutor("", "", 0) uci := NewUCIWithExecutor(exec, "") - rule := NewUCIFirewallRule() + rule := NewUCIFirewallRule(uci) rule.Name = ruleName rule.Index = ruleIndex rule.Src = ruleSrc @@ -45,13 +45,13 @@ func TestFWRuleUpdate(t *testing.T) { rule.DestPort = ruleDestPort rule.SourcePort = ruleSourcePort - if rule.Create(uci).ReturnCode != 0 { + if rule.Create().ReturnCode != 0 { t.Fatalf("UCIFirewallRule.Create() failed !") } rule.Name = "Tutu" - if rule.Update(uci).ReturnCode != 0 { + if rule.Update().ReturnCode != 0 { t.Fatalf("UCIFirewallRule.Update() failed !") } } @@ -60,7 +60,7 @@ func TestFWRuleDelete(t *testing.T) { exec := createMockExecutor("", "", 0) uci := NewUCIWithExecutor(exec, "") - rule := NewUCIFirewallRule() + rule := NewUCIFirewallRule(uci) rule.Name = ruleName rule.Index = ruleIndex rule.Src = ruleSrc @@ -69,7 +69,7 @@ func TestFWRuleDelete(t *testing.T) { rule.DestPort = ruleDestPort rule.SourcePort = ruleSourcePort - if rule.Delete(uci).ReturnCode != 0 { + if rule.Delete().ReturnCode != 0 { t.Fatalf("UCIWirelessInterface.Delete() failed !") } }