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