Moving UCI Client to structure for Redirects to
This commit is contained in:
parent
36ba15ab4e
commit
b26fa7f280
@ -6,6 +6,7 @@ import (
|
||||
|
||||
// UCIFirewallRedirect is the description of an Wireless interface (cf Openwrt doc) on top of an Wireless Device
|
||||
type UCIFirewallRedirect struct {
|
||||
UCI *UCI
|
||||
Name string
|
||||
Index int
|
||||
Src string
|
||||
@ -19,12 +20,13 @@ type UCIFirewallRedirect struct {
|
||||
}
|
||||
|
||||
// NewUCIFirewallRedirect builds a new UCIFirewallRedirect instance
|
||||
func NewUCIFirewallRedirect() *UCIFirewallRedirect {
|
||||
return &UCIFirewallRedirect{}
|
||||
func NewUCIFirewallRedirect(uci *UCI) *UCIFirewallRedirect {
|
||||
return &UCIFirewallRedirect{UCI: uci}
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
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 !)
|
||||
func (rd *UCIFirewallRedirect) Save(uci *UCI) *Action {
|
||||
func (rd *UCIFirewallRedirect) Save() *Action {
|
||||
uci := rd.UCI
|
||||
commitRes := uci.Commit()
|
||||
if commitRes.ReturnCode != 0 {
|
||||
return commitRes
|
||||
@ -70,7 +73,8 @@ func (rd *UCIFirewallRedirect) Save(uci *UCI) *Action {
|
||||
}
|
||||
|
||||
// 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)
|
||||
del := uci.Delete(toDelete)
|
||||
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
|
||||
func (rd *UCIFirewallRedirect) Update(uci *UCI) *Action {
|
||||
rd.Delete(uci)
|
||||
create := rd.Create(uci)
|
||||
func (rd *UCIFirewallRedirect) Update() *Action {
|
||||
uci := rd.UCI
|
||||
rd.Delete()
|
||||
create := rd.Create()
|
||||
if create.ReturnCode != 0 {
|
||||
return create
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ func TestFWRedirectCreate(t *testing.T) {
|
||||
exec := createMockExecutor("", "", 0)
|
||||
uci := NewUCIWithExecutor(exec, "")
|
||||
|
||||
redirect := NewUCIFirewallRedirect()
|
||||
redirect := NewUCIFirewallRedirect(uci)
|
||||
redirect.Name = redirectName
|
||||
redirect.Index = redirectIndex
|
||||
redirect.Src = redirectSrc
|
||||
@ -31,7 +31,7 @@ func TestFWRedirectCreate(t *testing.T) {
|
||||
redirect.DestIP = redirectDestIP
|
||||
redirect.DestPort = redirectDestPort
|
||||
|
||||
if redirect.Create(uci).ReturnCode != 0 {
|
||||
if redirect.Create().ReturnCode != 0 {
|
||||
t.Fatalf("UCIFirewallRedirect.Create() failed !")
|
||||
}
|
||||
}
|
||||
@ -40,7 +40,7 @@ func TestFWRedirectUpdate(t *testing.T) {
|
||||
exec := createMockExecutor("", "", 0)
|
||||
uci := NewUCIWithExecutor(exec, "")
|
||||
|
||||
redirect := NewUCIFirewallRedirect()
|
||||
redirect := NewUCIFirewallRedirect(uci)
|
||||
redirect.Name = redirectName
|
||||
redirect.Index = redirectIndex
|
||||
redirect.Src = redirectSrc
|
||||
@ -51,13 +51,13 @@ func TestFWRedirectUpdate(t *testing.T) {
|
||||
redirect.DestIP = redirectDestIP
|
||||
redirect.DestPort = redirectDestPort
|
||||
|
||||
if redirect.Create(uci).ReturnCode != 0 {
|
||||
if redirect.Create().ReturnCode != 0 {
|
||||
t.Fatalf("UCIFirewallRedirect.Create() failed !")
|
||||
}
|
||||
|
||||
redirect.Name = "NewRedirect"
|
||||
|
||||
if redirect.Update(uci).ReturnCode != 0 {
|
||||
if redirect.Update().ReturnCode != 0 {
|
||||
t.Fatalf("UCIFirewallRedirect.Update() failed !")
|
||||
}
|
||||
}
|
||||
@ -66,7 +66,7 @@ func TestFWRedirectDelete(t *testing.T) {
|
||||
exec := createMockExecutor("", "", 0)
|
||||
uci := NewUCIWithExecutor(exec, "")
|
||||
|
||||
redirect := NewUCIFirewallRedirect()
|
||||
redirect := NewUCIFirewallRedirect(uci)
|
||||
redirect.Name = redirectName
|
||||
redirect.Index = redirectIndex
|
||||
redirect.Src = redirectSrc
|
||||
@ -77,7 +77,7 @@ func TestFWRedirectDelete(t *testing.T) {
|
||||
redirect.DestIP = redirectDestIP
|
||||
redirect.DestPort = redirectDestPort
|
||||
|
||||
if redirect.Delete(uci).ReturnCode != 0 {
|
||||
if redirect.Delete().ReturnCode != 0 {
|
||||
t.Fatalf("UCIWirelessInterface.Delete() failed !")
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user