summaryrefslogtreecommitdiffstats
path: root/interfaces-controller.lua
blob: da963dd7872ad2d1eed042023de8c4cf05c4af12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
-- the interfaces  controller

module (..., package.seeall)

default_action = "read"

status = function (self)
	return self.model.get_status()
end

read = function (self)
	local cmdresult
	if self.sessiondata.cmdresult then
		cmdresult = self.sessiondata.cmdresult
	end
	self.sessiondata.cmdresult = nil

	local retval = self.model.get_all_interfaces()
	retval.value.cmdresult = cmdresult
	return retval
end

-- Accepts form info
-- Returns a cfe object (the form)
update = function(self)
	local name = self.clientdata.name or ""

	-- If interface is not found, return to list
	local data = self.model.get_iface_by_name ( name )
	if data.value.name.value == "" then
		redirect(self)
	end

	-- If the "Save" button was used, then attempt to do the update
	if self.clientdata.Save then
		-- update the iface info the form gave us
		for k,v in pairs (data.value) do
			if v.type == "boolean" then
				if self.clientdata[k] then
					v.value = true
				else
					v.value = false
				end
			elseif self.clientdata[k] then 
				v.value = self.clientdata[k]
			end
		end
		
		data = self.model.update_iface ( data )
	
		-- If validation worked then return to list
		if not data.errtxt then
			data.descr = "Updated Interface"
		end
	end

	-- If we reach this point in the function, then we are providing a form
	-- for the user to edit (either first time in, or validation failed)
	data.type = "form"
	data.option = "Save"
	data.label = "Update Interface"

	return data
end

delete = function(self)
	local name = self.clientdata.name or ""

	-- Attempt to delete the iface
	local cmdresult = self.model.delete_iface_by_name (name)
	self.sessiondata.cmdresult = cmdresult
	redirect_to_referrer(self)
end

ifup = function(self)
	local name = self.clientdata.name or ""

	-- Attempt to delete the iface
	local cmdresult = self.model.ifup_by_name (name)
	self.sessiondata.cmdresult = cmdresult
	redirect_to_referrer(self)
end

ifdown = function(self)
	local name = self.clientdata.name or ""

	-- Attempt to delete the iface
	local cmdresult = self.model.ifdown_by_name (name)
	self.sessiondata.cmdresult = cmdresult
	redirect_to_referrer(self)
end

create = function(self)
	local data = self.model.get_iface (self.clientdata.family, self.clientdata.method)

	-- If the "Create" button was used, then attempt to do the insert
	if self.clientdata.Create then
		for k,v in pairs (data.value) do
			if self.clientdata[k] then 
				v.value = self.clientdata[k]
			end
		end
		
		data = self.model.create_iface ( data )
		if not data.errtxt then
			data.descr = "Created Interface"
			redirect(self, "update?name="..data.value.name.value)
		elseif data.value.method and data.value.method.value == "" then
			data.value.method.errtxt = "Must define method"
		end
	end
	
	if data.value.family and data.value.family.value == "" then
		data.value.family.errtxt = "Must define family"
	end

	-- If we reach this point in the function, we are providing a form
	data.type = "form"
	data.option = "Create"
	data.label = "Create Interface"

	return data
end

editintfile = function(self)
	local data = self.model.get_file()

	if self.clientdata.Save then
		data.value.filecontent.value = self.clientdata.filecontent
		data = self.model.write_file(data)
		data.descr = "Saved file"
	end

	data.type = "form"
	data.option = "Save"
	data.label = "Edit Interfaces file"

	return data
end