From 5a3b78f053380c918b436f11559ac84228b00373 Mon Sep 17 00:00:00 2001 From: Georg Pfuetzenreuter Date: Wed, 10 Jan 2024 23:13:29 +0100 Subject: [PATCH 1/2] Repair virt_query outputter Existing code was not functional. Only return if a dictionary is passed and reference the correct data. Signed-off-by: Georg Pfuetzenreuter (cherry picked from commit e3c365ad8f385121aa878950e13892d986d79656) --- changelog/65841.fixed.md | 1 + salt/output/virt_query.py | 63 ++++++++++++++++++++------------------- 2 files changed, 33 insertions(+), 31 deletions(-) create mode 100644 changelog/65841.fixed.md diff --git a/changelog/65841.fixed.md b/changelog/65841.fixed.md new file mode 100644 index 00000000000..5ae34b3c976 --- /dev/null +++ b/changelog/65841.fixed.md @@ -0,0 +1 @@ +Restore functionality of virt_query outputter. diff --git a/salt/output/virt_query.py b/salt/output/virt_query.py index d20e6357e60..1bbbe52aee0 100644 --- a/salt/output/virt_query.py +++ b/salt/output/virt_query.py @@ -12,35 +12,36 @@ def output(data, **kwargs): # pylint: disable=unused-argument Display output for the salt-run virt.query function """ out = "" - for id_ in data["data"]: - out += "{}\n".format(id_) - for vm_ in data["data"][id_]["vm_info"]: - out += " {}\n".format(vm_) - vm_data = data[id_]["vm_info"][vm_] - if "cpu" in vm_data: - out += " CPU: {}\n".format(vm_data["cpu"]) - if "mem" in vm_data: - out += " Memory: {}\n".format(vm_data["mem"]) - if "state" in vm_data: - out += " State: {}\n".format(vm_data["state"]) - if "graphics" in vm_data: - if vm_data["graphics"].get("type", "") == "vnc": - out += " Graphics: vnc - {}:{}\n".format( - id_, vm_data["graphics"]["port"] - ) - if "disks" in vm_data: - for disk, d_data in vm_data["disks"].items(): - out += " Disk - {}:\n".format(disk) - out += " Size: {}\n".format(d_data["disk size"]) - out += " File: {}\n".format(d_data["file"]) - out += " File Format: {}\n".format(d_data["file format"]) - if "nics" in vm_data: - for mac in vm_data["nics"]: - out += " Nic - {}:\n".format(mac) - out += " Source: {}\n".format( - vm_data["nics"][mac]["source"][ - next(iter(vm_data["nics"][mac]["source"].keys())) - ] - ) - out += " Type: {}\n".format(vm_data["nics"][mac]["type"]) + if isinstance(data, dict) and "event" in data: + for id_ in data["event"]["data"]: + out += "{}\n".format(id_) + for vm_ in data["event"]["data"][id_]["vm_info"]: + out += " {}\n".format(vm_) + vm_data = data["event"]["data"][id_]["vm_info"][vm_] + if "cpu" in vm_data: + out += " CPU: {}\n".format(vm_data["cpu"]) + if "mem" in vm_data: + out += " Memory: {}\n".format(vm_data["mem"]) + if "state" in vm_data: + out += " State: {}\n".format(vm_data["state"]) + if "graphics" in vm_data: + if vm_data["graphics"].get("type", "") == "vnc": + out += " Graphics: vnc - {}:{}\n".format( + id_, vm_data["graphics"]["port"] + ) + if "disks" in vm_data: + for disk, d_data in vm_data["disks"].items(): + out += " Disk - {}:\n".format(disk) + out += " Size: {}\n".format(d_data["disk size"]) + out += " File: {}\n".format(d_data["file"]) + out += " File Format: {}\n".format(d_data["file format"]) + if "nics" in vm_data: + for mac in vm_data["nics"]: + out += " NIC - {}:\n".format(mac) + out += " Source: {}\n".format( + vm_data["nics"][mac]["source"][ + next(iter(vm_data["nics"][mac]["source"].keys())) + ] + ) + out += " Type: {}\n".format(vm_data["nics"][mac]["type"]) return out From 650518ef8ae2b54272b2a1f4577773e7b70f5b13 Mon Sep 17 00:00:00 2001 From: Georg Pfuetzenreuter Date: Wed, 10 Jan 2024 23:23:12 +0100 Subject: [PATCH 2/2] Facilitate block devices in virt_query outputter Disk data in Libvirt VMs does not contain a disk size if the disk references a block device. Skip the field for such disks instead of failing with a key error. Signed-off-by: Georg Pfuetzenreuter (cherry picked from commit ed73abd44117ad745e9c91f2b33caf04402b117c) --- changelog/65841.fixed.md | 2 +- salt/output/virt_query.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/changelog/65841.fixed.md b/changelog/65841.fixed.md index 5ae34b3c976..7fb6336ea18 100644 --- a/changelog/65841.fixed.md +++ b/changelog/65841.fixed.md @@ -1 +1 @@ -Restore functionality of virt_query outputter. +Restore functionality of virt_query outputter and add support for block devices. diff --git a/salt/output/virt_query.py b/salt/output/virt_query.py index 1bbbe52aee0..0f989fedfa7 100644 --- a/salt/output/virt_query.py +++ b/salt/output/virt_query.py @@ -32,7 +32,8 @@ def output(data, **kwargs): # pylint: disable=unused-argument if "disks" in vm_data: for disk, d_data in vm_data["disks"].items(): out += " Disk - {}:\n".format(disk) - out += " Size: {}\n".format(d_data["disk size"]) + if "disk size" in d_data: + out += " Size: {}\n".format(d_data["disk size"]) out += " File: {}\n".format(d_data["file"]) out += " File Format: {}\n".format(d_data["file format"]) if "nics" in vm_data: