Return proper errors if config is read-only
[cascardo/ipsilon.git] / ipsilon / admin / common.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2014  Simo Sorce <simo@redhat.com>
4 #
5 # see file 'COPYING' for use and warranty information
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 import cherrypy
21 from ipsilon.util.page import Page
22 from ipsilon.util.page import admin_protect
23 from ipsilon.util import config as pconfig
24
25
26 class AdminError(Exception):
27     def __init__(self, message):
28         super(AdminError, self).__init__(message)
29         self.message = message
30
31     def __str__(self):
32         return str(self.message)
33
34
35 class AdminPage(Page):
36
37     def __init__(self, *args, **kwargs):
38         super(AdminPage, self).__init__(*args, **kwargs)
39         self.default_headers.update({
40             'Cache-Control': 'no-cache, must-revalidate',
41             'Pragma': 'no-cache',
42             'Expires': 'Thu, 01 Dec 1994 16:00:00 GMT',
43         })
44         self.auth_protect = True
45
46
47 class AdminPluginConfig(AdminPage):
48
49     def __init__(self, po, site, parent):
50         super(AdminPluginConfig, self).__init__(site, form=True)
51         self._po = po
52         self.title = '%s plugin' % po.name
53         self.url = '%s/%s' % (parent.url, po.name)
54         self.facility = parent.facility
55         self.menu = [parent]
56         self.back = parent.url
57
58     def root_with_msg(self, message=None, message_type=None):
59         return self._template('admin/plugin_config.html', title=self.title,
60                               menu=self.menu, action=self.url, back=self.back,
61                               message=message, message_type=message_type,
62                               name='admin_%s_%s_form' % (self.facility,
63                                                          self._po.name),
64                               config=self._po.get_config_obj())
65
66     @admin_protect
67     def GET(self, *args, **kwargs):
68         return self.root_with_msg()
69
70     @admin_protect
71     def POST(self, *args, **kwargs):
72
73         if self._po.is_readonly:
74             return self.root_with_msg(
75                 message="Configuration is marked Read-Only",
76                 message_type="warning")
77
78         message = "Nothing was modified."
79         message_type = "info"
80         new_db_values = dict()
81
82         conf = self._po.get_config_obj()
83
84         for name, option in conf.iteritems():
85             if name in kwargs:
86                 value = kwargs[name]
87                 if isinstance(option, pconfig.List):
88                     value = [x.strip() for x in value.split('\n')]
89                 elif isinstance(option, pconfig.Condition):
90                     value = True
91             else:
92                 if isinstance(option, pconfig.Condition):
93                     value = False
94                 elif isinstance(option, pconfig.Choice):
95                     value = list()
96                     for a in option.get_allowed():
97                         aname = '%s_%s' % (name, a)
98                         if aname in kwargs:
99                             value.append(a)
100                 else:
101                     continue
102
103             if value != option.get_value():
104                 cherrypy.log.error("Storing [%s]: %s = %s" %
105                                    (self._po.name, name, value))
106             option.set_value(value)
107             new_db_values[name] = option.export_value()
108
109         if len(new_db_values) != 0:
110             # First we try to save in the database
111             try:
112                 self._po.save_plugin_config(new_db_values)
113                 message = "New configuration saved."
114                 message_type = "success"
115             except Exception:  # pylint: disable=broad-except
116                 message = "Failed to save data!"
117                 message_type = "error"
118
119             # Then refresh the actual objects
120             self._po.refresh_plugin_config()
121
122         return self.root_with_msg(message=message,
123                                   message_type=message_type)
124
125
126 class AdminPluginsOrder(AdminPage):
127
128     def __init__(self, site, parent, facility):
129         super(AdminPluginsOrder, self).__init__(site, form=True)
130         self.parent = parent
131         self.facility = facility
132         self.url = '%s/order' % parent.url
133         self.menu = [parent]
134
135     @admin_protect
136     def GET(self, *args, **kwargs):
137         return self.parent.root_with_msg()
138
139     def _get_enabled_list(self):
140         cur = list()
141         for p in self._site[self.facility].available.values():
142             if p.is_enabled:
143                 cur.append(p.name)
144         return cur
145
146     @admin_protect
147     def POST(self, *args, **kwargs):
148
149         if self._site[self.facility].is_readonly:
150             return self.parent.root_with_msg(
151                 message="Configuration is marked Read-Only",
152                 message_type="warning")
153
154         message = "Nothing was modified."
155         message_type = "info"
156         cur_enabled = self._get_enabled_list()
157
158         if 'order' in kwargs:
159             order = kwargs['order'].split(',')
160             if len(order) != 0:
161                 new_order = []
162                 try:
163                     for v in order:
164                         val = v.strip()
165                         if val not in cur_enabled:
166                             error = "Invalid plugin name: %s" % val
167                             raise ValueError(error)
168                         new_order.append(val)
169                     if len(new_order) < len(cur_enabled):
170                         for val in cur_enabled:
171                             if val not in new_order:
172                                 new_order.append(val)
173
174                     self.parent.save_enabled_plugins(new_order)
175
176                     # When all is saved update also live config. The
177                     # live config is the ordered list of plugin names.
178                     self._site[self.facility].refresh_enabled()
179
180                     message = "New configuration saved."
181                     message_type = "success"
182
183                 except ValueError, e:
184                     message = str(e)
185                     message_type = "error"
186
187                 except Exception, e:  # pylint: disable=broad-except
188                     message = "Failed to save data!"
189                     message_type = "error"
190
191         return self.parent.root_with_msg(message=message,
192                                          message_type=message_type)
193
194
195 class AdminPlugins(AdminPage):
196     def __init__(self, name, site, parent, facility, ordered=True):
197         super(AdminPlugins, self).__init__(site)
198         self._master = parent
199         self.name = name
200         self.title = '%s plugins' % name
201         self.url = '%s/%s' % (parent.url, name)
202         self.facility = facility
203         self.template = 'admin/plugins.html'
204         self.order = None
205         parent.add_subtree(name, self)
206
207         for plugin in self._site[facility].available:
208             cherrypy.log.error('Admin info plugin: %s' % plugin)
209             obj = self._site[facility].available[plugin]
210             page = AdminPluginConfig(obj, self._site, self)
211             if hasattr(obj, 'admin'):
212                 obj.admin.mount(page)
213             self.add_subtree(plugin, page)
214
215         if ordered:
216             self.order = AdminPluginsOrder(self._site, self, facility)
217
218     def save_enabled_plugins(self, names):
219         self._site[self.facility].save_enabled(names)
220
221     def root_with_msg(self, message=None, message_type=None):
222         plugins = self._site[self.facility]
223
224         targs = {'title': self.title,
225                  'menu': self._master.menu,
226                  'message': message,
227                  'message_type': message_type,
228                  'available': plugins.available,
229                  'enabled': plugins.enabled,
230                  'baseurl': self.url,
231                  'newurl': self.url}
232         if self.order:
233             targs['order_name'] = '%s_order_form' % self.name
234             targs['order_action'] = self.order.url
235
236         # pylint: disable=star-args
237         return self._template(self.template, **targs)
238
239     def root(self, *args, **kwargs):
240         return self.root_with_msg()
241
242     def _get_plugin_obj(self, plugin):
243         plugins = self._site[self.facility]
244         if plugins.is_readonly:
245             msg = "Configuration is marked Read-Only"
246             raise AdminError(msg)
247         if plugin not in plugins.available:
248             msg = "Unknown plugin %s" % plugin
249             raise AdminError(msg)
250         obj = plugins.available[plugin]
251         if obj.is_readonly:
252             msg = "Plugin Configuration is marked Read-Only"
253             raise AdminError(msg)
254         return obj
255
256     @admin_protect
257     def enable(self, plugin):
258         msg = None
259         try:
260             obj = self._get_plugin_obj(plugin)
261         except AdminError, e:
262             return self.root_with_msg(str(e), "warning")
263         if not obj.is_enabled:
264             obj.enable()
265             obj.save_enabled_state()
266             msg = "Plugin %s enabled" % obj.name
267         return self.root_with_msg(msg, "success")
268     enable.public_function = True
269
270     @admin_protect
271     def disable(self, plugin):
272         msg = None
273         try:
274             obj = self._get_plugin_obj(plugin)
275         except AdminError, e:
276             return self.root_with_msg(str(e), "warning")
277         if obj.is_enabled:
278             obj.disable()
279             obj.save_enabled_state()
280             msg = "Plugin %s disabled" % obj.name
281         return self.root_with_msg(msg, "success")
282     disable.public_function = True
283
284
285 class Admin(AdminPage):
286
287     def __init__(self, site, mount):
288         super(Admin, self).__init__(site)
289         self.title = 'Home'
290         self.mount = mount
291         self.url = '%s/%s' % (self.basepath, mount)
292         self.menu = [self]
293
294     def root(self, *args, **kwargs):
295         return self._template('admin/index.html',
296                               title='Configuration',
297                               baseurl=self.url,
298                               menu=self.menu)
299
300     def add_subtree(self, name, page):
301         self.__dict__[name] = page
302         self.menu.append(page)
303
304     def del_subtree(self, name):
305         self.menu.remove(self.__dict__[name])
306         del self.__dict__[name]
307
308     def get_menu_urls(self):
309         urls = dict()
310         for item in self.menu:
311             name = getattr(item, 'name', None)
312             if name:
313                 urls['%s_url' % name] = cherrypy.url('/%s/%s' % (self.mount,
314                                                                  name))
315         return urls
316
317     @admin_protect
318     def scheme(self):
319         cherrypy.response.headers.update({'Content-Type': 'image/svg+xml'})
320         urls = self.get_menu_urls()
321         # pylint: disable=star-args
322         return self._template('admin/ipsilon-scheme.svg', **urls)
323     scheme.public_function = True