Commit cf26a87a authored by Iustin Pop's avatar Iustin Pop
Browse files

Remove boiler-plate code about node/instance names



Currently we have lots of duplication of the error-checking (and proper
exception raising) around node/instance name expansion. LUCreateInstance
is the only place where we have abstracted this.

This patch creates two functions (ExpandNodeName and ExpandInstanceName)
that will either raise the proper exception or return the expanded name.
This allows a lot of cleanup of duplicate code.
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent ef7b75b2
......@@ -304,12 +304,9 @@ class LogicalUnit(object):
else:
assert locking.LEVEL_INSTANCE not in self.needed_locks, \
"_ExpandAndLockInstance called with instance-level locks set"
expanded_name = self.cfg.ExpandInstanceName(self.op.instance_name)
if expanded_name is None:
raise errors.OpPrereqError("Instance '%s' not known" %
self.op.instance_name, errors.ECODE_NOENT)
self.needed_locks[locking.LEVEL_INSTANCE] = expanded_name
self.op.instance_name = expanded_name
self.op.instance_name = _ExpandInstanceName(self.cfg,
self.op.instance_name)
self.needed_locks[locking.LEVEL_INSTANCE] = self.op.instance_name
def _LockInstancesNodes(self, primary_only=False):
"""Helper function to declare instances' nodes for locking.
......@@ -444,10 +441,7 @@ def _GetWantedNodes(lu, nodes):
wanted = []
for name in nodes:
node = lu.cfg.ExpandNodeName(name)
if node is None:
raise errors.OpPrereqError("No such node name '%s'" % name,
errors.ECODE_NOENT)
node = _ExpandNodeName(lu.cfg, name)
wanted.append(node)
return utils.NiceSort(wanted)
......@@ -471,15 +465,7 @@ def _GetWantedInstances(lu, instances):
errors.ECODE_INVAL)
if instances:
wanted = []
for name in instances:
instance = lu.cfg.ExpandInstanceName(name)
if instance is None:
raise errors.OpPrereqError("No such instance name '%s'" % name,
errors.ECODE_NOENT)
wanted.append(instance)
wanted = [_ExpandInstanceName(lu.cfg, name) for name in instances]
else:
wanted = utils.NiceSort(lu.cfg.GetInstanceList())
return wanted
......@@ -559,6 +545,33 @@ def _CheckNodeNotDrained(lu, node):
errors.ECODE_INVAL)
def _ExpandItemName(fn, name, kind):
"""Expand an item name.
@param fn: the function to use for expansion
@param name: requested item name
@param kind: text description ('Node' or 'Instance')
@return: the resolved (full) name
@raise errors.OpPrereqError: if the item is not found
"""
full_name = fn(name)
if full_name is None:
raise errors.OpPrereqError("%s '%s' not known" % (kind, name),
errors.ECODE_NOENT)
return full_name
def _ExpandNodeName(cfg, name):
"""Wrapper over L{_ExpandItemName} for nodes."""
return _ExpandItemName(cfg.ExpandNodeName, name, "Node")
def _ExpandInstanceName(cfg, name):
"""Wrapper over L{_ExpandItemName} for instance."""
return _ExpandItemName(cfg.ExpandInstanceName, name, "Instance")
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
memory, vcpus, nics, disk_template, disks,
bep, hvp, hypervisor_name):
......@@ -1723,10 +1736,7 @@ class LURepairDiskSizes(NoHooksLU):
if self.op.instances:
self.wanted_names = []
for name in self.op.instances:
full_name = self.cfg.ExpandInstanceName(name)
if full_name is None:
raise errors.OpPrereqError("Instance '%s' not known" % name,
errors.ECODE_NOENT)
full_name = _ExpandInstanceName(self.cfg, name)
self.wanted_names.append(full_name)
self.needed_locks = {
locking.LEVEL_NODE: [],
......@@ -2447,10 +2457,9 @@ class LURemoveNode(LogicalUnit):
Any errors are signaled by raising errors.OpPrereqError.
"""
node = self.cfg.GetNodeInfo(self.cfg.ExpandNodeName(self.op.node_name))
if node is None:
raise errors.OpPrereqError("Node '%s' is unknown." % self.op.node_name,
errors.ECODE_NOENT)
self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
node = self.cfg.GetNodeInfo(self.op.node_name)
assert node is not None
instance_list = self.cfg.GetInstanceList()
......@@ -2850,12 +2859,7 @@ class LUModifyNodeStorage(NoHooksLU):
REQ_BGL = False
def CheckArguments(self):
node_name = self.cfg.ExpandNodeName(self.op.node_name)
if node_name is None:
raise errors.OpPrereqError("Invalid node name '%s'" % self.op.node_name,
errors.ECODE_NOENT)
self.op.node_name = node_name
self.opnode_name = _ExpandNodeName(self.cfg, self.op.node_name)
storage_type = self.op.storage_type
if storage_type not in constants.VALID_STORAGE_TYPES:
......@@ -3129,11 +3133,7 @@ class LUSetNodeParams(LogicalUnit):
REQ_BGL = False
def CheckArguments(self):
node_name = self.cfg.ExpandNodeName(self.op.node_name)
if node_name is None:
raise errors.OpPrereqError("Invalid node name '%s'" % self.op.node_name,
errors.ECODE_INVAL)
self.op.node_name = node_name
self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
_CheckBooleanOpField(self.op, 'master_candidate')
_CheckBooleanOpField(self.op, 'offline')
_CheckBooleanOpField(self.op, 'drained')
......@@ -3282,12 +3282,8 @@ class LUPowercycleNode(NoHooksLU):
REQ_BGL = False
def CheckArguments(self):
node_name = self.cfg.ExpandNodeName(self.op.node_name)
if node_name is None:
raise errors.OpPrereqError("Invalid node name '%s'" % self.op.node_name,
errors.ECODE_NOENT)
self.op.node_name = node_name
if node_name == self.cfg.GetMasterNode() and not self.op.force:
self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
if self.op.node_name == self.cfg.GetMasterNode() and not self.op.force:
raise errors.OpPrereqError("The node is the master and the force"
" parameter was not set",
errors.ECODE_INVAL)
......@@ -3969,14 +3965,10 @@ class LUReinstallInstance(LogicalUnit):
self.op.force_variant = getattr(self.op, "force_variant", False)
if self.op.os_type is not None:
# OS verification
pnode = self.cfg.GetNodeInfo(
self.cfg.ExpandNodeName(instance.primary_node))
if pnode is None:
raise errors.OpPrereqError("Primary node '%s' is unknown" %
self.op.pnode, errors.ECODE_NOENT)
result = self.rpc.call_os_get(pnode.name, self.op.os_type)
pnode = _ExpandNodeName(self.cfg, instance.primary_node)
result = self.rpc.call_os_get(pnode, self.op.os_type)
result.Raise("OS '%s' not in supported OS list for primary node %s" %
(self.op.os_type, pnode.name),
(self.op.os_type, pnode),
prereq=True, ecode=errors.ECODE_INVAL)
if not self.op.force_variant:
_CheckOSVariant(result.payload, self.op.os_type)
......@@ -4115,11 +4107,10 @@ class LURenameInstance(LogicalUnit):
This checks that the instance is in the cluster and is not running.
"""
instance = self.cfg.GetInstanceInfo(
self.cfg.ExpandInstanceName(self.op.instance_name))
if instance is None:
raise errors.OpPrereqError("Instance '%s' not known" %
self.op.instance_name, errors.ECODE_NOENT)
self.op.instance_name = _ExpandInstanceName(self.cfg,
self.op.instance_name)
instance = self.cfg.GetInstanceInfo(self.op.instance_name)
assert instance is not None
_CheckNodeOnline(self, instance.primary_node)
if instance.admin_up:
......@@ -4780,10 +4771,7 @@ class LUMoveInstance(LogicalUnit):
def ExpandNames(self):
self._ExpandAndLockInstance()
target_node = self.cfg.ExpandNodeName(self.op.target_node)
if target_node is None:
raise errors.OpPrereqError("Node '%s' not known" %
self.op.target_node, errors.ECODE_NOENT)
target_node = _ExpandNodeName(self.cfg, self.op.target_node)
self.op.target_node = target_node
self.needed_locks[locking.LEVEL_NODE] = [target_node]
self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
......@@ -4957,10 +4945,7 @@ class LUMigrateNode(LogicalUnit):
REQ_BGL = False
def ExpandNames(self):
self.op.node_name = self.cfg.ExpandNodeName(self.op.node_name)
if self.op.node_name is None:
raise errors.OpPrereqError("Node '%s' not known" % self.op.node_name,
errors.ECODE_NOENT)
self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
self.needed_locks = {
locking.LEVEL_NODE: [self.op.node_name],
......@@ -5020,11 +5005,9 @@ class TLMigrateInstance(Tasklet):
This checks that the instance is in the cluster.
"""
instance = self.cfg.GetInstanceInfo(
self.cfg.ExpandInstanceName(self.instance_name))
if instance is None:
raise errors.OpPrereqError("Instance '%s' not known" %
self.instance_name, errors.ECODE_NOENT)
instance_name = _ExpandInstanceName(self.lu.cfg, self.instance_name)
instance = self.cfg.GetInstanceInfo(instance_name)
assert instance is not None
if instance.disk_template != constants.DT_DRBD8:
raise errors.OpPrereqError("Instance's disk layout is not"
......@@ -5680,15 +5663,6 @@ class LUCreateInstance(LogicalUnit):
raise errors.OpPrereqError("Cannot do ip checks without a name check",
errors.ECODE_INVAL)
def _ExpandNode(self, node):
"""Expands and checks one node name.
"""
node_full = self.cfg.ExpandNodeName(node)
if node_full is None:
raise errors.OpPrereqError("Unknown node %s" % node, errors.ECODE_NOENT)
return node_full
def ExpandNames(self):
"""ExpandNames for CreateInstance.
......@@ -5868,10 +5842,10 @@ class LUCreateInstance(LogicalUnit):
if self.op.iallocator:
self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
else:
self.op.pnode = self._ExpandNode(self.op.pnode)
self.op.pnode = _ExpandNodeName(self.cfg, self.op.pnode)
nodelist = [self.op.pnode]
if self.op.snode is not None:
self.op.snode = self._ExpandNode(self.op.snode)
self.op.snode = _ExpandNodeName(self.cfg, self.op.snode)
nodelist.append(self.op.snode)
self.needed_locks[locking.LEVEL_NODE] = nodelist
......@@ -5891,7 +5865,7 @@ class LUCreateInstance(LogicalUnit):
" path requires a source node option.",
errors.ECODE_INVAL)
else:
self.op.src_node = src_node = self._ExpandNode(src_node)
self.op.src_node = src_node = _ExpandNodeName(self.cfg, src_node)
if self.needed_locks[locking.LEVEL_NODE] is not locking.ALL_SET:
self.needed_locks[locking.LEVEL_NODE].append(src_node)
if not os.path.isabs(src_path):
......@@ -6372,11 +6346,7 @@ class LUReplaceDisks(LogicalUnit):
self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
elif self.op.remote_node is not None:
remote_node = self.cfg.ExpandNodeName(self.op.remote_node)
if remote_node is None:
raise errors.OpPrereqError("Node '%s' not known" %
self.op.remote_node, errors.ECODE_NOENT)
remote_node = _ExpandNodeName(self.cfg, self.op.remote_node)
self.op.remote_node = remote_node
# Warning: do not remove the locking of the new secondary here
......@@ -6447,10 +6417,7 @@ class LUEvacuateNode(LogicalUnit):
self.op.iallocator)
def ExpandNames(self):
self.op.node_name = self.cfg.ExpandNodeName(self.op.node_name)
if self.op.node_name is None:
raise errors.OpPrereqError("Node '%s' not known" % self.op.node_name,
errors.ECODE_NOENT)
self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
self.needed_locks = {}
......@@ -6459,18 +6426,13 @@ class LUEvacuateNode(LogicalUnit):
self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
elif self.op.remote_node is not None:
remote_node = self.cfg.ExpandNodeName(self.op.remote_node)
if remote_node is None:
raise errors.OpPrereqError("Node '%s' not known" %
self.op.remote_node, errors.ECODE_NOENT)
self.op.remote_node = remote_node
self.op.remote_node = _ExpandNodeName(self.cfg, self.op.remote_node)
# Warning: do not remove the locking of the new secondary here
# unless DRBD8.AddChildren is changed to work in parallel;
# currently it doesn't since parallel invocations of
# FindUnusedMinor will conflict
self.needed_locks[locking.LEVEL_NODE] = [remote_node]
self.needed_locks[locking.LEVEL_NODE] = [self.op.remote_node]
self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
else:
......@@ -7199,12 +7161,7 @@ class LURepairNodeStorage(NoHooksLU):
REQ_BGL = False
def CheckArguments(self):
node_name = self.cfg.ExpandNodeName(self.op.node_name)
if node_name is None:
raise errors.OpPrereqError("Invalid node name '%s'" % self.op.node_name,
errors.ECODE_NOENT)
self.op.node_name = node_name
self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
def ExpandNames(self):
self.needed_locks = {
......@@ -7373,10 +7330,7 @@ class LUQueryInstanceData(NoHooksLU):
if self.op.instances:
self.wanted_names = []
for name in self.op.instances:
full_name = self.cfg.ExpandInstanceName(name)
if full_name is None:
raise errors.OpPrereqError("Instance '%s' not known" % name,
errors.ECODE_NOENT)
full_name = _ExpandInstanceName(self.cfg, name)
self.wanted_names.append(full_name)
self.needed_locks[locking.LEVEL_INSTANCE] = self.wanted_names
else:
......@@ -8148,13 +8102,10 @@ class LUExportInstance(LogicalUnit):
"Cannot retrieve locked instance %s" % self.op.instance_name
_CheckNodeOnline(self, self.instance.primary_node)
self.dst_node = self.cfg.GetNodeInfo(
self.cfg.ExpandNodeName(self.op.target_node))
self.op.target_node = _ExpandNodeName(self.cfg, self.op.target_node)
self.dst_node = self.cfg.GetNodeInfo(self.op.target_node)
assert self.dst_node is not None
if self.dst_node is None:
# This is wrong node name, not a non-locked node
raise errors.OpPrereqError("Wrong node name %s" % self.op.target_node,
errors.ECODE_NOENT)
_CheckNodeOnline(self, self.dst_node.name)
_CheckNodeNotDrained(self, self.dst_node.name)
......@@ -8352,19 +8303,11 @@ class TagsLU(NoHooksLU): # pylint: disable-msg=W0223
def ExpandNames(self):
self.needed_locks = {}
if self.op.kind == constants.TAG_NODE:
name = self.cfg.ExpandNodeName(self.op.name)
if name is None:
raise errors.OpPrereqError("Invalid node name (%s)" %
(self.op.name,), errors.ECODE_NOENT)
self.op.name = name
self.needed_locks[locking.LEVEL_NODE] = name
self.op.name = _ExpandNodeName(self.cfg, self.op.name)
self.needed_locks[locking.LEVEL_NODE] = self.op.name
elif self.op.kind == constants.TAG_INSTANCE:
name = self.cfg.ExpandInstanceName(self.op.name)
if name is None:
raise errors.OpPrereqError("Invalid instance name (%s)" %
(self.op.name,), errors.ECODE_NOENT)
self.op.name = name
self.needed_locks[locking.LEVEL_INSTANCE] = name
self.op.name = _ExpandInstanceName(self.cfg, self.op.name)
self.needed_locks[locking.LEVEL_INSTANCE] = self.op.name
def CheckPrereq(self):
"""Check prerequisites.
......@@ -8898,10 +8841,7 @@ class LUTestAllocator(NoHooksLU):
if not hasattr(self.op, "name"):
raise errors.OpPrereqError("Missing attribute 'name' on opcode input",
errors.ECODE_INVAL)
fname = self.cfg.ExpandInstanceName(self.op.name)
if fname is None:
raise errors.OpPrereqError("Instance '%s' not found for relocation" %
self.op.name, errors.ECODE_NOENT)
fname = _ExpandInstanceName(self.cfg, self.op.name)
self.op.name = fname
self.relocate_from = self.cfg.GetInstanceInfo(fname).secondary_nodes
else:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment