Commit 8a23d2d3 authored by Iustin Pop's avatar Iustin Pop

Move string formatting out of LUQueryInstances

Currently, LUQueryInstances will provide strings for its results. This makes it
hard for other consumers than "gnt-instance list" to use the OpQueryInstances
opcode for whatever they wish to.

The change moves the formatting in five of the six cases where this happens to
the actual gnt-instance.ListInstances procedure and removes the unconditional
convert to string in LUQueryInstances.Exec()

Reviewed-by: imsnah
parent 72f0f7fd
......@@ -2138,25 +2138,19 @@ class LUQueryInstances(NoHooksLU):
elif field == "pnode":
val = instance.primary_node
elif field == "snodes":
val = ",".join(instance.secondary_nodes) or "-"
val = list(instance.secondary_nodes)
elif field == "admin_state":
if instance.status == "down":
val = "no"
else:
val = "yes"
val = (instance.status != "down")
elif field == "oper_state":
if instance.primary_node in bad_nodes:
val = "(node down)"
val = None
else:
if live_data.get(instance.name):
val = "running"
else:
val = "stopped"
val = bool(live_data.get(instance.name))
elif field == "admin_ram":
val = instance.memory
elif field == "oper_ram":
if instance.primary_node in bad_nodes:
val = "(node down)"
val = None
elif instance.name in live_data:
val = live_data[instance.name].get("memory", "?")
else:
......@@ -2172,12 +2166,11 @@ class LUQueryInstances(NoHooksLU):
elif field == "sda_size" or field == "sdb_size":
disk = instance.FindDisk(field[:3])
if disk is None:
val = "N/A"
val = None
else:
val = disk.size
else:
raise errors.ParameterError(field)
val = str(val)
iout.append(val)
output.append(iout)
......
......@@ -62,6 +62,32 @@ def ListInstances(opts, args):
numfields = ["admin_ram", "oper_ram", "sda_size", "sdb_size"]
# change raw values to nicer strings
for row in output:
for idx, field in enumerate(selected_fields):
val = row[idx]
if field == "snodes":
val = ",".join(val) or "-"
elif field == "admin_state":
if val:
val = "yes"
else:
val = "no"
elif field == "oper_state":
if val is None:
val = "(node down)"
elif val: # True
val = "running"
else:
val = "stopped"
elif field == "oper_ram":
if val is None:
val = "(node down)"
elif field == "sda_size" or field == "sdb_size":
if val is None:
val = "N/A"
row[idx] = str(val)
data = GenerateTable(separator=opts.separator, headers=headers,
fields=selected_fields, unitfields=unitfields,
numfields=numfields, data=output)
......
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