Rework LuCI build system
[project/luci.git] / modules / luci-mod-admin-full / luasrc / model / cbi / admin_network / iface_add.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2009-2010 Jo-Philipp Wich <xm@subsignal.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13
14 ]]--
15
16 local nw  = require "luci.model.network".init()
17 local fw  = require "luci.model.firewall".init()
18 local utl = require "luci.util"
19 local uci = require "luci.model.uci".cursor()
20
21 m = SimpleForm("network", translate("Create Interface"))
22 m.redirect = luci.dispatcher.build_url("admin/network/network")
23 m.reset = false
24
25 newnet = m:field(Value, "_netname", translate("Name of the new interface"),
26         translate("The allowed characters are: <code>A-Z</code>, <code>a-z</code>, " ..
27                 "<code>0-9</code> and <code>_</code>"
28         ))
29
30 newnet:depends("_attach", "")
31 newnet.default = arg[1] and "net_" .. arg[1]:gsub("[^%w_]+", "_")
32 newnet.datatype = "uciname"
33
34 newproto = m:field(ListValue, "_netproto", translate("Protocol of the new interface"))
35
36 netbridge = m:field(Flag, "_bridge", translate("Create a bridge over multiple interfaces"))
37
38
39 sifname = m:field(Value, "_ifname", translate("Cover the following interface"))
40
41 sifname.widget = "radio"
42 sifname.template  = "cbi/network_ifacelist"
43 sifname.nobridges = true
44
45
46 mifname = m:field(Value, "_ifnames", translate("Cover the following interfaces"))
47
48 mifname.widget = "checkbox"
49 mifname.template  = "cbi/network_ifacelist"
50 mifname.nobridges = true
51
52
53 local _, p
54 for _, p in ipairs(nw:get_protocols()) do
55         if p:is_installed() then
56                 newproto:value(p:proto(), p:get_i18n())
57                 if not p:is_virtual()  then netbridge:depends("_netproto", p:proto()) end
58                 if not p:is_floating() then
59                         sifname:depends({ _bridge = "",  _netproto = p:proto()})
60                         mifname:depends({ _bridge = "1", _netproto = p:proto()})
61                 end
62         end
63 end
64
65 function newproto.validate(self, value, section)
66         local name = newnet:formvalue(section)
67         if not name or #name == 0 then
68                 newnet:add_error(section, translate("No network name specified"))
69         elseif m:get(name) then
70                 newnet:add_error(section, translate("The given network name is not unique"))
71         end
72
73         local proto = nw:get_protocol(value)
74         if proto and not proto:is_floating() then
75                 local br = (netbridge:formvalue(section) == "1")
76                 local ifn = br and mifname:formvalue(section) or sifname:formvalue(section)
77                 for ifn in utl.imatch(ifn) do
78                         return value
79                 end
80                 return nil, translate("The selected protocol needs a device assigned")
81         end
82         return value
83 end
84
85 function newproto.write(self, section, value)
86         local name = newnet:formvalue(section)
87         if name and #name > 0 then
88                 local br = (netbridge:formvalue(section) == "1") and "bridge" or nil
89                 local net = nw:add_network(name, { proto = value, type = br })
90                 if net then
91                         local ifn
92                         for ifn in utl.imatch(
93                                 br and mifname:formvalue(section) or sifname:formvalue(section)
94                         ) do
95                                 net:add_interface(ifn)
96                         end
97                         nw:save("network")
98                         nw:save("wireless")
99                 end
100                 luci.http.redirect(luci.dispatcher.build_url("admin/network/network", name))
101         end
102 end
103
104 return m