Adding uci network manipulation support

We need to be able to create new network interfaces ...
This commit is contained in:
Philippe Caseiro 2018-10-24 16:55:47 +02:00
parent 7a6dadd3f8
commit 2dc61045cf
2 changed files with 165 additions and 0 deletions

86
uci_network_interface.go Normal file
View File

@ -0,0 +1,86 @@
package owrt
import "fmt"
// UCINetworkInterface describes uci network inteface (uci show network)
type UCINetworkInterface struct {
uciClient *UCI
Name string
Proto string
IFName string
IPAddr string
Netmask string
DNS string
IFType string
Metric string
}
// NewUCINetworkInterface builds a new UCIWirelessConf instance
func NewUCINetworkInterface(uci *UCI) *UCINetworkInterface {
return &UCINetworkInterface{
uciClient: uci,
}
}
// Create add a new entry for wifi interface in UCI Configuration
func (ni *UCINetworkInterface) Create(uci *UCI) *Action {
conf := make(map[string]string)
conf["proto"] = ni.Proto
conf["ifname"] = ni.IFName
conf["ipaddr"] = ni.IPAddr
conf["netmask"] = ni.Netmask
conf["dns"] = ni.DNS
conf["type"] = ni.IFType
conf["metric"] = ni.Metric
result := uci.Set(fmt.Sprintf("network.%s", ni.Name), "interface")
if result.ReturnCode != 0 {
return result
}
for key, value := range conf {
result := uci.Set(fmt.Sprintf("network.%s.%s", ni.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 (ni *UCINetworkInterface) Save(uci *UCI) *Action {
commitRes := uci.Commit()
if commitRes.ReturnCode != 0 {
return commitRes
}
reload := uci.Reload()
return reload
}
// Delete remove wifi interface from UCI Configuration
func (ni *UCINetworkInterface) Delete(uci *UCI) *Action {
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)
if create.ReturnCode != 0 {
return create
}
return uci.Commit()
}

View File

@ -0,0 +1,79 @@
package owrt
import (
"testing"
)
const (
ifName = "cadoles"
ifProto = "static"
ifIFName = "cdl-fake"
ifIPAddr = "192.168.59.1"
ifNetmask = "255.255.255.0"
ifDNS = "192.168.59.1"
ifIFType = "bridge"
ifMetric = "9000"
)
func TestNetCreate(t *testing.T) {
exec := createMockExecutor("", "", 0)
uci := NewUCIWithExecutor(exec)
iface := NewUCINetworkInterface(uci)
iface.Name = ifName
iface.Proto = ifProto
iface.IFName = ifIFName
iface.IPAddr = ifIPAddr
iface.Netmask = ifNetmask
iface.DNS = ifDNS
iface.IFType = ifIFType
iface.Metric = ifMetric
if iface.Create(uci).ReturnCode != 0 {
t.Fatalf("UCINetworkInterface.Create() failed !")
}
}
func TestNetUpdate(t *testing.T) {
exec := createMockExecutor("", "", 0)
uci := NewUCIWithExecutor(exec)
iface := NewUCINetworkInterface(uci)
iface.Name = ifName
iface.Proto = ifProto
iface.IFName = ifIFName
iface.IPAddr = ifIPAddr
iface.Netmask = ifNetmask
iface.DNS = ifDNS
iface.IFType = ifIFType
iface.Metric = ifMetric
if iface.Create(uci).ReturnCode != 0 {
t.Fatalf("UCINetworkInterface.Create() failed !")
}
iface.Metric = "1000"
if iface.Update(uci).ReturnCode != 0 {
t.Fatalf("UCINetworkInterface.Update() failed !")
}
}
func TestNetDelete(t *testing.T) {
exec := createMockExecutor("", "", 0)
uci := NewUCIWithExecutor(exec)
iface := NewUCINetworkInterface(uci)
iface.Name = ifName
iface.Proto = ifProto
iface.IFName = ifIFName
iface.IPAddr = ifIPAddr
iface.Netmask = ifNetmask
iface.DNS = ifDNS
iface.IFType = ifIFType
iface.Metric = ifMetric
if iface.Delete(uci).ReturnCode != 0 {
t.Fatalf("UCINetworkInterface.Delete() failed !")
}
}