Moving UCI Client to structure for Redirects to

This commit is contained in:
Philippe Caseiro 2018-11-07 17:17:57 +01:00
parent 36ba15ab4e
commit b26fa7f280
2 changed files with 20 additions and 15 deletions

View File

@ -6,6 +6,7 @@ import (
// UCIFirewallRedirect is the description of an Wireless interface (cf Openwrt doc) on top of an Wireless Device // UCIFirewallRedirect is the description of an Wireless interface (cf Openwrt doc) on top of an Wireless Device
type UCIFirewallRedirect struct { type UCIFirewallRedirect struct {
UCI *UCI
Name string Name string
Index int Index int
Src string Src string
@ -19,12 +20,13 @@ type UCIFirewallRedirect struct {
} }
// NewUCIFirewallRedirect builds a new UCIFirewallRedirect instance // NewUCIFirewallRedirect builds a new UCIFirewallRedirect instance
func NewUCIFirewallRedirect() *UCIFirewallRedirect { func NewUCIFirewallRedirect(uci *UCI) *UCIFirewallRedirect {
return &UCIFirewallRedirect{} return &UCIFirewallRedirect{UCI: uci}
} }
// Create add a new firewall rule in UCI Configuration // Create add a new firewall rule in UCI Configuration
func (rd *UCIFirewallRedirect) Create(uci *UCI) *Action { func (rd *UCIFirewallRedirect) Create() *Action {
uci := rd.UCI
confPrefix := fmt.Sprintf("firewall.@redirect[%d]", rd.Index) confPrefix := fmt.Sprintf("firewall.@redirect[%d]", rd.Index)
conf := make(map[string][]string) conf := make(map[string][]string)
@ -59,7 +61,8 @@ func (rd *UCIFirewallRedirect) Create(uci *UCI) *Action {
} }
// Save commit and relaod configuration (writes it to files !) // Save commit and relaod configuration (writes it to files !)
func (rd *UCIFirewallRedirect) Save(uci *UCI) *Action { func (rd *UCIFirewallRedirect) Save() *Action {
uci := rd.UCI
commitRes := uci.Commit() commitRes := uci.Commit()
if commitRes.ReturnCode != 0 { if commitRes.ReturnCode != 0 {
return commitRes return commitRes
@ -70,7 +73,8 @@ func (rd *UCIFirewallRedirect) Save(uci *UCI) *Action {
} }
// Delete remove wifi interface from UCI Configuration // Delete remove wifi interface from UCI Configuration
func (rd *UCIFirewallRedirect) Delete(uci *UCI) *Action { func (rd *UCIFirewallRedirect) Delete() *Action {
uci := rd.UCI
toDelete := fmt.Sprintf("firewall.@redirect[%d]", rd.Index) toDelete := fmt.Sprintf("firewall.@redirect[%d]", rd.Index)
del := uci.Delete(toDelete) del := uci.Delete(toDelete)
if del.ReturnCode != 0 { if del.ReturnCode != 0 {
@ -80,9 +84,10 @@ func (rd *UCIFirewallRedirect) Delete(uci *UCI) *Action {
} }
// Update add a new entry for wifi interface in UCI Configuration // Update add a new entry for wifi interface in UCI Configuration
func (rd *UCIFirewallRedirect) Update(uci *UCI) *Action { func (rd *UCIFirewallRedirect) Update() *Action {
rd.Delete(uci) uci := rd.UCI
create := rd.Create(uci) rd.Delete()
create := rd.Create()
if create.ReturnCode != 0 { if create.ReturnCode != 0 {
return create return create
} }

View File

@ -20,7 +20,7 @@ func TestFWRedirectCreate(t *testing.T) {
exec := createMockExecutor("", "", 0) exec := createMockExecutor("", "", 0)
uci := NewUCIWithExecutor(exec, "") uci := NewUCIWithExecutor(exec, "")
redirect := NewUCIFirewallRedirect() redirect := NewUCIFirewallRedirect(uci)
redirect.Name = redirectName redirect.Name = redirectName
redirect.Index = redirectIndex redirect.Index = redirectIndex
redirect.Src = redirectSrc redirect.Src = redirectSrc
@ -31,7 +31,7 @@ func TestFWRedirectCreate(t *testing.T) {
redirect.DestIP = redirectDestIP redirect.DestIP = redirectDestIP
redirect.DestPort = redirectDestPort redirect.DestPort = redirectDestPort
if redirect.Create(uci).ReturnCode != 0 { if redirect.Create().ReturnCode != 0 {
t.Fatalf("UCIFirewallRedirect.Create() failed !") t.Fatalf("UCIFirewallRedirect.Create() failed !")
} }
} }
@ -40,7 +40,7 @@ func TestFWRedirectUpdate(t *testing.T) {
exec := createMockExecutor("", "", 0) exec := createMockExecutor("", "", 0)
uci := NewUCIWithExecutor(exec, "") uci := NewUCIWithExecutor(exec, "")
redirect := NewUCIFirewallRedirect() redirect := NewUCIFirewallRedirect(uci)
redirect.Name = redirectName redirect.Name = redirectName
redirect.Index = redirectIndex redirect.Index = redirectIndex
redirect.Src = redirectSrc redirect.Src = redirectSrc
@ -51,13 +51,13 @@ func TestFWRedirectUpdate(t *testing.T) {
redirect.DestIP = redirectDestIP redirect.DestIP = redirectDestIP
redirect.DestPort = redirectDestPort redirect.DestPort = redirectDestPort
if redirect.Create(uci).ReturnCode != 0 { if redirect.Create().ReturnCode != 0 {
t.Fatalf("UCIFirewallRedirect.Create() failed !") t.Fatalf("UCIFirewallRedirect.Create() failed !")
} }
redirect.Name = "NewRedirect" redirect.Name = "NewRedirect"
if redirect.Update(uci).ReturnCode != 0 { if redirect.Update().ReturnCode != 0 {
t.Fatalf("UCIFirewallRedirect.Update() failed !") t.Fatalf("UCIFirewallRedirect.Update() failed !")
} }
} }
@ -66,7 +66,7 @@ func TestFWRedirectDelete(t *testing.T) {
exec := createMockExecutor("", "", 0) exec := createMockExecutor("", "", 0)
uci := NewUCIWithExecutor(exec, "") uci := NewUCIWithExecutor(exec, "")
redirect := NewUCIFirewallRedirect() redirect := NewUCIFirewallRedirect(uci)
redirect.Name = redirectName redirect.Name = redirectName
redirect.Index = redirectIndex redirect.Index = redirectIndex
redirect.Src = redirectSrc redirect.Src = redirectSrc
@ -77,7 +77,7 @@ func TestFWRedirectDelete(t *testing.T) {
redirect.DestIP = redirectDestIP redirect.DestIP = redirectDestIP
redirect.DestPort = redirectDestPort redirect.DestPort = redirectDestPort
if redirect.Delete(uci).ReturnCode != 0 { if redirect.Delete().ReturnCode != 0 {
t.Fatalf("UCIWirelessInterface.Delete() failed !") t.Fatalf("UCIWirelessInterface.Delete() failed !")
} }
} }