From 989ba0c41162600fdf139d93f6fde6aee136f05b Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Fri, 18 Jun 2010 11:36:14 +0200
Subject: [PATCH] Abstract export mode validity check
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The export mode is checked in two places with the exact same code…

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>
---
 lib/cmdlib.py | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index afa376ffb..ea8d3aada 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -685,6 +685,18 @@ def _CheckInstanceDown(lu, instance, reason):
                                (instance.name, reason), errors.ECODE_STATE)
 
 
+def _CheckExportMode(mode):
+  """Ensures that a given export mode is valid.
+
+  @param mode: the export mode to check
+  @raise errors.OpPrereqError: when the export mode is not valid
+
+  """
+  if mode not in constants.EXPORT_MODES:
+    raise errors.OpPrereqError("Invalid export mode %r" % mode,
+                               errors.ECODE_INVAL)
+
+
 def _ExpandItemName(fn, name, kind):
   """Expand an item name.
 
@@ -9160,9 +9172,7 @@ class LUPrepareExport(NoHooksLU):
     """Check the arguments.
 
     """
-    if self.op.mode not in constants.EXPORT_MODES:
-      raise errors.OpPrereqError("Invalid export mode %r" % self.op.mode,
-                                 errors.ECODE_INVAL)
+    _CheckExportMode(self.op.mode)
 
   def ExpandNames(self):
     self._ExpandAndLockInstance()
@@ -9237,9 +9247,7 @@ class LUExportInstance(LogicalUnit):
       raise errors.OpPrereqError("Can not remove instance without shutting it"
                                  " down before")
 
-    if self.op.mode not in constants.EXPORT_MODES:
-      raise errors.OpPrereqError("Invalid export mode %r" % self.op.mode,
-                                 errors.ECODE_INVAL)
+    _CheckExportMode(self.op.mode)
 
     if self.op.mode == constants.EXPORT_MODE_REMOTE:
       if not self.x509_key_name:
-- 
GitLab