Handle the result of QueryGroups() correctly
[ganeti-local] / lib / client / gnt_job.py
index 93a9ba7..a7c76a2 100644 (file)
@@ -102,11 +102,13 @@ def ListJobs(opts, args):
 
   qfilter = qlang.MakeSimpleFilter("status", opts.status_filter)
 
+  cl = GetClient(query=True)
+
   return GenericList(constants.QR_JOB, selected_fields, args, None,
                      opts.separator, not opts.no_headers,
                      format_override=_JOB_LIST_FORMAT, verbose=opts.verbose,
                      force_filter=opts.force_filter, namefield="id",
-                     qfilter=qfilter, isnumeric=True)
+                     qfilter=qfilter, isnumeric=True, cl=cl)
 
 
 def ListJobFields(opts, args):
@@ -119,8 +121,10 @@ def ListJobFields(opts, args):
   @return: the desired exit code
 
   """
+  cl = GetClient(query=True)
+
   return GenericListFields(constants.QR_JOB, args, opts.separator,
-                           not opts.no_headers)
+                           not opts.no_headers, cl=cl)
 
 
 def ArchiveJobs(opts, args):
@@ -174,19 +178,25 @@ def AutoArchiveJobs(opts, args):
   return 0
 
 
-def CancelJobs(opts, args, cl=None, _stdout_fn=ToStdout, _ask_fn=AskUser):
-  """Cancel not-yet-started jobs.
+def _MultiJobAction(opts, args, cl, stdout_fn, ask_fn, question, action_fn):
+  """Applies a function to multipe jobs.
 
-  @param opts: the command line options selected by the user
+  @param opts: Command line options
   @type args: list
-  @param args: should contain the job IDs to be cancelled
+  @param args: Job IDs
   @rtype: int
-  @return: the desired exit code
+  @return: Exit code
 
   """
   if cl is None:
     cl = GetClient()
 
+  if stdout_fn is None:
+    stdout_fn = ToStdout
+
+  if ask_fn is None:
+    ask_fn = AskUser
+
   result = constants.EXIT_SUCCESS
 
   if bool(args) ^ (opts.status_filter is None):
@@ -206,24 +216,59 @@ def CancelJobs(opts, args, cl=None, _stdout_fn=ToStdout, _ask_fn=AskUser):
       (_, table) = FormatQueryResult(response, header=True,
                                      format_override=_JOB_LIST_FORMAT)
       for line in table:
-        _stdout_fn(line)
+        stdout_fn(line)
 
-      if not _ask_fn("Cancel job(s) listed above?"):
+      if not ask_fn(question):
         return constants.EXIT_CONFIRMATION
   else:
     jobs = args
 
   for job_id in jobs:
-    (success, msg) = cl.CancelJob(job_id)
+    (success, msg) = action_fn(cl, job_id)
 
     if not success:
       result = constants.EXIT_FAILURE
 
-    _stdout_fn(msg)
+    stdout_fn(msg)
 
   return result
 
 
+def CancelJobs(opts, args, cl=None, _stdout_fn=ToStdout, _ask_fn=AskUser):
+  """Cancel not-yet-started jobs.
+
+  @param opts: the command line options selected by the user
+  @type args: list
+  @param args: should contain the job IDs to be cancelled
+  @rtype: int
+  @return: the desired exit code
+
+  """
+  return _MultiJobAction(opts, args, cl, _stdout_fn, _ask_fn,
+                         "Cancel job(s) listed above?",
+                         lambda cl, job_id: cl.CancelJob(job_id))
+
+
+def ChangePriority(opts, args):
+  """Change priority of jobs.
+
+  @param opts: Command line options
+  @type args: list
+  @param args: Job IDs
+  @rtype: int
+  @return: Exit code
+
+  """
+  if opts.priority is None:
+    ToStderr("--priority option must be given.")
+    return constants.EXIT_FAILURE
+
+  return _MultiJobAction(opts, args, None, None, None,
+                         "Change priority of job(s) listed above?",
+                         lambda cl, job_id:
+                           cl.ChangeJobPriority(job_id, opts.priority))
+
+
 def ShowJobs(opts, args):
   """Show detailed information about jobs.
 
@@ -251,7 +296,8 @@ def ShowJobs(opts, args):
     ]
 
   qfilter = qlang.MakeSimpleFilter("id", _ParseJobIds(args))
-  result = GetClient().Query(constants.QR_JOB, selected_fields, qfilter).data
+  cl = GetClient(query=True)
+  result = cl.Query(constants.QR_JOB, selected_fields, qfilter).data
 
   first = True
 
@@ -473,6 +519,12 @@ commands = {
   "watch": (
     WatchJob, [ArgJobId(min=1, max=1)], [],
     "<job-id>", "Follows a job and prints its output as it arrives"),
+  "change-priority": (
+    ChangePriority, [ArgJobId()],
+    [PRIORITY_OPT, FORCE_OPT, _PENDING_OPT, _QUEUED_OPT, _WAITING_OPT],
+    "--priority <priority> {[--force] {--pending | --queued | --waiting} |"
+    " <job-id> [<job-id> ...]}",
+    "Change the priority of jobs"),
   }