Moving UCI client to the structure

avoid pass UCI object to each method
This commit is contained in:
Philippe Caseiro 2018-11-07 17:09:51 +01:00
parent 62269fbc86
commit 36ba15ab4e
2 changed files with 18 additions and 14 deletions

View File

@ -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
}

View File

@ -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 !")
}
}