owrt/uci_dhcp_conf.go

97 lines
2.1 KiB
Go

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 {
if value == "" {
return &Action{
CommandResult: &CommandResult{
Stdout: "",
Stderr: fmt.Sprintf("dhcp.%s can't be empty", key),
ReturnCode: 100,
},
Command: "Empty value error",
}
}
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()
}