Add formatters for Node and VM, fix handling

This commit is contained in:
Joshua Boniface 2023-08-09 13:13:03 -04:00
parent 01122415f6
commit a032dcc5c8
4 changed files with 235 additions and 238 deletions

View File

@ -84,8 +84,12 @@ def finish(success=True, data=None, formatter=None):
""" """
if data is not None: if data is not None:
if formatter is not None: if formatter is not None and success:
echo(CLI_CONFIG, formatter(data)) if formatter.__name__ == "<lambda>":
# We don't pass CLI_CONFIG into lambdas
echo(CLI_CONFIG, formatter(data))
else:
echo(CLI_CONFIG, formatter(CLI_CONFIG, data))
else: else:
echo(CLI_CONFIG, data) echo(CLI_CONFIG, data)
@ -1597,34 +1601,26 @@ def cli_vm_tag():
############################################################################### ###############################################################################
# > pvc vm tag get TODO:formatter # > pvc vm tag get
############################################################################### ###############################################################################
@click.command(name="get", short_help="Get the current tags of a virtual machine.") @click.command(name="get", short_help="Get the current tags of a virtual machine.")
@connection_req @connection_req
@click.argument("domain") @click.argument("domain")
@click.option( @format_opt(
"-r", {
"--raw", "pretty": cli_vm_tag_get_format_pretty,
"raw", "raw": lambda d: "\n".join([t["name"] for t in d["tags"]]),
is_flag=True, "json": lambda d: jdumps(d),
default=False, "json-pretty": lambda d: jdumps(d, indent=2),
help="Display the raw value only without formatting.", }
) )
def cli_vm_tag_get(domain, raw): def cli_vm_tag_get(domain, format_function):
""" """
Get the current tags of the virtual machine DOMAIN. Get the current tags of the virtual machine DOMAIN.
""" """
retcode, retdata = pvc.lib.vm.vm_tags_get(CLI_CONFIG, domain) retcode, retdata = pvc.lib.vm.vm_tags_get(CLI_CONFIG, domain)
if retcode: finish(retcode, retdata, format_function)
if not raw:
retdata = pvc.lib.vm.format_vm_tags(CLI_CONFIG, domain, retdata["tags"])
else:
if len(retdata["tags"]) > 0:
retdata = "\n".join([tag["name"] for tag in retdata["tags"]])
else:
retdata = "No tags found."
finish(retcode, retdata)
############################################################################### ###############################################################################
@ -1684,32 +1680,28 @@ def cli_vm_vcpu():
############################################################################### ###############################################################################
# > pvc vm vcpu get TODO:formatter # > pvc vm vcpu get
############################################################################### ###############################################################################
@click.command( @click.command(
name="get", short_help="Get the current vCPU count of a virtual machine." name="get", short_help="Get the current vCPU count of a virtual machine."
) )
@connection_req @connection_req
@click.argument("domain") @click.argument("domain")
@click.option( @format_opt(
"-r", {
"--raw", "pretty": cli_vm_vcpu_get_format_pretty,
"raw", "raw": lambda d: d["vcpus"],
is_flag=True, "json": lambda d: jdumps(d),
default=False, "json-pretty": lambda d: jdumps(d, indent=2),
help="Display the raw value only without formatting.", }
) )
def cli_vm_vcpu_get(domain, raw): def cli_vm_vcpu_get(domain, format_function):
""" """
Get the current vCPU count of the virtual machine DOMAIN. Get the current vCPU count of the virtual machine DOMAIN.
""" """
retcode, retmsg = pvc.lib.vm.vm_vcpus_get(CLI_CONFIG, domain) retcode, retmsg = pvc.lib.vm.vm_vcpus_get(CLI_CONFIG, domain)
if not raw: finish(retcode, retmsg, format_function)
retmsg = pvc.lib.vm.format_vm_vcpus(CLI_CONFIG, domain, retmsg)
else:
retmsg = retmsg[0] # Get only the first part of the tuple (vm_vcpus)
finish(retcode, retmsg)
############################################################################### ###############################################################################
@ -1767,30 +1759,28 @@ def cli_vm_memory():
############################################################################### ###############################################################################
# > pvc vm memory get TODO:formatter # > pvc vm memory get
############################################################################### ###############################################################################
@click.command( @click.command(
name="get", short_help="Get the current provisioned memory of a virtual machine." name="get", short_help="Get the current provisioned memory of a virtual machine."
) )
@connection_req @connection_req
@click.argument("domain") @click.argument("domain")
@click.option( @format_opt(
"-r", {
"--raw", "pretty": cli_vm_memory_get_format_pretty,
"raw", "raw": lambda d: d["memory"],
is_flag=True, "json": lambda d: jdumps(d),
default=False, "json-pretty": lambda d: jdumps(d, indent=2),
help="Display the raw value only without formatting.", }
) )
def cli_vm_memory_get(domain, raw): def cli_vm_memory_get(domain, format_function):
""" """
Get the current provisioned memory of the virtual machine DOMAIN. Get the current provisioned memory of the virtual machine DOMAIN.
""" """
retcode, retmsg = pvc.lib.vm.vm_memory_get(CLI_CONFIG, domain) retcode, retmsg = pvc.lib.vm.vm_memory_get(CLI_CONFIG, domain)
if not raw: finish(retcode, retmsg, format_function)
retmsg = pvc.lib.vm.format_vm_memory(CLI_CONFIG, domain, retmsg)
finish(retcode, retmsg)
############################################################################### ###############################################################################
@ -1829,33 +1819,26 @@ def cli_vm_network():
############################################################################### ###############################################################################
# > pvc vm network get TODO:formatter # > pvc vm network get
############################################################################### ###############################################################################
@click.command(name="get", short_help="Get the networks of a virtual machine.") @click.command(name="get", short_help="Get the networks of a virtual machine.")
@connection_req @connection_req
@click.argument("domain") @click.argument("domain")
@click.option( @format_opt(
"-r", {
"--raw", "pretty": cli_vm_network_get_format_pretty,
"raw", "raw": lambda d: ",".join([t["network"] for t in d["networks"]]),
is_flag=True, "json": lambda d: jdumps(d),
default=False, "json-pretty": lambda d: jdumps(d, indent=2),
help="Display the raw values only without formatting.", }
) )
def cli_vm_network_get(domain, raw): def cli_vm_network_get(domain, format_function):
""" """
Get the networks of the virtual machine DOMAIN. Get the networks of the virtual machine DOMAIN.
""" """
retcode, retdata = pvc.lib.vm.vm_networks_get(CLI_CONFIG, domain) retcode, retdata = pvc.lib.vm.vm_networks_get(CLI_CONFIG, domain)
if not raw: finish(retcode, retdata, format_function)
retmsg = pvc.lib.vm.format_vm_networks(CLI_CONFIG, domain, retdata)
else:
network_vnis = list()
for network in retdata:
network_vnis.append(network[0])
retmsg = ",".join(network_vnis)
finish(retcode, retmsg)
############################################################################### ###############################################################################
@ -2013,33 +1996,28 @@ def cli_vm_volume():
############################################################################### ###############################################################################
# > pvc vm volume get TODO:formatter # > pvc vm volume get
############################################################################### ###############################################################################
@click.command(name="get", short_help="Get the volumes of a virtual machine.") @click.command(name="get", short_help="Get the volumes of a virtual machine.")
@connection_req @connection_req
@click.argument("domain") @click.argument("domain")
@click.option( @format_opt(
"-r", {
"--raw", "pretty": cli_vm_volume_get_format_pretty,
"raw", "raw": lambda d: ",".join(
is_flag=True, [f"{v['protocol']}:{v['volume']}" for v in d["volumes"]]
default=False, ),
help="Display the raw values only without formatting.", "json": lambda d: jdumps(d),
"json-pretty": lambda d: jdumps(d, indent=2),
}
) )
def cli_vm_volume_get(domain, raw): def cli_vm_volume_get(domain, format_function):
""" """
Get the volumes of the virtual machine DOMAIN. Get the volumes of the virtual machine DOMAIN.
""" """
retcode, retdata = pvc.lib.vm.vm_volumes_get(CLI_CONFIG, domain) retcode, retdata = pvc.lib.vm.vm_volumes_get(CLI_CONFIG, domain)
if not raw: finish(retcode, retdata, format_function)
retmsg = pvc.lib.vm.format_vm_volumes(CLI_CONFIG, domain, retdata)
else:
volume_paths = list()
for volume in retdata:
volume_paths.append("{}:{}".format(volume[2], volume[0]))
retmsg = ",".join(volume_paths)
finish(retcode, retmsg)
############################################################################### ###############################################################################
@ -2208,32 +2186,30 @@ def cli_vm_dump(filename, domain):
############################################################################### ###############################################################################
# > pvc vm info TODO:formatter # > pvc vm info
############################################################################### ###############################################################################
@click.command(name="info", short_help="Show details of a VM object.") @click.command(name="info", short_help="Show details of a VM object.")
@connection_req @connection_req
@click.argument("domain") @click.argument("domain")
@click.option( @format_opt(
"-l", {
"--long", "pretty": cli_vm_info_format_pretty,
"long_output", "long": cli_vm_info_format_long,
is_flag=True, "json": lambda d: jdumps(d),
default=False, "json-pretty": lambda d: jdumps(d, indent=2),
help="Display more detailed information.", }
) )
def cli_vm_info(domain, long_output): def cli_vm_info(domain, format_function):
""" """
Show information about virtual machine DOMAIN. DOMAIN may be a UUID or name. Show information about virtual machine DOMAIN. DOMAIN may be a UUID or name.
""" """
retcode, retdata = pvc.lib.vm.vm_info(CLI_CONFIG, domain) retcode, retdata = pvc.lib.vm.vm_info(CLI_CONFIG, domain)
if retcode: finish(retcode, retdata, format_function)
retdata = pvc.lib.vm.format_info(CLI_CONFIG, retdata, long_output)
finish(retcode, retdata)
############################################################################### ###############################################################################
# > pvc vm list TODO:formatter # > pvc vm list
############################################################################### ###############################################################################
@click.command(name="list", short_help="List all VM objects.") @click.command(name="list", short_help="List all VM objects.")
@connection_req @connection_req
@ -2259,14 +2235,6 @@ def cli_vm_info(domain, long_output):
default=None, default=None,
help="Limit list to VMs with the specified tag.", help="Limit list to VMs with the specified tag.",
) )
@click.option(
"-r",
"--raw",
"raw",
is_flag=True,
default=False,
help="Display the raw list of VM names only.",
)
@click.option( @click.option(
"-n", "-n",
"--negate", "--negate",
@ -2275,7 +2243,15 @@ def cli_vm_info(domain, long_output):
default=False, default=False,
help="Negate the specified node, state, or tag limit(s).", help="Negate the specified node, state, or tag limit(s).",
) )
def cli_vm_list(target_node, target_state, target_tag, limit, raw, negate): @format_opt(
{
"pretty": cli_vm_list_format_pretty,
"raw": lambda d: "\n".join([v["name"] for v in d]),
"json": lambda d: jdumps(d),
"json-pretty": lambda d: jdumps(d, indent=2),
}
)
def cli_vm_list(target_node, target_state, target_tag, limit, negate, format_function):
""" """
List all virtual machines; optionally only match names or full UUIDs matching regex LIMIT. List all virtual machines; optionally only match names or full UUIDs matching regex LIMIT.
@ -2285,12 +2261,7 @@ def cli_vm_list(target_node, target_state, target_tag, limit, raw, negate):
retcode, retdata = pvc.lib.vm.vm_list( retcode, retdata = pvc.lib.vm.vm_list(
CLI_CONFIG, limit, target_node, target_state, target_tag, negate CLI_CONFIG, limit, target_node, target_state, target_tag, negate
) )
if retcode: finish(retcode, retdata, format_function)
retdata = pvc.lib.vm.format_list(CLI_CONFIG, retdata, raw)
else:
if raw:
retdata = ""
finish(retcode, retdata)
############################################################################### ###############################################################################
@ -3347,8 +3318,8 @@ def cli_storage_osd_set(osd_property):
# > pvc storage osd unset # > pvc storage osd unset
############################################################################### ###############################################################################
@click.command(name="unset", short_help="Unset OSD property.") @click.command(name="unset", short_help="Unset OSD property.")
@connection_req
@click.argument("osd_property") @click.argument("osd_property")
@cluster_req
def cli_storage_osd_unset(osd_property): def cli_storage_osd_unset(osd_property):
""" """
Unset (disable) a Ceph OSD property OSD_PROPERTY on the cluster. Unset (disable) a Ceph OSD property OSD_PROPERTY on the cluster.
@ -5218,7 +5189,7 @@ def cli_provisioner_create(
# > pvc provisioner status TODO:formatter # > pvc provisioner status TODO:formatter
############################################################################### ###############################################################################
@click.command(name="status", short_help="Show status of provisioner job.") @click.command(name="status", short_help="Show status of provisioner job.")
@connection_Req @connection_req
@click.argument("job", required=False, default=None) @click.argument("job", required=False, default=None)
def cli_provisioner_status(job): def cli_provisioner_status(job):
""" """

View File

@ -21,6 +21,13 @@
from pvc.lib.node import format_info as node_format_info from pvc.lib.node import format_info as node_format_info
from pvc.lib.node import format_list as node_format_list from pvc.lib.node import format_list as node_format_list
from pvc.lib.vm import format_vm_tags as vm_format_tags
from pvc.lib.vm import format_vm_vcpus as vm_format_vcpus
from pvc.lib.vm import format_vm_memory as vm_format_memory
from pvc.lib.vm import format_vm_networks as vm_format_networks
from pvc.lib.vm import format_vm_volumes as vm_format_volumes
from pvc.lib.vm import format_info as vm_format_info
from pvc.lib.vm import format_list as vm_format_list
# Define colour values for use in formatters # Define colour values for use in formatters
@ -36,7 +43,7 @@ ansii = {
} }
def cli_cluster_status_format_pretty(data): def cli_cluster_status_format_pretty(CLI_CONFIG, data):
""" """
Pretty format the full output of cli_cluster_status Pretty format the full output of cli_cluster_status
""" """
@ -188,7 +195,7 @@ def cli_cluster_status_format_pretty(data):
return "\n".join(output) return "\n".join(output)
def cli_cluster_status_format_short(data): def cli_cluster_status_format_short(CLI_CONFIG, data):
""" """
Pretty format the health-only output of cli_cluster_status Pretty format the health-only output of cli_cluster_status
""" """
@ -233,7 +240,7 @@ def cli_cluster_status_format_short(data):
return "\n".join(output) return "\n".join(output)
def cli_connection_list_format_pretty(data): def cli_connection_list_format_pretty(CLI_CONFIG, data):
""" """
Pretty format the output of cli_connection_list Pretty format the output of cli_connection_list
""" """
@ -305,7 +312,7 @@ def cli_connection_list_format_pretty(data):
return "\n".join(output) return "\n".join(output)
def cli_connection_detail_format_pretty(data): def cli_connection_detail_format_pretty(CLI_CONFIG, data):
""" """
Pretty format the output of cli_connection_detail Pretty format the output of cli_connection_detail
""" """
@ -425,25 +432,89 @@ def cli_connection_detail_format_pretty(data):
return "\n".join(output) return "\n".join(output)
def cli_node_info_format_pretty(data): def cli_node_info_format_pretty(CLI_CONFIG, data):
""" """
Pretty format the basic output of cli_node_info Pretty format the basic output of cli_node_info
""" """
return node_format_info(data, long_output=False) return node_format_info(CLI_CONFIG, data, long_output=False)
def cli_node_info_format_long(data): def cli_node_info_format_long(CLI_CONFIG, data):
""" """
Pretty format the full output of cli_node_info Pretty format the full output of cli_node_info
""" """
return node_format_info(data, long_output=True) return node_format_info(CLI_CONFIG, data, long_output=True)
def cli_node_list_format_pretty(data): def cli_node_list_format_pretty(CLI_CONFIG, data):
""" """
Pretty format the output of cli_node_list Pretty format the output of cli_node_list
""" """
return node_format_list(data) return node_format_list(CLI_CONFIG, data)
def cli_vm_tag_get_format_pretty(CLI_CONFIG, data):
"""
Pretty format the output of cli_vm_tag_get
"""
return vm_format_tags(CLI_CONFIG, data)
def cli_vm_vcpu_get_format_pretty(CLI_CONFIG, data):
"""
Pretty format the output of cli_vm_vcpu_get
"""
return vm_format_vcpus(CLI_CONFIG, data)
def cli_vm_memory_get_format_pretty(CLI_CONFIG, data):
"""
Pretty format the output of cli_vm_memory_get
"""
return vm_format_memory(CLI_CONFIG, data)
def cli_vm_network_get_format_pretty(CLI_CONFIG, data):
"""
Pretty format the output of cli_vm_network_get
"""
return vm_format_networks(CLI_CONFIG, data)
def cli_vm_volume_get_format_pretty(CLI_CONFIG, data):
"""
Pretty format the output of cli_vm_volume_get
"""
return vm_format_volumes(CLI_CONFIG, data)
def cli_vm_info_format_pretty(CLI_CONFIG, data):
"""
Pretty format the basic output of cli_vm_info
"""
return vm_format_info(CLI_CONFIG, data, long_output=False)
def cli_vm_info_format_long(CLI_CONFIG, data):
"""
Pretty format the full output of cli_vm_info
"""
return vm_format_info(CLI_CONFIG, data, long_output=True)
def cli_vm_list_format_pretty(CLI_CONFIG, data):
"""
Pretty format the output of cli_vm_list
"""
return vm_format_list(CLI_CONFIG, data)

View File

@ -273,7 +273,7 @@ def getOutputColours(node_information):
) )
def format_info(node_information, long_output): def format_info(config, node_information, long_output):
( (
health_colour, health_colour,
daemon_state_colour, daemon_state_colour,
@ -442,7 +442,7 @@ def format_info(node_information, long_output):
return "\n".join(ainformation) return "\n".join(ainformation)
def format_list(node_list): def format_list(config, node_list):
if node_list == "Node not found.": if node_list == "Node not found.":
return node_list return node_list

View File

@ -286,20 +286,18 @@ def vm_tag_set(config, vm, action, tag, protected=False):
return retstatus, response.json().get("message", "") return retstatus, response.json().get("message", "")
def format_vm_tags(config, name, tags): def format_vm_tags(config, data):
""" """
Format the output of a tags dictionary in a nice table Format the output of a tags dictionary in a nice table
""" """
tags = data.get("tags", [])
if len(tags) < 1: if len(tags) < 1:
return "No tags found." return "No tags found."
output_list = [] output_list = []
name_length = 5
_name_length = len(name) + 1
if _name_length > name_length:
name_length = _name_length
tags_name_length = 4 tags_name_length = 4
tags_type_length = 5 tags_type_length = 5
tags_protected_length = 10 tags_protected_length = 10
@ -495,44 +493,38 @@ def vm_vcpus_get(config, vm):
except Exception: except Exception:
return False, "ERROR: Failed to parse XML data." return False, "ERROR: Failed to parse XML data."
vm_vcpus = int(parsed_xml.vcpu.text) data = dict()
vm_sockets = parsed_xml.cpu.topology.attrib.get("sockets") data["name"] = vm
vm_cores = parsed_xml.cpu.topology.attrib.get("cores") data["vcpus"] = int(parsed_xml.vcpu.text)
vm_threads = parsed_xml.cpu.topology.attrib.get("threads") data["sockets"] = parsed_xml.cpu.topology.attrib.get("sockets")
data["cores"] = parsed_xml.cpu.topology.attrib.get("cores")
data["threads"] = parsed_xml.cpu.topology.attrib.get("threads")
return True, (vm_vcpus, (vm_sockets, vm_cores, vm_threads)) return True, data
def format_vm_vcpus(config, name, vcpus): def format_vm_vcpus(config, data):
""" """
Format the output of a vCPU value in a nice table Format the output of a vCPU value in a nice table
""" """
output_list = [] output_list = []
name_length = 5
_name_length = len(name) + 1
if _name_length > name_length:
name_length = _name_length
vcpus_length = 6 vcpus_length = 6
sockets_length = 8 sockets_length = 8
cores_length = 6 cores_length = 6
threads_length = 8 threads_length = 8
output_list.append( output_list.append(
"{bold}{name: <{name_length}} \ "{bold}{vcpus: <{vcpus_length}} \
{vcpus: <{vcpus_length}} \
{sockets: <{sockets_length}} \ {sockets: <{sockets_length}} \
{cores: <{cores_length}} \ {cores: <{cores_length}} \
{threads: <{threads_length}}{end_bold}".format( {threads: <{threads_length}}{end_bold}".format(
name_length=name_length,
vcpus_length=vcpus_length, vcpus_length=vcpus_length,
sockets_length=sockets_length, sockets_length=sockets_length,
cores_length=cores_length, cores_length=cores_length,
threads_length=threads_length, threads_length=threads_length,
bold=ansiprint.bold(), bold=ansiprint.bold(),
end_bold=ansiprint.end(), end_bold=ansiprint.end(),
name="Name",
vcpus="vCPUs", vcpus="vCPUs",
sockets="Sockets", sockets="Sockets",
cores="Cores", cores="Cores",
@ -540,23 +532,20 @@ def format_vm_vcpus(config, name, vcpus):
) )
) )
output_list.append( output_list.append(
"{bold}{name: <{name_length}} \ "{bold}{vcpus: <{vcpus_length}} \
{vcpus: <{vcpus_length}} \
{sockets: <{sockets_length}} \ {sockets: <{sockets_length}} \
{cores: <{cores_length}} \ {cores: <{cores_length}} \
{threads: <{threads_length}}{end_bold}".format( {threads: <{threads_length}}{end_bold}".format(
name_length=name_length,
vcpus_length=vcpus_length, vcpus_length=vcpus_length,
sockets_length=sockets_length, sockets_length=sockets_length,
cores_length=cores_length, cores_length=cores_length,
threads_length=threads_length, threads_length=threads_length,
bold="", bold="",
end_bold="", end_bold="",
name=name, vcpus=data["vcpus"],
vcpus=vcpus[0], sockets=data["sockets"],
sockets=vcpus[1][0], cores=data["cores"],
cores=vcpus[1][1], threads=data["threads"],
threads=vcpus[1][2],
) )
) )
return "\n".join(output_list) return "\n".join(output_list)
@ -619,44 +608,35 @@ def vm_memory_get(config, vm):
except Exception: except Exception:
return False, "ERROR: Failed to parse XML data." return False, "ERROR: Failed to parse XML data."
vm_memory = int(parsed_xml.memory.text) data = dict()
data["name"] = vm
data["memory"] = int(parsed_xml.memory.text)
return True, vm_memory return True, data
def format_vm_memory(config, name, memory): def format_vm_memory(config, data):
""" """
Format the output of a memory value in a nice table Format the output of a memory value in a nice table
""" """
output_list = [] output_list = []
name_length = 5
_name_length = len(name) + 1
if _name_length > name_length:
name_length = _name_length
memory_length = 6 memory_length = 6
output_list.append( output_list.append(
"{bold}{name: <{name_length}} \ "{bold}{memory: <{memory_length}}{end_bold}".format(
{memory: <{memory_length}}{end_bold}".format(
name_length=name_length,
memory_length=memory_length, memory_length=memory_length,
bold=ansiprint.bold(), bold=ansiprint.bold(),
end_bold=ansiprint.end(), end_bold=ansiprint.end(),
name="Name",
memory="RAM (M)", memory="RAM (M)",
) )
) )
output_list.append( output_list.append(
"{bold}{name: <{name_length}} \ "{bold}{memory: <{memory_length}}{end_bold}".format(
{memory: <{memory_length}}{end_bold}".format(
name_length=name_length,
memory_length=memory_length, memory_length=memory_length,
bold="", bold="",
end_bold="", end_bold="",
name=name, memory=data["memory"],
memory=memory,
) )
) )
return "\n".join(output_list) return "\n".join(output_list)
@ -946,7 +926,9 @@ def vm_networks_get(config, vm):
except Exception: except Exception:
return False, "ERROR: Failed to parse XML data." return False, "ERROR: Failed to parse XML data."
network_data = list() data = dict()
data["name"] = vm
data["networks"] = list()
for interface in parsed_xml.devices.find("interface"): for interface in parsed_xml.devices.find("interface"):
mac_address = interface.mac.attrib.get("address") mac_address = interface.mac.attrib.get("address")
model = interface.model.attrib.get("type") model = interface.model.attrib.get("type")
@ -960,76 +942,65 @@ def vm_networks_get(config, vm):
elif interface_type == "hostdev": elif interface_type == "hostdev":
network = "hostdev:{}".format(interface.source.attrib.get("dev")) network = "hostdev:{}".format(interface.source.attrib.get("dev"))
network_data.append((network, mac_address, model)) data["networks"].append(
{"network": network, "mac_address": mac_address, "model": model}
)
return True, network_data return True, data
def format_vm_networks(config, name, networks): def format_vm_networks(config, data):
""" """
Format the output of a network list in a nice table Format the output of a network list in a nice table
""" """
output_list = [] output_list = []
name_length = 5 network_length = 8
vni_length = 8
macaddr_length = 12 macaddr_length = 12
model_length = 6 model_length = 6
_name_length = len(name) + 1 for network in data["networks"]:
if _name_length > name_length: _network_length = len(network["network"]) + 1
name_length = _name_length if _network_length > network_length:
network_length = _network_length
for network in networks: _macaddr_length = len(network["mac_address"]) + 1
_vni_length = len(network[0]) + 1
if _vni_length > vni_length:
vni_length = _vni_length
_macaddr_length = len(network[1]) + 1
if _macaddr_length > macaddr_length: if _macaddr_length > macaddr_length:
macaddr_length = _macaddr_length macaddr_length = _macaddr_length
_model_length = len(network[2]) + 1 _model_length = len(network["model"]) + 1
if _model_length > model_length: if _model_length > model_length:
model_length = _model_length model_length = _model_length
output_list.append( output_list.append(
"{bold}{name: <{name_length}} \ "{bold}{network: <{network_length}} \
{vni: <{vni_length}} \
{macaddr: <{macaddr_length}} \ {macaddr: <{macaddr_length}} \
{model: <{model_length}}{end_bold}".format( {model: <{model_length}}{end_bold}".format(
name_length=name_length, network_length=network_length,
vni_length=vni_length,
macaddr_length=macaddr_length, macaddr_length=macaddr_length,
model_length=model_length, model_length=model_length,
bold=ansiprint.bold(), bold=ansiprint.bold(),
end_bold=ansiprint.end(), end_bold=ansiprint.end(),
name="Name", network="Network",
vni="Network",
macaddr="MAC Address", macaddr="MAC Address",
model="Model", model="Model",
) )
) )
count = 0 count = 0
for network in networks: for network in data["networks"]:
if count > 0:
name = ""
count += 1 count += 1
output_list.append( output_list.append(
"{bold}{name: <{name_length}} \ "{bold}{network: <{network_length}} \
{vni: <{vni_length}} \
{macaddr: <{macaddr_length}} \ {macaddr: <{macaddr_length}} \
{model: <{model_length}}{end_bold}".format( {model: <{model_length}}{end_bold}".format(
name_length=name_length, network_length=network_length,
vni_length=vni_length,
macaddr_length=macaddr_length, macaddr_length=macaddr_length,
model_length=model_length, model_length=model_length,
bold="", bold="",
end_bold="", end_bold="",
name=name, network=network["network"],
vni=network[0], macaddr=network["mac_address"],
macaddr=network[1], model=network["model"],
model=network[2],
) )
) )
return "\n".join(output_list) return "\n".join(output_list)
@ -1270,7 +1241,9 @@ def vm_volumes_get(config, vm):
except Exception: except Exception:
return False, "ERROR: Failed to parse XML data." return False, "ERROR: Failed to parse XML data."
volume_data = list() data = dict()
data["name"] = vm
data["volumes"] = list()
for disk in parsed_xml.devices.find("disk"): for disk in parsed_xml.devices.find("disk"):
protocol = disk.attrib.get("type") protocol = disk.attrib.get("type")
disk_id = disk.target.attrib.get("dev") disk_id = disk.target.attrib.get("dev")
@ -1285,58 +1258,52 @@ def vm_volumes_get(config, vm):
protocol = "unknown" protocol = "unknown"
source = "unknown" source = "unknown"
volume_data.append((source, disk_id, protocol, bus)) data["volumes"].append(
{"volume": source, "disk_id": disk_id, "protocol": protocol, "bus": bus}
)
return True, volume_data return True, data
def format_vm_volumes(config, name, volumes): def format_vm_volumes(config, data):
""" """
Format the output of a volume value in a nice table Format the output of a volume value in a nice table
""" """
output_list = [] output_list = []
name_length = 5
volume_length = 7 volume_length = 7
disk_id_length = 4 disk_id_length = 4
protocol_length = 5 protocol_length = 5
bus_length = 4 bus_length = 4
_name_length = len(name) + 1 for volume in data["volumes"]:
if _name_length > name_length: _volume_length = len(volume["volume"]) + 1
name_length = _name_length
for volume in volumes:
_volume_length = len(volume[0]) + 1
if _volume_length > volume_length: if _volume_length > volume_length:
volume_length = _volume_length volume_length = _volume_length
_disk_id_length = len(volume[1]) + 1 _disk_id_length = len(volume["disk_id"]) + 1
if _disk_id_length > disk_id_length: if _disk_id_length > disk_id_length:
disk_id_length = _disk_id_length disk_id_length = _disk_id_length
_protocol_length = len(volume[2]) + 1 _protocol_length = len(volume["protocol"]) + 1
if _protocol_length > protocol_length: if _protocol_length > protocol_length:
protocol_length = _protocol_length protocol_length = _protocol_length
_bus_length = len(volume[3]) + 1 _bus_length = len(volume["bus"]) + 1
if _bus_length > bus_length: if _bus_length > bus_length:
bus_length = _bus_length bus_length = _bus_length
output_list.append( output_list.append(
"{bold}{name: <{name_length}} \ "{bold}{volume: <{volume_length}} \
{volume: <{volume_length}} \
{disk_id: <{disk_id_length}} \ {disk_id: <{disk_id_length}} \
{protocol: <{protocol_length}} \ {protocol: <{protocol_length}} \
{bus: <{bus_length}}{end_bold}".format( {bus: <{bus_length}}{end_bold}".format(
name_length=name_length,
volume_length=volume_length, volume_length=volume_length,
disk_id_length=disk_id_length, disk_id_length=disk_id_length,
protocol_length=protocol_length, protocol_length=protocol_length,
bus_length=bus_length, bus_length=bus_length,
bold=ansiprint.bold(), bold=ansiprint.bold(),
end_bold=ansiprint.end(), end_bold=ansiprint.end(),
name="Name",
volume="Volume", volume="Volume",
disk_id="Dev", disk_id="Dev",
protocol="Type", protocol="Type",
@ -1344,28 +1311,23 @@ def format_vm_volumes(config, name, volumes):
) )
) )
count = 0 count = 0
for volume in volumes: for volume in data["volumes"]:
if count > 0:
name = ""
count += 1 count += 1
output_list.append( output_list.append(
"{bold}{name: <{name_length}} \ "{bold}{volume: <{volume_length}} \
{volume: <{volume_length}} \
{disk_id: <{disk_id_length}} \ {disk_id: <{disk_id_length}} \
{protocol: <{protocol_length}} \ {protocol: <{protocol_length}} \
{bus: <{bus_length}}{end_bold}".format( {bus: <{bus_length}}{end_bold}".format(
name_length=name_length,
volume_length=volume_length, volume_length=volume_length,
disk_id_length=disk_id_length, disk_id_length=disk_id_length,
protocol_length=protocol_length, protocol_length=protocol_length,
bus_length=bus_length, bus_length=bus_length,
bold="", bold="",
end_bold="", end_bold="",
name=name, volume=volume["volume"],
volume=volume[0], disk_id=volume["disk_id"],
disk_id=volume[1], protocol=volume["protocol"],
protocol=volume[2], bus=volume["bus"],
bus=volume[3],
) )
) )
return "\n".join(output_list) return "\n".join(output_list)
@ -1869,7 +1831,7 @@ def format_info(config, domain_information, long_output):
return "\n".join(ainformation) return "\n".join(ainformation)
def format_list(config, vm_list, raw): def format_list(config, vm_list):
# Function to strip the "br" off of nets and return a nicer list # Function to strip the "br" off of nets and return a nicer list
def getNiceNetID(domain_information): def getNiceNetID(domain_information):
# Network list # Network list
@ -1888,13 +1850,6 @@ def format_list(config, vm_list, raw):
tag_list.append(tag["name"]) tag_list.append(tag["name"])
return tag_list return tag_list
# Handle raw mode since it just lists the names
if raw:
ainformation = list()
for vm in sorted(item["name"] for item in vm_list):
ainformation.append(vm)
return "\n".join(ainformation)
vm_list_output = [] vm_list_output = []
# Determine optimal column widths # Determine optimal column widths