From 3fe11ba3ce04c8dbe3c7a3c433ab4a21c1878070 Mon Sep 17 00:00:00 2001
From: Manuel Franceschini <livewire@google.com>
Date: Wed, 28 Jul 2010 16:58:19 +0200
Subject: [PATCH] gnt-instance rename: Fix bug and rename params

This patch fixes a bug when gnt-instance rename was invoked with
--no-name-check. It renames the internal variables to be consistent with
the ones in equivalent instance add code. Furthermore it checks whether
and instance rename is invoked with --no-name-check but without
--no-ip-check and throws an exception if so.

Signed-off-by: Manuel Franceschini <livewire@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 lib/cmdlib.py        | 33 ++++++++++++++++++++-------------
 lib/opcodes.py       |  2 +-
 scripts/gnt-instance |  4 ++--
 3 files changed, 23 insertions(+), 16 deletions(-)

diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index 8fad4a186..6d9386faf 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -4870,10 +4870,19 @@ class LURenameInstance(LogicalUnit):
   _OP_PARAMS = [
     _PInstanceName,
     ("new_name", _NoDefault, _TNonEmptyString),
-    ("ignore_ip", False, _TBool),
-    ("check_name", True, _TBool),
+    ("ip_check", False, _TBool),
+    ("name_check", True, _TBool),
     ]
 
+  def CheckArguments(self):
+    """Check arguments.
+
+    """
+    if self.op.ip_check and not self.op.name_check:
+      # TODO: make the ip check more flexible and not depend on the name check
+      raise errors.OpPrereqError("Cannot do ip check without a name check",
+                                 errors.ECODE_INVAL)
+
   def BuildHooksEnv(self):
     """Build hooks env.
 
@@ -4899,23 +4908,21 @@ class LURenameInstance(LogicalUnit):
     _CheckInstanceDown(self, instance, "cannot rename")
     self.instance = instance
 
-    # new name verification
-    if self.op.check_name:
-      name_info = netutils.GetHostInfo(self.op.new_name)
-      self.op.new_name = name_info.name
-
     new_name = self.op.new_name
+    if self.op.name_check:
+      hostinfo = netutils.HostInfo(netutils.HostInfo.NormalizeName(new_name))
+      new_name = hostinfo.name
+      if (self.op.ip_check and
+          netutils.TcpPing(hostinfo.ip, constants.DEFAULT_NODED_PORT)):
+        raise errors.OpPrereqError("IP %s of instance %s already in use" %
+                                   (hostinfo.ip, new_name),
+                                   errors.ECODE_NOTUNIQUE)
 
     instance_list = self.cfg.GetInstanceList()
     if new_name in instance_list:
       raise errors.OpPrereqError("Instance '%s' is already in the cluster" %
                                  new_name, errors.ECODE_EXISTS)
 
-    if not self.op.ignore_ip:
-      if netutils.TcpPing(name_info.ip, constants.DEFAULT_NODED_PORT):
-        raise errors.OpPrereqError("IP %s of instance %s already in use" %
-                                   (name_info.ip, new_name),
-                                   errors.ECODE_NOTUNIQUE)
 
   def Exec(self, feedback_fn):
     """Reinstall the instance.
@@ -6527,7 +6534,7 @@ class LUCreateInstance(LogicalUnit):
 
     if self.op.ip_check and not self.op.name_check:
       # TODO: make the ip check more flexible and not depend on the name check
-      raise errors.OpPrereqError("Cannot do ip checks without a name check",
+      raise errors.OpPrereqError("Cannot do ip check without a name check",
                                  errors.ECODE_INVAL)
 
     # check nics' parameter names
diff --git a/lib/opcodes.py b/lib/opcodes.py
index d99d7bd98..457d44221 100644
--- a/lib/opcodes.py
+++ b/lib/opcodes.py
@@ -505,7 +505,7 @@ class OpRenameInstance(OpCode):
   """Rename an instance."""
   OP_ID = "OP_INSTANCE_RENAME"
   __slots__ = [
-    "instance_name", "ignore_ip", "new_name", "check_name",
+    "instance_name", "ip_check", "new_name", "name_check",
     ]
 
 
diff --git a/scripts/gnt-instance b/scripts/gnt-instance
index 3dd7baa6e..7d72dfd09 100755
--- a/scripts/gnt-instance
+++ b/scripts/gnt-instance
@@ -634,8 +634,8 @@ def RenameInstance(opts, args):
 
   op = opcodes.OpRenameInstance(instance_name=args[0],
                                 new_name=args[1],
-                                ignore_ip=not opts.ip_check,
-                                check_name=opts.name_check)
+                                ip_check=opts.ip_check,
+                                name_check=opts.name_check)
   SubmitOrSend(op, opts)
   return 0
 
-- 
GitLab