diff --git a/uci.go b/uci.go index 9fac6f8..7fc88df 100644 --- a/uci.go +++ b/uci.go @@ -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) diff --git a/uci_dhcp_conf.go b/uci_dhcp_conf.go new file mode 100644 index 0000000..1fcb788 --- /dev/null +++ b/uci_dhcp_conf.go @@ -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() +} diff --git a/uci_dhcp_conf_test.go b/uci_dhcp_conf_test.go new file mode 100644 index 0000000..c47f2f7 --- /dev/null +++ b/uci_dhcp_conf_test.go @@ -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 !") + } +} diff --git a/uci_network_interface.go b/uci_network_interface.go index 881d407..9efbbae 100644 --- a/uci_network_interface.go +++ b/uci_network_interface.go @@ -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 } diff --git a/uci_network_interface_test.go b/uci_network_interface_test.go index a7896d0..8a18533 100644 --- a/uci_network_interface_test.go +++ b/uci_network_interface_test.go @@ -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 !") } }