These checks were repeated in the code, and will have to be reused for the instance renames.
Signed-off-by: Hrvoje Ribicic <r...@google.com> --- lib/cmdlib/instance.py | 14 ++++---------- lib/cmdlib/instance_utils.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/cmdlib/instance.py b/lib/cmdlib/instance.py index 1a6d08b..a9d2843 100644 --- a/lib/cmdlib/instance.py +++ b/lib/cmdlib/instance.py @@ -65,7 +65,7 @@ from ganeti.cmdlib.instance_utils import BuildInstanceHookEnvByObject, \ ReleaseLocks, CheckNodeVmCapable, CheckTargetNodeIPolicy, \ GetInstanceInfoText, RemoveDisks, CheckNodeFreeMemory, \ CheckInstanceBridgesExist, CheckNicsBridgesExist, UpdateMetadata, \ - CheckCompressionTool + CheckCompressionTool, CheckInstanceExistence import ganeti.masterd.instance @@ -578,10 +578,7 @@ class LUInstanceCreate(LogicalUnit): # this is just a preventive check, but someone might still add this # instance in the meantime, and creation will fail at lock-add time - if self.op.instance_name in\ - [inst.name for inst in self.cfg.GetAllInstancesInfo().values()]: - raise errors.OpPrereqError("Instance '%s' is already in the cluster" % - self.op.instance_name, errors.ECODE_EXISTS) + CheckInstanceExistence(self, self.op.instance_name) self.add_locks[locking.LEVEL_INSTANCE] = self.op.instance_name @@ -1900,11 +1897,8 @@ class LUInstanceRename(LogicalUnit): (hostname.ip, new_name), errors.ECODE_NOTUNIQUE) - instance_names = [inst.name for - inst in self.cfg.GetAllInstancesInfo().values()] - if new_name in instance_names and new_name != instance.name: - raise errors.OpPrereqError("Instance '%s' is already in the cluster" % - new_name, errors.ECODE_EXISTS) + if new_name != instance.name: + CheckInstanceExistence(self, new_name) def Exec(self, feedback_fn): """Rename the instance. diff --git a/lib/cmdlib/instance_utils.py b/lib/cmdlib/instance_utils.py index 6674a76..f9545d0 100644 --- a/lib/cmdlib/instance_utils.py +++ b/lib/cmdlib/instance_utils.py @@ -744,3 +744,18 @@ def BuildDiskEnv(idx, disk): ret["INSTANCE_DISK%d_MODE" % idx] = mode return ret + + +def CheckInstanceExistence(lu, instance_name): + """Raises an error if an instance with the given name exists already. + + @type instance_name: string + @param instance_name: The name of the instance. + + To be used in the locking phase. + + """ + if instance_name in \ + [inst.name for inst in lu.cfg.GetAllInstancesInfo().values()]: + raise errors.OpPrereqError("Instance '%s' is already in the cluster" % + instance_name, errors.ECODE_EXISTS) -- 2.0.0.526.g5318336