From 045dd6d9e8ad07c827331864823446fc4637733c Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Tue, 27 Oct 2009 14:27:46 +0900
Subject: [PATCH] Add ecode to rpc.py's RpcResult.Raise()

This patch adds a new ecode argument to RpcResult.Raise(). This allows
specifying the error code (for both OpExec and OpPrereq errors).

Note that this patch also makes the OpExecError exceptions raised from
_FindFaultInstanceDisks have the error code classification.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 lib/cmdlib.py | 26 +++++++++++++++-----------
 lib/rpc.py    |  8 ++++++--
 2 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index 6e5fbaba9..be21ed1c7 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -716,7 +716,7 @@ def _CheckNicsBridgesExist(lu, target_nics, target_node,
   if brlist:
     result = lu.rpc.call_bridges_exist(target_node, brlist)
     result.Raise("Error checking bridges on destination node '%s'" %
-                 target_node, prereq=True)
+                 target_node, prereq=True, ecode=errors.ECODE_ENVIRON)
 
 
 def _CheckInstanceBridgesExist(lu, instance, node=None):
@@ -797,7 +797,7 @@ def _FindFaultyInstanceDisks(cfg, rpc, instance, node_name, prereq):
 
   result = rpc.call_blockdev_getmirrorstatus(node_name, instance.disks)
   result.Raise("Failed to get disk status from node %s" % node_name,
-               prereq=prereq)
+               prereq=prereq, ecode=errors.ECODE_ENVIRON)
 
   for idx, bdev_status in enumerate(result.payload):
     if bdev_status and bdev_status.ldisk_status == constants.LDS_FAULTY:
@@ -2967,7 +2967,7 @@ class LUAddNode(LogicalUnit):
       result = self.rpc.call_node_has_ip_address(new_node.name,
                                                  new_node.secondary_ip)
       result.Raise("Failure checking secondary ip on node %s" % new_node.name,
-                   prereq=True)
+                   prereq=True, ecode=errors.ECODE_ENVIRON)
       if not result.payload:
         raise errors.OpExecError("Node claims it doesn't have the secondary ip"
                                  " you gave (%s). Please fix and re-run this"
@@ -3539,7 +3539,8 @@ def _CheckNodeFreeMemory(lu, node, reason, requested, hypervisor_name):
 
   """
   nodeinfo = lu.rpc.call_node_info([node], lu.cfg.GetVGName(), hypervisor_name)
-  nodeinfo[node].Raise("Can't get data from node %s" % node, prereq=True)
+  nodeinfo[node].Raise("Can't get data from node %s" % node,
+                       prereq=True, ecode=errors.ECODE_ENVIRON)
   free_mem = nodeinfo[node].payload.get('memory_free', None)
   if not isinstance(free_mem, int):
     raise errors.OpPrereqError("Can't compute free memory on node %s, result"
@@ -3627,7 +3628,7 @@ class LUStartupInstance(LogicalUnit):
                                               instance.name,
                                               instance.hypervisor)
     remote_info.Raise("Error checking node %s" % instance.primary_node,
-                      prereq=True)
+                      prereq=True, ecode=errors.ECODE_ENVIRON)
     if not remote_info.payload: # not running already
       _CheckNodeFreeMemory(self, instance.primary_node,
                            "starting instance %s" % instance.name,
@@ -3846,7 +3847,7 @@ class LUReinstallInstance(LogicalUnit):
                                               instance.name,
                                               instance.hypervisor)
     remote_info.Raise("Error checking node %s" % instance.primary_node,
-                      prereq=True)
+                      prereq=True, ecode=errors.ECODE_ENVIRON)
     if remote_info.payload:
       raise errors.OpPrereqError("Instance '%s' is running on the node %s" %
                                  (self.op.instance_name,
@@ -3864,7 +3865,8 @@ class LUReinstallInstance(LogicalUnit):
                                    self.op.pnode, errors.ECODE_NOENT)
       result = self.rpc.call_os_get(pnode.name, self.op.os_type)
       result.Raise("OS '%s' not in supported OS list for primary node %s" %
-                   (self.op.os_type, pnode.name), prereq=True)
+                   (self.op.os_type, pnode.name),
+                   prereq=True, ecode=errors.ECODE_INVAL)
       if not self.op.force_variant:
         _CheckOSVariant(result.payload, self.op.os_type)
 
@@ -3946,7 +3948,7 @@ class LURecreateInstanceDisks(LogicalUnit):
                                               instance.name,
                                               instance.hypervisor)
     remote_info.Raise("Error checking node %s" % instance.primary_node,
-                      prereq=True)
+                      prereq=True, ecode=errors.ECODE_ENVIRON)
     if remote_info.payload:
       raise errors.OpPrereqError("Instance '%s' is running on the node %s" %
                                  (self.op.instance_name,
@@ -4014,7 +4016,7 @@ class LURenameInstance(LogicalUnit):
                                               instance.name,
                                               instance.hypervisor)
     remote_info.Raise("Error checking node %s" % instance.primary_node,
-                      prereq=True)
+                      prereq=True, ecode=errors.ECODE_ENVIRON)
     if remote_info.payload:
       raise errors.OpPrereqError("Instance '%s' is running on the node %s" %
                                  (self.op.instance_name,
@@ -4910,7 +4912,8 @@ class TLMigrateInstance(Tasklet):
       _CheckNodeNotDrained(self, target_node)
       result = self.rpc.call_instance_migratable(instance.primary_node,
                                                  instance)
-      result.Raise("Can't migrate, please use failover", prereq=True)
+      result.Raise("Can't migrate, please use failover",
+                   prereq=True, ecode=errors.ECODE_STATE)
 
     self.instance = instance
 
@@ -5979,7 +5982,8 @@ class LUCreateInstance(LogicalUnit):
     # os verification
     result = self.rpc.call_os_get(pnode.name, self.op.os_type)
     result.Raise("OS '%s' not in supported os list for primary node %s" %
-                 (self.op.os_type, pnode.name), prereq=True)
+                 (self.op.os_type, pnode.name),
+                 prereq=True, ecode=errors.ECODE_INVAL)
     if not self.op.force_variant:
       _CheckOSVariant(result.payload, self.op.os_type)
 
diff --git a/lib/rpc.py b/lib/rpc.py
index 3c12e2a90..edcb9d7e7 100644
--- a/lib/rpc.py
+++ b/lib/rpc.py
@@ -137,7 +137,7 @@ class RpcResult(object):
     else:
       return "No error information"
 
-  def Raise(self, msg, prereq=False):
+  def Raise(self, msg, prereq=False, ecode=None):
     """If the result has failed, raise an OpExecError.
 
     This is used so that LU code doesn't have to check for each
@@ -156,7 +156,11 @@ class RpcResult(object):
       ec = errors.OpPrereqError
     else:
       ec = errors.OpExecError
-    raise ec(msg)
+    if ecode is not None:
+      args = (msg, prereq)
+    else:
+      args = (msg, )
+    raise ec(*args)
 
 
 class Client:
-- 
GitLab