c3bf664bd0235980bac81a930398c546a51ee1f5
[project/luci.git] / modules / 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
23 newnet = m:field(Value, "_netname", translate("Name of the new interface"),
24         translate("The allowed characters are: <code>A-Z</code>, <code>a-z</code>, " ..
25                 "<code>0-9</code> and <code>_</code>"
26         ))
27
28 newnet:depends("_attach", "")
29 newnet.default = arg[1] and "net_" .. arg[1]:gsub("[^%w_]+", "_")
30 newnet.datatype = "uciname"
31
32 netbridge = m:field(Flag, "_bridge", translate("Create a bridge over multiple interfaces"))
33
34
35 sifname = m:field(Value, "_ifname", translate("Cover the following interface"),
36         translate("Note: If you choose an interface here which is part of another network, it will be moved into this network."))
37
38 sifname.widget = "radio"
39 sifname.template = "cbi/network_ifacelist"
40 sifname.nobridges = true
41 sifname:depends("_bridge", "")
42
43
44 mifname = m:field(Value, "_ifnames", translate("Cover the following interfaces"),
45         translate("Note: If you choose an interface here which is part of another network, it will be moved into this network."))
46
47 mifname.widget = "checkbox"
48 mifname.template = "cbi/network_ifacelist"
49 mifname.nobridges = true
50 mifname:depends("_bridge", "1")
51
52 function newnet.write(self, section, value)
53         local bridge = netbridge:formvalue(section) == "1"
54         local ifaces = bridge and mifname:formvalue(section) or sifname:formvalue(section)
55
56         local nn = nw:add_network(value, { proto = "none" })
57         if nn then
58                 if bridge then
59                         nn:set("type", "bridge")
60                 end
61
62                 local iface
63                 for iface in utl.imatch(ifaces) do
64                         nn:add_interface(iface)
65                         if not bridge then
66                                 break
67                         end
68                 end
69
70                 nw:save("network")
71                 nw:save("wireless")
72
73                 luci.http.redirect(luci.dispatcher.build_url("admin/network/network", nn:name()))
74         end
75 end
76
77 return m