Commit 31a5d995 authored by Adeodato Simo's avatar Adeodato Simo
Browse files

cmdlib.py: convert LUQueryGroups to query2


Signed-off-by: default avatarAdeodato Simo <dato@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent a0d64991
...@@ -9920,47 +9920,30 @@ class LUAddGroup(LogicalUnit): ...@@ -9920,47 +9920,30 @@ class LUAddGroup(LogicalUnit):
del self.remove_locks[locking.LEVEL_NODEGROUP] del self.remove_locks[locking.LEVEL_NODEGROUP]
class LUQueryGroups(NoHooksLU): class _GroupQuery(_QueryBase):
"""Logical unit for querying node groups.
""" FIELDS = query.GROUP_FIELDS
# pylint: disable-msg=W0142
REQ_BGL = False
_FIELDS_DYNAMIC = utils.FieldSet()
_SIMPLE_FIELDS = ["name", "uuid", "alloc_policy",
"ctime", "mtime", "serial_no"]
_FIELDS_STATIC = utils.FieldSet("node_cnt", "node_list", "pinst_cnt",
"pinst_list", *_SIMPLE_FIELDS)
def CheckArguments(self):
_CheckOutputFields(static=self._FIELDS_STATIC,
dynamic=self._FIELDS_DYNAMIC,
selected=self.op.output_fields)
def ExpandNames(self): def ExpandNames(self, lu):
self.needed_locks = {} lu.needed_locks = {}
def Exec(self, feedback_fn):
"""Computes the list of groups and their attributes.
""" self._all_groups = lu.cfg.GetAllNodeGroupsInfo()
all_groups = self.cfg.GetAllNodeGroupsInfo() name_to_uuid = dict((g.name, g.uuid) for g in self._all_groups.values())
name_to_uuid = dict((g.name, g.uuid) for g in all_groups.values())
if not self.op.names: if not self.names:
sorted_names = utils.NiceSort(name_to_uuid.keys()) self.wanted = [name_to_uuid[name]
my_groups = [name_to_uuid[n] for n in sorted_names] for name in utils.NiceSort(name_to_uuid.keys())]
else: else:
# Accept names to be either names or UUIDs. # Accept names to be either names or UUIDs.
all_uuid = frozenset(all_groups.keys())
my_groups = []
missing = [] missing = []
self.wanted = []
all_uuid = frozenset(self._all_groups.keys())
for name in self.op.names: for name in self.names:
if name in all_uuid: if name in all_uuid:
my_groups.append(name) self.wanted.append(name)
elif name in name_to_uuid: elif name in name_to_uuid:
my_groups.append(name_to_uuid[name]) self.wanted.append(name_to_uuid[name])
else: else:
missing.append(name) missing.append(name)
...@@ -9968,18 +9951,27 @@ class LUQueryGroups(NoHooksLU): ...@@ -9968,18 +9951,27 @@ class LUQueryGroups(NoHooksLU):
raise errors.OpPrereqError("Some groups do not exist: %s" % missing, raise errors.OpPrereqError("Some groups do not exist: %s" % missing,
errors.ECODE_NOENT) errors.ECODE_NOENT)
do_nodes = bool(frozenset(["node_cnt", "node_list"]). def DeclareLocks(self, lu, level):
intersection(self.op.output_fields)) pass
do_instances = bool(frozenset(["pinst_cnt", "pinst_list"]). def _GetQueryData(self, lu):
intersection(self.op.output_fields)) """Computes the list of node groups and their attributes.
# We need to map group->[nodes], and group->[instances]. The former is """
# directly attainable, but the latter we have to do through instance->node, do_nodes = query.GQ_NODE in self.requested_data
# hence we need to process nodes even if we only need instance information. do_instances = query.GQ_INST in self.requested_data
group_to_nodes = None
group_to_instances = None
# For GQ_NODE, we need to map group->[nodes], and group->[instances] for
# GQ_INST. The former is attainable with just GetAllNodesInfo(), but for the
# latter GetAllInstancesInfo() is not enough, for we have to go through
# instance->node. Hence, we will need to process nodes even if we only need
# instance information.
if do_nodes or do_instances: if do_nodes or do_instances:
all_nodes = self.cfg.GetAllNodesInfo() all_nodes = lu.cfg.GetAllNodesInfo()
group_to_nodes = dict((all_groups[name].uuid, []) for name in my_groups) group_to_nodes = dict((uuid, []) for uuid in self.wanted)
node_to_group = {} node_to_group = {}
for node in all_nodes.values(): for node in all_nodes.values():
...@@ -9988,37 +9980,37 @@ class LUQueryGroups(NoHooksLU): ...@@ -9988,37 +9980,37 @@ class LUQueryGroups(NoHooksLU):
node_to_group[node.name] = node.group node_to_group[node.name] = node.group
if do_instances: if do_instances:
all_instances = self.cfg.GetAllInstancesInfo() all_instances = lu.cfg.GetAllInstancesInfo()
group_to_instances = dict((all_groups[name].uuid, []) group_to_instances = dict((uuid, []) for uuid in self.wanted)
for name in my_groups)
for instance in all_instances.values(): for instance in all_instances.values():
node = instance.primary_node node = instance.primary_node
if node in node_to_group: if node in node_to_group:
group_to_instances[node_to_group[node]].append(instance.name) group_to_instances[node_to_group[node]].append(instance.name)
output = [] if not do_nodes:
# Do not pass on node information if it was not requested.
group_to_nodes = None
for uuid in my_groups: return query.GroupQueryData([self._all_groups[uuid]
group = all_groups[uuid] for uuid in self.wanted],
group_output = [] group_to_nodes, group_to_instances)
for field in self.op.output_fields:
if field in self._SIMPLE_FIELDS:
val = getattr(group, field)
elif field == "node_list":
val = utils.NiceSort(group_to_nodes[group.uuid])
elif field == "node_cnt":
val = len(group_to_nodes[group.uuid])
elif field == "pinst_list":
val = utils.NiceSort(group_to_instances[group.uuid])
elif field == "pinst_cnt":
val = len(group_to_instances[group.uuid])
else:
raise errors.ParameterError(field)
group_output.append(val)
output.append(group_output)
return output class LUQueryGroups(NoHooksLU):
"""Logical unit for querying node groups.
"""
REQ_BGL = False
def CheckArguments(self):
self.gq = _GroupQuery(self.op.names, self.op.output_fields, False)
def ExpandNames(self):
self.gq.ExpandNames(self)
def Exec(self, feedback_fn):
return self.gq.OldStyleQuery(self)
class LUSetGroupParams(LogicalUnit): class LUSetGroupParams(LogicalUnit):
...@@ -11016,6 +11008,7 @@ class LUTestAllocator(NoHooksLU): ...@@ -11016,6 +11008,7 @@ class LUTestAllocator(NoHooksLU):
_QUERY_IMPL = { _QUERY_IMPL = {
constants.QR_INSTANCE: _InstanceQuery, constants.QR_INSTANCE: _InstanceQuery,
constants.QR_NODE: _NodeQuery, constants.QR_NODE: _NodeQuery,
constants.QR_GROUP: _GroupQuery,
} }
......
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