Implement certificate delete

Implement the DELETE verb on certificate endpoints. This calls the D-Bus
delete interface on corresponding D-Bus objects.

Change-Id: Id829f9064474edd2324ce3c4a66148041b70a95b
Signed-off-by: Deepak Kodihalli <dkodihal@in.ibm.com>
diff --git a/module/obmc/wsgi/apps/rest_dbus.py b/module/obmc/wsgi/apps/rest_dbus.py
index 5a5aaec..6066126 100644
--- a/module/obmc/wsgi/apps/rest_dbus.py
+++ b/module/obmc/wsgi/apps/rest_dbus.py
@@ -877,25 +877,34 @@
 
 
 class CertificateHandler:
-    file_loc = '/tmp'
     file_suffix = '.pem'
     file_prefix = 'cert_'
     CERT_PATH = '/xyz/openbmc_project/certs'
     CERT_IFACE = 'xyz.openbmc_project.Certs.Install'
 
-    def do_upload(cls, route_handler, cert_type, service):
-        def cleanup():
-            if os.path.exists(temp.name):
-                os.remove(temp.name)
-
+    def __init__(self, route_handler, cert_type, service):
         if not service:
             abort(500, "Missing service")
         if not cert_type:
             abort(500, "Missing certificate type")
+        bus = dbus.SystemBus()
+        certPath = self.CERT_PATH + "/" + cert_type + "/" + service
+        intfs = route_handler.try_mapper_call(
+            route_handler.mapper.get_object, path=certPath)
+        for busName,intf in intfs.items():
+            if self.CERT_IFACE in intf:
+                self.obj = bus.get_object(busName, certPath)
+                return
+        abort(404, "Path not found")
+
+    def do_upload(self):
+        def cleanup():
+            if os.path.exists(temp.name):
+                os.remove(temp.name)
 
         with tempfile.NamedTemporaryFile(
-            suffix=cls.file_suffix,
-            prefix=cls.file_prefix,
+            suffix=self.file_suffix,
+            prefix=self.file_prefix,
             delete=False) as temp:
             try:
                 file_contents = request.body.read()
@@ -909,27 +918,23 @@
                 abort(500, "Unexpected Error")
 
         try:
-            bus = dbus.SystemBus()
-            certPath = cls.CERT_PATH + "/" + cert_type + "/" + service
-            intfs = route_handler.try_mapper_call(
-                route_handler.mapper.get_object, path=certPath)
-            for busname,intf in intfs.items():
-                if cls.CERT_IFACE in intf:
-                    obj = bus.get_object(busName, certPath)
-                    iface = dbus.Interface(obj, cls.CERT_IFACE)
-                    iface.Install(temp.name)
-                    cleanup()
-                    return
-            abort(404, "Path not found")
+            iface = dbus.Interface(self.obj, self.CERT_IFACE)
+            iface.Install(temp.name)
         except Exception as e:
             cleanup()
             abort(503, str(e))
+        cleanup()
+
+    def do_delete(self):
+        delete_iface = dbus.Interface(
+            self.obj, dbus_interface=DELETE_IFACE)
+        delete_iface.Delete()
 
 
 class CertificatePutHandler(RouteHandler):
     ''' Handles the /xyz/openbmc_project/certs/<cert_type>/<service> route. '''
 
-    verbs = ['PUT']
+    verbs = ['PUT', 'DELETE']
     rules = ['/xyz/openbmc_project/certs/<cert_type>/<service>']
     content_type = 'application/octet-stream'
 
@@ -938,7 +943,10 @@
             app, bus, self.verbs, self.rules, self.content_type)
 
     def do_put(self, cert_type, service):
-        return CertificateHandler().do_upload(self, cert_type, service)
+        return CertificateHandler(self, cert_type, service).do_upload()
+
+    def do_delete(self, cert_type, service):
+        return CertificateHandler(self, cert_type, service).do_delete()
 
     def find(self, **kw):
         pass