added image argument to CLI
[osm/vim-emu.git] / emuvim / api / zerorpcapi.py
index 71505a5..be44444 100644 (file)
@@ -7,7 +7,7 @@ import logging
 import threading
 import zerorpc
 
-logging.basicConfig(level=logging.DEBUG)
+logging.basicConfig(level=logging.INFO)
 
 
 class ZeroRpcApiEndpoint(object):
@@ -56,16 +56,47 @@ class MultiDatacenterApi(object):
     def __init__(self, dcs):
         self.dcs = dcs
 
-    def compute_action_start(self, dc_name, compute_name):
-        # TODO what to return UUID / IP ?
+    def compute_action_start(self, dc_name, compute_name, image):
+        # TODO what to return UUID / given name / internal name ?
         logging.debug("RPC CALL: compute start")
-        if dc_name in self.dcs:
-            self.dcs[dc_name].addCompute(compute_name)
+        try:
+            c = self.dcs.get(dc_name).startCompute(compute_name, image=image)
+            return str(c.name)
+        except Exception as ex:
+            logging.exception("RPC error.")
+            return ex.message
 
     def compute_action_stop(self, dc_name, compute_name):
-        logging.info("compute stop")
-        if dc_name in self.dcs:
-            self.dcs[dc_name].removeCompute(compute_name)
+        logging.debug("RPC CALL: compute stop")
+        try:
+            return self.dcs.get(dc_name).stopCompute(compute_name)
+        except Exception as ex:
+            logging.exception("RPC error.")
+            return ex.message
 
-    def compute_list(self):
-        pass
+    def compute_list(self, dc_name):
+        logging.debug("RPC CALL: compute list")
+        try:
+            if dc_name is None:
+                # return list with all compute nodes in all DCs
+                all_containers = []
+                for dc in self.dcs.itervalues():
+                    all_containers += dc.listCompute()
+                return [(c.name, c.getStatus())
+                        for c in all_containers]
+            else:
+                # return list of compute nodes for specified DC
+                return [(c.name, c.getStatus())
+                        for c in self.dcs.get(dc_name).listCompute()]
+        except Exception as ex:
+            logging.exception("RPC error.")
+            return ex.message
+
+    def compute_status(self, dc_name, compute_name):
+        logging.debug("RPC CALL: compute status")
+        try:
+            return self.dcs.get(
+                dc_name).containers.get(compute_name).getStatus()
+        except Exception as ex:
+            logging.exception("RPC error.")
+            return ex.message