Adding DHCP Configuration Support for project EFS/Quid

This commit is contained in:
Philippe Caseiro 2018-10-25 10:24:45 +02:00
parent 2dc61045cf
commit 3282ef5f1b
5 changed files with 226 additions and 17 deletions

11
uci.go
View File

@ -70,6 +70,17 @@ func (u *UCI) Reload() *Action {
return &Action{cmdResult, "reload_config"}
}
// Save commit and relaod configuration (writes it to files !)
func (u *UCI) Save() *Action {
commitRes := u.Commit()
if commitRes.ReturnCode != 0 {
return commitRes
}
reload := u.Reload()
return reload
}
// Show returns the output of uci show command
func (u *UCI) Show(target string) *Action {
cmdRes := u.uciRun("show", target)

86
uci_dhcp_conf.go Normal file
View File

@ -0,0 +1,86 @@
package owrt
import "fmt"
// UCIDHCPConf describes uci dhcp server configuration for an network inteface (uci show dhcp)
type UCIDHCPConf struct {
Name string
IFName string
LeaseTime string
RangeLimit string
StartIP string
uciClient *UCI
}
// NewUCIDHCPConf builds a new UCIDHCPConf instance
func NewUCIDHCPConf(uci *UCI) *UCIDHCPConf {
return &UCIDHCPConf{
uciClient: uci,
}
}
// Create add a new entry for wifi interface in UCI Configuration
func (dh *UCIDHCPConf) Create() *Action {
uci := dh.uciClient
conf := make(map[string]string)
conf["name"] = dh.Name
conf["interface"] = dh.IFName
conf["leasetime"] = dh.LeaseTime
conf["limit"] = dh.RangeLimit
conf["ra_management"] = "1"
conf["start"] = dh.StartIP
result := uci.Set(fmt.Sprintf("dhcp.%s", dh.Name), "dhcp")
if result.ReturnCode != 0 {
return result
}
for key, value := range conf {
result := uci.Set(fmt.Sprintf("dhcp.%s.%s", dh.Name, key), value)
if result.ReturnCode != 0 {
return result
}
}
return &Action{
CommandResult: &CommandResult{
Stdout: "",
Stderr: "",
ReturnCode: 0,
},
}
}
// Save commit and relaod configuration (writes it to files !)
func (dh *UCIDHCPConf) Save() *Action {
uci := dh.uciClient
commitRes := uci.Commit()
if commitRes.ReturnCode != 0 {
return commitRes
}
reload := uci.Reload()
return reload
}
// Delete remove wifi interface from UCI Configuration
func (dh *UCIDHCPConf) Delete() *Action {
uci := dh.uciClient
toDelete := fmt.Sprintf("network.%s", dh.Name)
del := uci.Delete(toDelete)
if del.ReturnCode != 0 {
return del
}
return uci.Commit()
}
// Update add a new entry for wifi interface in UCI Configuration
func (dh *UCIDHCPConf) Update() *Action {
uci := dh.uciClient
dh.Delete()
create := dh.Create()
if create.ReturnCode != 0 {
return create
}
return uci.Commit()
}

94
uci_dhcp_conf_test.go Normal file
View File

@ -0,0 +1,94 @@
package owrt
import (
"testing"
)
const (
ifNameDHCP = "cadoles"
ifProtoDHCP = "static"
ifIFNameDHCP = "cdl-fake"
ifIPAddrDHCP = "192.168.59.1"
ifNetmaskDHCP = "255.255.255.0"
ifDNSDHCP = "192.168.59.1"
ifIFTypeDHCP = "bridge"
ifMetricDHCP = "9000"
ifRangeLimitDHCP = "10"
ifStartIPDHCP = "192.168.59.10"
)
func TestNetCreateWithDHCP(t *testing.T) {
exec := createMockExecutor("", "", 0)
uci := NewUCIWithExecutor(exec)
iface := NewUCINetworkInterface(uci)
iface.Name = ifNameDHCP
iface.Proto = ifProtoDHCP
iface.IFName = ifIFNameDHCP
iface.IPAddr = ifIPAddrDHCP
iface.Netmask = ifNetmaskDHCP
iface.DNS = ifDNSDHCP
iface.IFType = ifIFTypeDHCP
iface.Metric = ifMetricDHCP
iface.DHCP.Name = ifNameDHCP
iface.DHCP.LeaseTime = ifNameDHCP
iface.DHCP.RangeLimit = ifRangeLimitDHCP
iface.DHCP.StartIP = ifStartIPDHCP
if iface.Create().ReturnCode != 0 {
t.Fatalf("UCINetworkInterface.Create() failed !")
}
}
func TestNetUpdateWithDHCP(t *testing.T) {
exec := createMockExecutor("", "", 0)
uci := NewUCIWithExecutor(exec)
iface := NewUCINetworkInterface(uci)
iface.Name = ifNameDHCP
iface.Proto = ifProtoDHCP
iface.IFName = ifIFNameDHCP
iface.IPAddr = ifIPAddrDHCP
iface.Netmask = ifNetmaskDHCP
iface.DNS = ifDNSDHCP
iface.IFType = ifIFTypeDHCP
iface.Metric = ifMetricDHCP
iface.DHCP.Name = ifNameDHCP
iface.DHCP.LeaseTime = ifNameDHCP
iface.DHCP.RangeLimit = ifRangeLimitDHCP
iface.DHCP.StartIP = ifStartIPDHCP
if iface.Create().ReturnCode != 0 {
t.Fatalf("UCINetworkInterface.Create() failed !")
}
iface.Metric = "1000"
iface.DHCP.RangeLimit = "100"
if iface.Update().ReturnCode != 0 {
t.Fatalf("UCINetworkInterface.Update() failed !")
}
}
func TestNetDeleteWithDHCP(t *testing.T) {
exec := createMockExecutor("", "", 0)
uci := NewUCIWithExecutor(exec)
iface := NewUCINetworkInterface(uci)
iface.Name = ifNameDHCP
iface.Proto = ifProtoDHCP
iface.IFName = ifIFNameDHCP
iface.IPAddr = ifIPAddrDHCP
iface.Netmask = ifNetmaskDHCP
iface.DNS = ifDNSDHCP
iface.IFType = ifIFTypeDHCP
iface.Metric = ifMetricDHCP
iface.DHCP.Name = ifNameDHCP
iface.DHCP.LeaseTime = ifNameDHCP
iface.DHCP.RangeLimit = ifRangeLimitDHCP
iface.DHCP.StartIP = ifStartIPDHCP
if iface.Delete().ReturnCode != 0 {
t.Fatalf("UCINetworkInterface.Delete() failed !")
}
}

View File

@ -13,17 +13,22 @@ type UCINetworkInterface struct {
DNS string
IFType string
Metric string
DHCP *UCIDHCPConf
}
// NewUCINetworkInterface builds a new UCIWirelessConf instance
func NewUCINetworkInterface(uci *UCI) *UCINetworkInterface {
return &UCINetworkInterface{
uciClient: uci,
DHCP: &UCIDHCPConf{
uciClient: uci,
},
}
}
// Create add a new entry for wifi interface in UCI Configuration
func (ni *UCINetworkInterface) Create(uci *UCI) *Action {
func (ni *UCINetworkInterface) Create() *Action {
uci := ni.uciClient
conf := make(map[string]string)
conf["proto"] = ni.Proto
@ -45,6 +50,13 @@ func (ni *UCINetworkInterface) Create(uci *UCI) *Action {
}
}
if ni.DHCP.Name != "" {
dhCreate := ni.DHCP.Create()
if dhCreate.ReturnCode != 0 {
return dhCreate
}
}
return &Action{
CommandResult: &CommandResult{
Stdout: "",
@ -55,30 +67,36 @@ func (ni *UCINetworkInterface) Create(uci *UCI) *Action {
}
// Save commit and relaod configuration (writes it to files !)
func (ni *UCINetworkInterface) Save(uci *UCI) *Action {
commitRes := uci.Commit()
if commitRes.ReturnCode != 0 {
return commitRes
}
reload := uci.Reload()
return reload
func (ni *UCINetworkInterface) Save() *Action {
return ni.uciClient.Save()
}
// Delete remove wifi interface from UCI Configuration
func (ni *UCINetworkInterface) Delete(uci *UCI) *Action {
func (ni *UCINetworkInterface) Delete() *Action {
uci := ni.uciClient
if ni.DHCP.Name != "" {
toDelete := fmt.Sprintf("dhcp.%s", ni.Name)
del := uci.Delete(toDelete)
if del.ReturnCode != 0 {
return del
}
}
toDelete := fmt.Sprintf("network.%s", ni.Name)
del := uci.Delete(toDelete)
if del.ReturnCode != 0 {
return del
}
return uci.Commit()
}
// Update add a new entry for wifi interface in UCI Configuration
func (ni *UCINetworkInterface) Update(uci *UCI) *Action {
ni.Delete(uci)
create := ni.Create(uci)
func (ni *UCINetworkInterface) Update() *Action {
uci := ni.uciClient
ni.Delete()
create := ni.Create()
if create.ReturnCode != 0 {
return create
}

View File

@ -29,7 +29,7 @@ func TestNetCreate(t *testing.T) {
iface.IFType = ifIFType
iface.Metric = ifMetric
if iface.Create(uci).ReturnCode != 0 {
if iface.Create().ReturnCode != 0 {
t.Fatalf("UCINetworkInterface.Create() failed !")
}
}
@ -48,13 +48,13 @@ func TestNetUpdate(t *testing.T) {
iface.IFType = ifIFType
iface.Metric = ifMetric
if iface.Create(uci).ReturnCode != 0 {
if iface.Create().ReturnCode != 0 {
t.Fatalf("UCINetworkInterface.Create() failed !")
}
iface.Metric = "1000"
if iface.Update(uci).ReturnCode != 0 {
if iface.Update().ReturnCode != 0 {
t.Fatalf("UCINetworkInterface.Update() failed !")
}
}
@ -73,7 +73,7 @@ func TestNetDelete(t *testing.T) {
iface.IFType = ifIFType
iface.Metric = ifMetric
if iface.Delete(uci).ReturnCode != 0 {
if iface.Delete().ReturnCode != 0 {
t.Fatalf("UCINetworkInterface.Delete() failed !")
}
}