Add notification plugin support to AddSliceToNodes and DeleteSliceFromNodes
authorsmbaker <smbaker@fc8-storktest.lan>
Thu, 25 Apr 2013 05:07:02 +0000 (22:07 -0700)
committersmbaker <smbaker@fc8-storktest.lan>
Thu, 25 Apr 2013 05:07:02 +0000 (22:07 -0700)
PLC/Methods/AddSliceToNodes.py
PLC/Methods/DeleteSliceFromNodes.py
PLC/Plugins.py [new file with mode: 0644]

index db074ca..a7124d8 100644 (file)
@@ -5,6 +5,7 @@ from PLC.Nodes import Node, Nodes
 from PLC.Slices import Slice, Slices
 from PLC.Persons import Person, Persons
 from PLC.Auth import Auth
+from PLC.Plugins import PluginManager
 
 class AddSliceToNodes(Method):
     """
@@ -69,4 +70,6 @@ class AddSliceToNodes(Method):
                               'Slice': [slice['slice_id']]}
         self.message = 'Slice %d added to nodes %s' % (slice['slice_id'], nodeids)
 
+        PluginManager(self.api, auth).notify("slice.AddToNodes", self.event_objects)
+
         return 1
index 1a82ad1..2c15d58 100644 (file)
@@ -4,6 +4,7 @@ from PLC.Parameter import Parameter, Mixed
 from PLC.Nodes import Node, Nodes
 from PLC.Slices import Slice, Slices
 from PLC.Auth import Auth
+from PLC.Plugins import PluginManager
 
 class DeleteSliceFromNodes(Method):
     """
@@ -58,4 +59,6 @@ class DeleteSliceFromNodes(Method):
         self.event_objects = {'Node': [node['node_id'] for node in nodes],
                               'Slice': [slice['slice_id']]}
 
+        PluginManager(self.api, auth).notify("slice.DeleteFromNodes", self.event_objects)
+
         return 1
diff --git a/PLC/Plugins.py b/PLC/Plugins.py
new file mode 100644 (file)
index 0000000..375ec11
--- /dev/null
@@ -0,0 +1,48 @@
+import sys
+import pkg_resources
+import traceback
+
+from PLC.Debug import log
+
+"""
+from PLC.Plugins import PluginManager
+pm = PluginManager(None, None)
+pm.dump()
+pm.notify("who", {"what": "this!"})
+"""
+
+class PluginManager:
+    def __init__(self, api, auth):
+        self.api = api
+        self.auth = auth
+        self.plugins = []
+
+        for entrypoint in pkg_resources.iter_entry_points("plcapi.plugin","api_notify"):
+            save = sys.path
+            try:
+                # pkg_resources looks for modules in sys.path. Make sure it can
+                # find our plugins.
+                sys.path.append("/usr/share/plc_api")
+
+                try:
+                    pluginclass = entrypoint.load()
+                    plugin = pluginclass()
+                    self.plugins.append(plugin)
+                except Exception, exc:
+                    print >>log, "WARNING: failed to load plugin %s" % str(entrypoint)
+                    traceback.print_exc(5,log)
+            finally:
+                sys.path = save
+
+    def notify(self, action, info={}):
+        for plugin in self.plugins:
+            try:
+                plugin.notify(self.api, self.auth, action, info)
+            except Exception, exc:
+                print >>log, "WARNING: failed to run plugin during action %s" % str(action)
+                traceback.print_exc(5,log)
+
+    def dump(self):
+        for plugin in self.plugins:
+            print plugin.__class__.__name__
+