# Buggy code
def output(data):
    """
    Display output for the salt-run virt.query function
    """
    out = ""
    for id_ in data:
        out += "{0}\n".format(id_)
        for vm_ in data[id_]["vm_info"]:
            out += "  {0}\n".format(vm_)
            vm_data = data[id_]["vm_info"][vm_]
            if "cpu" in vm_data:
                out += "    CPU: {0}\n".format(vm_data["cpu"])
            if "mem" in vm_data:
                out += "    Memory: {0}\n".format(vm_data["mem"])
            if "state" in vm_data:
                out += "    State: {0}\n".format(vm_data["state"])
            if "graphics" in vm_data:
                if vm_data["graphics"].get("type", "") == "vnc":
                    out += "    Graphics: vnc - {0}:{1}\n".format(
                        id_, vm_data["graphics"]["port"]
                    )
            if "disks" in vm_data:
                for disk, d_data in vm_data["disks"].items():
                    out += "    Disk - {0}:\n".format(disk)
                    out += "      Size: {0}\n".format(d_data["disk size"])
                    out += "      File: {0}\n".format(d_data["file"])
                    out += "      File Format: {0}\n".format(d_data["file format"])
            if "nics" in vm_data:
                for mac in vm_data["nics"]:
                    out += "    Nic - {0}:\n".format(mac)
                    out += "      Source: {0}\n".format(
                        vm_data["nics"][mac]["source"][
                            vm_data["nics"][mac]["source"].iterkeys().next()
                        ]
                    )
                    out += "      Type: {0}\n".format(vm_data["nics"][mac]["type"])
    return out


# Traceback (TypeError)
[root@*******-ecp states]# salt-run virt.query
[ERROR   ] An un-handled exception was caught by salt's global exception handler:
TypeError: string indices must be integers, not str
Traceback (most recent call last):
File "/usr/bin/salt-run", line 10, in <module>
salt_run()
File "/usr/lib/python2.6/site-packages/salt/scripts.py", line 90, in salt_run
client.run()
File "/usr/lib/python2.6/site-packages/salt/cli/__init__.py", line 346, in run
runner.run()
File "/usr/lib/python2.6/site-packages/salt/runner.py", line 153, in run
self.opts['fun'], self.opts['arg'], self.opts)
File "/usr/lib/python2.6/site-packages/salt/runner.py", line 85, in cmd
return self.functions[fun](*args, **kwargs)
File "/usr/lib/python2.6/site-packages/salt/runners/virt.py", line 76, in query
salt.output.display_output(chunk, 'virt_query', __opts__)
File "/usr/lib/python2.6/site-packages/salt/output/__init__.py", line 30, in display_output
display_data = get_printout(out, opts)(data).rstrip()
File "/usr/lib/python2.6/site-packages/salt/output/virt_query.py", line 14, in output
for vm_ in data[id_]['vm_info']:
TypeError: string indices must be integers, not str
Traceback (most recent call last):
File "/usr/bin/salt-run", line 10, in <module>
salt_run()
File "/usr/lib/python2.6/site-packages/salt/scripts.py", line 90, in salt_run
client.run()
File "/usr/lib/python2.6/site-packages/salt/cli/__init__.py", line 346, in run
runner.run()
File "/usr/lib/python2.6/site-packages/salt/runner.py", line 153, in run
self.opts['fun'], self.opts['arg'], self.opts)
File "/usr/lib/python2.6/site-packages/salt/runner.py", line 85, in cmd
return self.functions[fun](*args, **kwargs)
File "/usr/lib/python2.6/site-packages/salt/runners/virt.py", line 76, in query
salt.output.display_output(chunk, 'virt_query', __opts__)
File "/usr/lib/python2.6/site-packages/salt/output/__init__.py", line 30, in display_output
display_data = get_printout(out, opts)(data).rstrip()
File "/usr/lib/python2.6/site-packages/salt/output/virt_query.py", line 14, in output
for vm_ in data[id_]['vm_info']:
TypeError: string indices must be integers, not str

# Fixed code
def output(data):
    """
    Display output for the salt-run virt.query function
    """
    out = ""
    for id_ in data["data"]:
        out += "{0}\n".format(id_)
        for vm_ in data["data"][id_]["vm_info"]:
            out += "  {0}\n".format(vm_)
            vm_data = data[id_]["vm_info"][vm_]
            if "cpu" in vm_data:
                out += "    CPU: {0}\n".format(vm_data["cpu"])
            if "mem" in vm_data:
                out += "    Memory: {0}\n".format(vm_data["mem"])
            if "state" in vm_data:
                out += "    State: {0}\n".format(vm_data["state"])
            if "graphics" in vm_data:
                if vm_data["graphics"].get("type", "") == "vnc":
                    out += "    Graphics: vnc - {0}:{1}\n".format(
                        id_, vm_data["graphics"]["port"]
                    )
            if "disks" in vm_data:
                for disk, d_data in vm_data["disks"].items():
                    out += "    Disk - {0}:\n".format(disk)
                    out += "      Size: {0}\n".format(d_data["disk size"])
                    out += "      File: {0}\n".format(d_data["file"])
                    out += "      File Format: {0}\n".format(d_data["file format"])
            if "nics" in vm_data:
                for mac in vm_data["nics"]:
                    out += "    Nic - {0}:\n".format(mac)
                    out += "      Source: {0}\n".format(
                        vm_data["nics"][mac]["source"][
                            vm_data["nics"][mac]["source"].iterkeys().next()
                        ]
                    )
                    out += "      Type: {0}\n".format(vm_data["nics"][mac]["type"])
    return out