Return proper errors if config is read-only
authorSimo Sorce <simo@redhat.com>
Mon, 3 Nov 2014 22:28:58 +0000 (17:28 -0500)
committerPatrick Uiterwijk <puiterwijk@redhat.com>
Wed, 12 Nov 2014 22:47:39 +0000 (23:47 +0100)
Do not throw 501 errors, instead return warnings that the configuration
changes cannot be applied.

Signed-off-by: Simo Sorce <simo@redhat.com>
Reviewed-by: Patrick Uiterwijk <puiterwijk@redhat.com>
ipsilon/admin/common.py
ipsilon/util/plugin.py

index 9c82142..0177402 100755 (executable)
@@ -23,6 +23,15 @@ from ipsilon.util.page import admin_protect
 from ipsilon.util import config as pconfig
 
 
 from ipsilon.util import config as pconfig
 
 
+class AdminError(Exception):
+    def __init__(self, message):
+        super(AdminError, self).__init__(message)
+        self.message = message
+
+    def __str__(self):
+        return str(self.message)
+
+
 class AdminPage(Page):
 
     def __init__(self, *args, **kwargs):
 class AdminPage(Page):
 
     def __init__(self, *args, **kwargs):
@@ -61,6 +70,11 @@ class AdminPluginConfig(AdminPage):
     @admin_protect
     def POST(self, *args, **kwargs):
 
     @admin_protect
     def POST(self, *args, **kwargs):
 
+        if self._po.is_readonly:
+            return self.root_with_msg(
+                message="Configuration is marked Read-Only",
+                message_type="warning")
+
         message = "Nothing was modified."
         message_type = "info"
         new_db_values = dict()
         message = "Nothing was modified."
         message_type = "info"
         new_db_values = dict()
@@ -131,6 +145,12 @@ class AdminPluginsOrder(AdminPage):
 
     @admin_protect
     def POST(self, *args, **kwargs):
 
     @admin_protect
     def POST(self, *args, **kwargs):
+
+        if self._site[self.facility].is_readonly:
+            return self.parent.root_with_msg(
+                message="Configuration is marked Read-Only",
+                message_type="warning")
+
         message = "Nothing was modified."
         message_type = "info"
         cur_enabled = self._get_enabled_list()
         message = "Nothing was modified."
         message_type = "info"
         cur_enabled = self._get_enabled_list()
@@ -219,14 +239,27 @@ class AdminPlugins(AdminPage):
     def root(self, *args, **kwargs):
         return self.root_with_msg()
 
     def root(self, *args, **kwargs):
         return self.root_with_msg()
 
-    @admin_protect
-    def enable(self, plugin):
-        msg = None
+    def _get_plugin_obj(self, plugin):
         plugins = self._site[self.facility]
         plugins = self._site[self.facility]
+        if plugins.is_readonly:
+            msg = "Configuration is marked Read-Only"
+            raise AdminError(msg)
         if plugin not in plugins.available:
             msg = "Unknown plugin %s" % plugin
         if plugin not in plugins.available:
             msg = "Unknown plugin %s" % plugin
-            return self.root_with_msg(msg, "error")
+            raise AdminError(msg)
         obj = plugins.available[plugin]
         obj = plugins.available[plugin]
+        if obj.is_readonly:
+            msg = "Plugin Configuration is marked Read-Only"
+            raise AdminError(msg)
+        return obj
+
+    @admin_protect
+    def enable(self, plugin):
+        msg = None
+        try:
+            obj = self._get_plugin_obj(plugin)
+        except AdminError, e:
+            return self.root_with_msg(str(e), "warning")
         if not obj.is_enabled:
             obj.enable()
             obj.save_enabled_state()
         if not obj.is_enabled:
             obj.enable()
             obj.save_enabled_state()
@@ -237,11 +270,10 @@ class AdminPlugins(AdminPage):
     @admin_protect
     def disable(self, plugin):
         msg = None
     @admin_protect
     def disable(self, plugin):
         msg = None
-        plugins = self._site[self.facility]
-        if plugin not in plugins.available:
-            msg = "Unknown plugin %s" % plugin
-            return self.root_with_msg(msg, "error")
-        obj = plugins.available[plugin]
+        try:
+            obj = self._get_plugin_obj(plugin)
+        except AdminError, e:
+            return self.root_with_msg(str(e), "warning")
         if obj.is_enabled:
             obj.disable()
             obj.save_enabled_state()
         if obj.is_enabled:
             obj.disable()
             obj.save_enabled_state()
index 063767c..ae98b4c 100755 (executable)
@@ -89,6 +89,10 @@ class PluginLoader(Log):
             self.__data = AdminStore()
         return self.__data
 
             self.__data = AdminStore()
         return self.__data
 
+    @property
+    def is_readonly(self):
+        return self._data.is_readonly
+
     def get_plugins(self):
         p = Plugins()
         return p.get_plugins(self._pathname, self._plugin_type, self)
     def get_plugins(self):
         p = Plugins()
         return p.get_plugins(self._pathname, self._plugin_type, self)
@@ -129,6 +133,10 @@ class PluginObject(Log):
         self._plugins = plugins
         self.is_enabled = False
 
         self._plugins = plugins
         self.is_enabled = False
 
+    @property
+    def is_readonly(self):
+        return self._data.is_readonly
+
     def on_enable(self):
         return
 
     def on_enable(self):
         return