From abe362d33580762bb3ef2445f66ce1e3abb7159b Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Tue, 17 Apr 2012 20:06:55 +0200
Subject: [PATCH] Fix error in opcode result processing
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

LUXI queries are processed without callbacks (see
server.masterd.ClientOps._Query). With commit 07923a3c the logic for
checking an opcode's result for jobs to submit was changed and
subsequently raised an exception (β€œ'NoneType' object has no attribute
'SubmitManyJobs'”) in such a case. Before said commit the exception
would also have been raised if an opcode used by a query submitted jobs.

This patch changes the logic to only resolve the method if callbacks are
defined and to use an exception-raising implementation otherwise.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 lib/mcpu.py | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/lib/mcpu.py b/lib/mcpu.py
index 2bf875f87..cd5a4e4c3 100644
--- a/lib/mcpu.py
+++ b/lib/mcpu.py
@@ -220,6 +220,14 @@ def _ProcessResult(submit_fn, op, result):
   return result
 
 
+def _FailingSubmitManyJobs(_):
+  """Implementation of L{OpExecCbBase.SubmitManyJobs} to raise an exception.
+
+  """
+  raise errors.ProgrammerError("Opcodes processed without callbacks (e.g."
+                               " queries) can not submit jobs")
+
+
 def _RpcResultsToHooksResults(rpc_results):
   """Function to convert RPC results to the format expected by HooksMaster.
 
@@ -299,9 +307,13 @@ class Processor(object):
                    " the operation")
       return lu.dry_run_result
 
+    if self._cbs:
+      submit_mj_fn = self._cbs.SubmitManyJobs
+    else:
+      submit_mj_fn = _FailingSubmitManyJobs
+
     try:
-      result = _ProcessResult(self._cbs.SubmitManyJobs, lu.op,
-                              lu.Exec(self.Log))
+      result = _ProcessResult(submit_mj_fn, lu.op, lu.Exec(self.Log))
       h_results = hm.RunPhase(constants.HOOKS_PHASE_POST)
       result = lu.HooksCallBack(constants.HOOKS_PHASE_POST, h_results,
                                 self.Log, result)
-- 
GitLab