From fe742047fdc94ed02bf12993b2b13c8d4313a92b Mon Sep 17 00:00:00 2001
From: Seblu <sebastien.luttringer@smartjog.com>
Date: Thu, 17 Feb 2011 15:22:34 +0100
Subject: [PATCH] Command start,stop,pause,resume,destroy use now TqlCommand

---
 cccli/command/vm.py | 132 +++++++++++++++++++++-----------------------
 1 file changed, 64 insertions(+), 68 deletions(-)

diff --git a/cccli/command/vm.py b/cccli/command/vm.py
index 01836d3..2e85d53 100644
--- a/cccli/command/vm.py
+++ b/cccli/command/vm.py
@@ -8,101 +8,97 @@ from sjrpc.core.exceptions import *
 from cccli.printer import Printer, color
 from cccli.command.command import TqlCommand
 
-class VmCommand(TqlCommand):
-    '''Command for vm style'''
+class Command_start(TqlCommand):
+    '''Start a stopped vm'''
 
     def __init__(self, cli, argv0):
         TqlCommand.__init__(self, cli, argv0)
-        self.set_usage("%prog [options] <tql>")
-        self.add_option("--tg", action="store_true", dest="noask",
-                        help="Don't ask confirmation (Dangerous)")
+        self.tql_filter += "&r=vm&status=stopped"
 
-    def _vm_action(self, argv, filters=None):
-        # parse args
+    def __call__(self, argv):
+        # arg parse
         self.parse_args(argv)
-        if len(self.args) == 0:
+        if len(self.args) != 1:
             raise cmdBadArgument()
-        tql = str.join("", self.args)
-        # append securty options by command name
-        if filters is not None and not self.options.raw:
-            tql += filters
-        if self.options.direct:
-            try:
-                objs = self.cli.rpc.call(self.args[0], tql)
-            except RpcError as e:
-                raise cmdError("RPCError: %s"%str(e))
-        else:
-            # get objects id
-            try:
-                objs = self.cli.rpc.call("list", tql)
-            except RpcError as e:
-                raise cmdError("RPCError: %s"%str(e))
-            # no result, goodbye
-            if len(objs) == 0:
-                raise cmdWarning("tql: '%s': No result."%tql)
-            self.printer.out("You will %s:"%self.args[0])
-            for obj in objs:
-                self.printer.out("%sid:%s%s%s"%(color["green"],color["yellow"],obj["id"],color["reset"]))
-            self.printer.out("%sCount: %s%s"%(color["green"],color["reset"], len(objs)))
-            # be sure boby want do that
-            if not self.options.noask:
-                self.printer.out("%sProceed?%s"%(color["lred"], color["reset"]))
-                if self.printer.ask("Answer (yes/NO): ") != "yes":
-                    raise cmdWarning("Aborted")
-                if len(objs) > 5:
-                    self.printer.out("%sYou request is on more than 5 objets!%s"%(color["yellow"], color["reset"]))
-                    self.printer.out("%sAre you really sure?%s"%(color["lred"], color["reset"]))
-                    if self.printer.ask("Answer (Sir, yes Sir!): ") != "Sir, yes Sir!":
-                        raise cmdWarning("Bad Answer. Aborted")
-            # execute action for each object
-            for obj in objs:
-                self.printer.out("%s%s%s %s%s%s"%(color["lblue"],
-                                                  argv[0],
-                                                  color["reset"],
-                                                  color["yellow"],
-                                                  obj["id"],
-                                                  color["reset"]))
-                try:
-                    self.cli.rpc.call(argv[0], "id:%s"%obj["id"])
-                except RpcError as e:
-                    self.printer.error("RPCError: %s"%str(e))
-
-
-class Command_start(VmCommand):
-    '''Start a stopped vm'''
-    def __call__(self, argv):
-        self._vm_action(argv, "&r=vm&status=stopped")
+        # rpc call
+        self.rpccall("start", self.args[0])
 
 
-class Command_stop(VmCommand):
+class Command_stop(TqlCommand):
     '''Stop a running vm'''
 
+    def __init__(self, cli, argv0):
+        TqlCommand.__init__(self, cli, argv0)
+        self.tql_filter += "&r=vm&status=running"
+
     def __call__(self, argv):
-        self._vm_action(argv, "&r=vm&status=running")
+        # arg parse
+        self.parse_args(argv)
+        if len(self.args) != 1:
+            raise cmdBadArgument()
+        # rpc call
+        self.rpccall("stop", self.args[0])
 
 
-class Command_destroy(VmCommand):
+class Command_destroy(TqlCommand):
     '''Force a vm to stop'''
 
+    def __init__(self, cli, argv0):
+        TqlCommand.__init__(self, cli, argv0)
+        self.tql_filter += "&r=vm&status!=stopped"
+
     def __call__(self, argv):
-        self._vm_action(argv, "&r=vm&status!=stopped")
+        # arg parse
+        self.parse_args(argv)
+        if len(self.args) != 1:
+            raise cmdBadArgument()
+        # rpc call
+        self.rpccall("destroy", self.args[0])
+
 
-class Command_restart(VmCommand):
+class Command_restart(TqlCommand):
     '''Restart a vm'''
 
+    def __init__(self, cli, argv0):
+        TqlCommand.__init__(self, cli, argv0)
+        self.tql_filter += "&r=vm&status=running"
+
     def __call__(self, argv):
-        self._vm_action(argv, "&r=vm")
+        # arg parse
+        self.parse_args(argv)
+        if len(self.args) != 1:
+            raise cmdBadArgument()
+        # rpc call
+        self.rpccall("restart", self.args[0])
 
 
-class Command_pause(VmCommand):
+class Command_pause(TqlCommand):
     '''Pause a running vm'''
 
+    def __init__(self, cli, argv0):
+        TqlCommand.__init__(self, cli, argv0)
+        self.tql_filter += "&r=vm&status=stopped"
+
     def __call__(self, argv):
-        self._vm_action(argv, "&r=vm&status=running")
+        # arg parse
+        self.parse_args(argv)
+        if len(self.args) != 1:
+            raise cmdBadArgument()
+        # rpc call
+        self.rpccall("pause", self.args[0])
 
 
-class Command_resume(VmCommand):
+class Command_resume(TqlCommand):
     '''Resume a paused vm'''
 
+    def __init__(self, cli, argv0):
+        TqlCommand.__init__(self, cli, argv0)
+        self.tql_filter += "&r=vm&status=paused"
+
     def __call__(self, argv):
-        self._vm_action(argv, "&r=vm&status=paused")
+        # arg parse
+        self.parse_args(argv)
+        if len(self.args) != 1:
+            raise cmdBadArgument()
+        # rpc call
+        self.rpccall("resume", self.args[0])
-- 
GitLab