Commit 130a6a6f authored by Iustin Pop's avatar Iustin Pop

Add a ‘tags’ field to instance and node listing

Currently there isn't any easy way to list all nodes or instance and
their tags; you have to query each node in turn, or list all the tags
via something like “gnt-cluster search-tags '.*'”. Of course, this is
not optimal.

The patch adds a new fields to “gnt-instance list” and “gnt-node list”
called ‘tags’, that will list the tags of the object in comma-separated
form. This field will be empty if there are no tags (when using a
separator this output can still be parsed by other scripts).

At opcode level, there is a new fields called ‘tags’ that returns a
(python) list of the object tags.

Reviewed-by: ultrotter
parent 03a8dbdc
...@@ -1271,7 +1271,7 @@ class LUQueryNodes(NoHooksLU): ...@@ -1271,7 +1271,7 @@ class LUQueryNodes(NoHooksLU):
_CheckOutputFields(static=["name", "pinst_cnt", "sinst_cnt", _CheckOutputFields(static=["name", "pinst_cnt", "sinst_cnt",
"pinst_list", "sinst_list", "pinst_list", "sinst_list",
"pip", "sip"], "pip", "sip", "tags"],
dynamic=self.dynamic_fields, dynamic=self.dynamic_fields,
selected=self.op.output_fields) selected=self.op.output_fields)
...@@ -1342,6 +1342,8 @@ class LUQueryNodes(NoHooksLU): ...@@ -1342,6 +1342,8 @@ class LUQueryNodes(NoHooksLU):
val = node.primary_ip val = node.primary_ip
elif field == "sip": elif field == "sip":
val = node.secondary_ip val = node.secondary_ip
elif field == "tags":
val = list(node.GetTags())
elif field in self.dynamic_fields: elif field in self.dynamic_fields:
val = live_data[node.name].get(field, None) val = live_data[node.name].get(field, None)
else: else:
...@@ -2488,7 +2490,7 @@ class LUQueryInstances(NoHooksLU): ...@@ -2488,7 +2490,7 @@ class LUQueryInstances(NoHooksLU):
_CheckOutputFields(static=["name", "os", "pnode", "snodes", _CheckOutputFields(static=["name", "os", "pnode", "snodes",
"admin_state", "admin_ram", "admin_state", "admin_ram",
"disk_template", "ip", "mac", "bridge", "disk_template", "ip", "mac", "bridge",
"sda_size", "sdb_size", "vcpus"], "sda_size", "sdb_size", "vcpus", "tags"],
dynamic=self.dynamic_fields, dynamic=self.dynamic_fields,
selected=self.op.output_fields) selected=self.op.output_fields)
...@@ -2581,6 +2583,8 @@ class LUQueryInstances(NoHooksLU): ...@@ -2581,6 +2583,8 @@ class LUQueryInstances(NoHooksLU):
val = disk.size val = disk.size
elif field == "vcpus": elif field == "vcpus":
val = instance.vcpus val = instance.vcpus
elif field == "tags":
val = list(instance.GetTags())
else: else:
raise errors.ParameterError(field) raise errors.ParameterError(field)
iout.append(val) iout.append(val)
......
...@@ -575,6 +575,13 @@ ...@@ -575,6 +575,13 @@
instance</simpara> instance</simpara>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term>tags</term>
<listitem>
<simpara>comma-separated list of the instances's
tags</simpara>
</listitem>
</varlistentry>
</variablelist> </variablelist>
</para> </para>
......
...@@ -326,6 +326,13 @@ ...@@ -326,6 +326,13 @@
changes in this value)</simpara> changes in this value)</simpara>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term>tags</term>
<listitem>
<simpara>comma-separated list of the node's
tags</simpara>
</listitem>
</varlistentry>
</variablelist> </variablelist>
</para> </para>
......
...@@ -188,7 +188,7 @@ def ListInstances(opts, args): ...@@ -188,7 +188,7 @@ def ListInstances(opts, args):
"ip": "IP Address", "mac": "MAC Address", "ip": "IP Address", "mac": "MAC Address",
"bridge": "Bridge", "vcpus": "VCPUs", "bridge": "Bridge", "vcpus": "VCPUs",
"sda_size": "Disk/0", "sdb_size": "Disk/1", "sda_size": "Disk/0", "sdb_size": "Disk/1",
"status": "Status", "status": "Status", "tags": "Tags",
} }
else: else:
headers = None headers = None
...@@ -200,6 +200,7 @@ def ListInstances(opts, args): ...@@ -200,6 +200,7 @@ def ListInstances(opts, args):
numfields = ["admin_ram", "oper_ram", "sda_size", "sdb_size", "vcpus"] numfields = ["admin_ram", "oper_ram", "sda_size", "sdb_size", "vcpus"]
list_type_fields = ("tags",)
# change raw values to nicer strings # change raw values to nicer strings
for row in output: for row in output:
for idx, field in enumerate(selected_fields): for idx, field in enumerate(selected_fields):
...@@ -224,6 +225,8 @@ def ListInstances(opts, args): ...@@ -224,6 +225,8 @@ def ListInstances(opts, args):
elif field == "sda_size" or field == "sdb_size": elif field == "sda_size" or field == "sdb_size":
if val is None: if val is None:
val = "N/A" val = "N/A"
elif field in list_type_fields:
val = ",".join(val)
row[idx] = str(val) row[idx] = str(val)
data = GenerateTable(separator=opts.separator, headers=headers, data = GenerateTable(separator=opts.separator, headers=headers,
......
...@@ -70,6 +70,7 @@ def ListNodes(opts, args): ...@@ -70,6 +70,7 @@ def ListNodes(opts, args):
"mtotal": "MTotal", "mnode": "MNode", "mfree": "MFree", "mtotal": "MTotal", "mnode": "MNode", "mfree": "MFree",
"bootid": "BootID", "bootid": "BootID",
"ctotal": "CTotal", "ctotal": "CTotal",
"tags": "Tags",
} }
else: else:
headers = None headers = None
...@@ -84,13 +85,12 @@ def ListNodes(opts, args): ...@@ -84,13 +85,12 @@ def ListNodes(opts, args):
"pinst_cnt", "sinst_cnt", "pinst_cnt", "sinst_cnt",
"ctotal"] "ctotal"]
list_type_fields = ("pinst_list", "sinst_list", "tags")
# change raw values to nicer strings # change raw values to nicer strings
for row in output: for row in output:
for idx, field in enumerate(selected_fields): for idx, field in enumerate(selected_fields):
val = row[idx] val = row[idx]
if field == "pinst_list": if field in list_type_fields:
val = ",".join(val)
elif field == "sinst_list":
val = ",".join(val) val = ",".join(val)
elif val is None: elif val is None:
val = "?" val = "?"
......
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