can set expires upon slice creation
[plcapi.git] / PLC / Methods / GetDummyBoxMedium.py
index 59fbf14..3ba8fd4 100644 (file)
@@ -1,6 +1,7 @@
 #
 # Marta Carbone - UniPi
 # $Id$
+# $URL$
 #
 # This class requires the rpm package containing
 # the picobsd image to be installed
@@ -24,16 +25,14 @@ BASE_IMAGE = "/usr/share/dummynet/picobsd"
 
 class GetDummyBoxMedium(Method):
     """
-    This method is used to get a boot image for the DummyNetBox
+    This method is used to fetch a boot image for the DummyNetBox
     with its configuration file embedded.
 
-    We need to provide the dummybox_id of the DummyNetBox
-    we want to generate.
-    Since every time a new configuration file will be generated,
-    THIS OPERATION WILL INVALIDATE ANY PREVIOUSLY DUMMYNETBOX BOOT MEDIUM.
+    Every time this method is called, a new key is generated in the configuration file,
+    SO THIS OPERATION WILL INVALIDATE ANY PREVIOUSLY DUMMYNETBOX BOOT MEDIUM.
 
-    Returns the iso image customized for the DummyNetBox with the new
-    key integrated in the image, and update the key fields in the database.
+    This method updates the key fields in the database.
+    It returns a base64-encoded boot image.
     """
     # I added the session role, because this method should be called from the web
     roles = ['admin', 'pi', 'tech', 'session']
@@ -158,9 +157,13 @@ class GetDummyBoxMedium(Method):
         os.system("rm %s" % (lockfile))
 
         # if all goes fine store the key in the database
-        dummybox['key'] = new_key
-        dummybox.sync()
-
-        # return the file
-        #return IMAGE_NAME
-        return base64.b64encode(file(IMAGE_NAME).read())
+        nodes = Nodes(self.api, dummybox_id)
+        if not nodes:
+            raise PLCInvalidArgument, "No such node %r"%node_id_or_hostname
+        nodes[0]['key'] = new_key
+        nodes.sync()
+
+        # return the file's content base64-encoded
+        result = file(IMAGE_NAME).read()
+        os.unlink(IMAGE_NAME)
+        return base64.b64encode(result)