owrt/uci_network_interface.go

105 lines
2.2 KiB
Go

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
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() *Action {
uci := ni.uciClient
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
}
}
if ni.DHCP.Name != "" {
dhCreate := ni.DHCP.Create()
if dhCreate.ReturnCode != 0 {
return dhCreate
}
}
return &Action{
CommandResult: &CommandResult{
Stdout: "",
Stderr: "",
ReturnCode: 0,
},
}
}
// Save commit and relaod configuration (writes it to files !)
func (ni *UCINetworkInterface) Save() *Action {
return ni.uciClient.Save()
}
// Delete remove wifi interface from UCI Configuration
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() *Action {
uci := ni.uciClient
ni.Delete()
create := ni.Create()
if create.ReturnCode != 0 {
return create
}
return uci.Commit()
}