Commit 31bf511f authored by Iustin Pop's avatar Iustin Pop
Browse files

Convert cmdlib.py to _FieldSet

This patch converts the current usage of _CheckOutputFields to the
FieldSet class, but it doesn't start to use its variable matching
features.

Reviewed-by: ultrotter
parent 6de7c41d
...@@ -416,20 +416,20 @@ def _GetWantedInstances(lu, instances): ...@@ -416,20 +416,20 @@ def _GetWantedInstances(lu, instances):
def _CheckOutputFields(static, dynamic, selected): def _CheckOutputFields(static, dynamic, selected):
"""Checks whether all selected fields are valid. """Checks whether all selected fields are valid.
Args: @type static: L{_FieldSet}
static: Static fields @param static: static fields set
dynamic: Dynamic fields @type dynamic: L{_FieldSet}
@param dynamic: dynamic fields set
""" """
static_fields = frozenset(static) f = _FieldSet()
dynamic_fields = frozenset(dynamic) f.Extend(static)
f.Extend(dynamic)
all_fields = static_fields | dynamic_fields
if not all_fields.issuperset(selected): delta = f.NonMatching(selected)
if delta:
raise errors.OpPrereqError("Unknown output fields selected: %s" raise errors.OpPrereqError("Unknown output fields selected: %s"
% ",".join(frozenset(selected). % ",".join(delta))
difference(all_fields)))
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status, def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
...@@ -1362,14 +1362,15 @@ class LUDiagnoseOS(NoHooksLU): ...@@ -1362,14 +1362,15 @@ class LUDiagnoseOS(NoHooksLU):
""" """
_OP_REQP = ["output_fields", "names"] _OP_REQP = ["output_fields", "names"]
REQ_BGL = False REQ_BGL = False
_FIELDS_STATIC = _FieldSet()
_FIELDS_DYNAMIC = _FieldSet("name", "valid", "node_status")
def ExpandNames(self): def ExpandNames(self):
if self.op.names: if self.op.names:
raise errors.OpPrereqError("Selective OS query not supported") raise errors.OpPrereqError("Selective OS query not supported")
self.dynamic_fields = frozenset(["name", "valid", "node_status"]) _CheckOutputFields(static=self._FIELDS_STATIC,
_CheckOutputFields(static=[], dynamic=self._FIELDS_DYNAMIC,
dynamic=self.dynamic_fields,
selected=self.op.output_fields) selected=self.op.output_fields)
# Lock all nodes, in shared mode # Lock all nodes, in shared mode
...@@ -1517,24 +1518,23 @@ class LUQueryNodes(NoHooksLU): ...@@ -1517,24 +1518,23 @@ class LUQueryNodes(NoHooksLU):
""" """
_OP_REQP = ["output_fields", "names"] _OP_REQP = ["output_fields", "names"]
REQ_BGL = False REQ_BGL = False
_FIELDS_DYNAMIC = _FieldSet(
"dtotal", "dfree",
"mtotal", "mnode", "mfree",
"bootid",
"ctotal",
)
_FIELDS_STATIC = _FieldSet(
"name", "pinst_cnt", "sinst_cnt",
"pinst_list", "sinst_list",
"pip", "sip", "tags",
"serial_no",
)
def ExpandNames(self): def ExpandNames(self):
self.dynamic_fields = frozenset([ _CheckOutputFields(static=self._FIELDS_STATIC,
"dtotal", "dfree", dynamic=self._FIELDS_DYNAMIC,
"mtotal", "mnode", "mfree",
"bootid",
"ctotal",
])
self.static_fields = frozenset([
"name", "pinst_cnt", "sinst_cnt",
"pinst_list", "sinst_list",
"pip", "sip", "tags",
"serial_no",
])
_CheckOutputFields(static=self.static_fields,
dynamic=self.dynamic_fields,
selected=self.op.output_fields) selected=self.op.output_fields)
self.needed_locks = {} self.needed_locks = {}
...@@ -1545,7 +1545,7 @@ class LUQueryNodes(NoHooksLU): ...@@ -1545,7 +1545,7 @@ class LUQueryNodes(NoHooksLU):
else: else:
self.wanted = locking.ALL_SET self.wanted = locking.ALL_SET
self.do_locking = not self.static_fields.issuperset(self.op.output_fields) self.do_locking = self._FIELDS_STATIC.NonMatching(self.op.output_fields)
if self.do_locking: if self.do_locking:
# if we don't request only static fields, we need to lock the nodes # if we don't request only static fields, we need to lock the nodes
self.needed_locks[locking.LEVEL_NODE] = self.wanted self.needed_locks[locking.LEVEL_NODE] = self.wanted
...@@ -1580,7 +1580,7 @@ class LUQueryNodes(NoHooksLU): ...@@ -1580,7 +1580,7 @@ class LUQueryNodes(NoHooksLU):
# begin data gathering # begin data gathering
if self.dynamic_fields.intersection(self.op.output_fields): if self.do_locking:
live_data = {} live_data = {}
node_data = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(), node_data = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(),
self.cfg.GetHypervisorType()) self.cfg.GetHypervisorType())
...@@ -1641,7 +1641,7 @@ class LUQueryNodes(NoHooksLU): ...@@ -1641,7 +1641,7 @@ class LUQueryNodes(NoHooksLU):
val = list(node.GetTags()) val = list(node.GetTags())
elif field == "serial_no": elif field == "serial_no":
val = node.serial_no val = node.serial_no
elif field in self.dynamic_fields: elif self._FIELDS_DYNAMIC.Matches(field):
val = live_data[node.name].get(field, None) val = live_data[node.name].get(field, None)
else: else:
raise errors.ParameterError(field) raise errors.ParameterError(field)
...@@ -1657,10 +1657,12 @@ class LUQueryNodeVolumes(NoHooksLU): ...@@ -1657,10 +1657,12 @@ class LUQueryNodeVolumes(NoHooksLU):
""" """
_OP_REQP = ["nodes", "output_fields"] _OP_REQP = ["nodes", "output_fields"]
REQ_BGL = False REQ_BGL = False
_FIELDS_DYNAMIC = _FieldSet("phys", "vg", "name", "size", "instance")
_FIELDS_STATIC = _FieldSet("node")
def ExpandNames(self): def ExpandNames(self):
_CheckOutputFields(static=["node"], _CheckOutputFields(static=self._FIELDS_STATIC,
dynamic=["phys", "vg", "name", "size", "instance"], dynamic=self._FIELDS_DYNAMIC,
selected=self.op.output_fields) selected=self.op.output_fields)
self.needed_locks = {} self.needed_locks = {}
...@@ -1977,13 +1979,14 @@ class LUQueryConfigValues(NoHooksLU): ...@@ -1977,13 +1979,14 @@ class LUQueryConfigValues(NoHooksLU):
""" """
_OP_REQP = [] _OP_REQP = []
REQ_BGL = False REQ_BGL = False
_FIELDS_DYNAMIC = _FieldSet()
_FIELDS_STATIC = _FieldSet("cluster_name", "master_node", "drain_flag")
def ExpandNames(self): def ExpandNames(self):
self.needed_locks = {} self.needed_locks = {}
static_fields = ["cluster_name", "master_node", "drain_flag"] _CheckOutputFields(static=self._FIELDS_STATIC,
_CheckOutputFields(static=static_fields, dynamic=self._FIELDS_DYNAMIC,
dynamic=[],
selected=self.op.output_fields) selected=self.op.output_fields)
def CheckPrereq(self): def CheckPrereq(self):
...@@ -2711,22 +2714,22 @@ class LUQueryInstances(NoHooksLU): ...@@ -2711,22 +2714,22 @@ class LUQueryInstances(NoHooksLU):
""" """
_OP_REQP = ["output_fields", "names"] _OP_REQP = ["output_fields", "names"]
REQ_BGL = False REQ_BGL = False
_FIELDS_STATIC = _FieldSet(*["name", "os", "pnode", "snodes",
"admin_state", "admin_ram",
"disk_template", "ip", "mac", "bridge",
"sda_size", "sdb_size", "vcpus", "tags",
"network_port", "beparams",
"serial_no", "hypervisor", "hvparams",] +
["hv/%s" % name
for name in constants.HVS_PARAMETERS] +
["be/%s" % name
for name in constants.BES_PARAMETERS])
_FIELDS_DYNAMIC = _FieldSet("oper_state", "oper_ram", "status")
def ExpandNames(self): def ExpandNames(self):
self.dynamic_fields = frozenset(["oper_state", "oper_ram", "status"]) _CheckOutputFields(static=self._FIELDS_STATIC,
hvp = ["hv/%s" % name for name in constants.HVS_PARAMETERS] dynamic=self._FIELDS_DYNAMIC,
bep = ["be/%s" % name for name in constants.BES_PARAMETERS]
self.static_fields = frozenset([
"name", "os", "pnode", "snodes",
"admin_state", "admin_ram",
"disk_template", "ip", "mac", "bridge",
"sda_size", "sdb_size", "vcpus", "tags",
"network_port", "beparams",
"serial_no", "hypervisor", "hvparams",
] + hvp + bep)
_CheckOutputFields(static=self.static_fields,
dynamic=self.dynamic_fields,
selected=self.op.output_fields) selected=self.op.output_fields)
self.needed_locks = {} self.needed_locks = {}
...@@ -2738,7 +2741,7 @@ class LUQueryInstances(NoHooksLU): ...@@ -2738,7 +2741,7 @@ class LUQueryInstances(NoHooksLU):
else: else:
self.wanted = locking.ALL_SET self.wanted = locking.ALL_SET
self.do_locking = not self.static_fields.issuperset(self.op.output_fields) self.do_locking = self._FIELDS_STATIC.NonMatching(self.op.output_fields)
if self.do_locking: if self.do_locking:
self.needed_locks[locking.LEVEL_INSTANCE] = self.wanted self.needed_locks[locking.LEVEL_INSTANCE] = self.wanted
self.needed_locks[locking.LEVEL_NODE] = [] self.needed_locks[locking.LEVEL_NODE] = []
...@@ -2780,7 +2783,7 @@ class LUQueryInstances(NoHooksLU): ...@@ -2780,7 +2783,7 @@ class LUQueryInstances(NoHooksLU):
hv_list = list(set([inst.hypervisor for inst in instance_list])) hv_list = list(set([inst.hypervisor for inst in instance_list]))
bad_nodes = [] bad_nodes = []
if self.dynamic_fields.intersection(self.op.output_fields): if self.do_locking:
live_data = {} live_data = {}
node_data = self.rpc.call_all_instances_info(nodes, hv_list) node_data = self.rpc.call_all_instances_info(nodes, hv_list)
for name in nodes: for name in nodes:
......
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