# Buggy code
def get_nics(vm_):
    """
    Return info about the network interfaces of a named vm

    CLI Example:

    .. code-block:: bash

        salt '*' virt.get_nics <vm name>
    """
    nics = {}
    doc = minidom.parse(_StringIO(get_xml(vm_)))
    for node in doc.getElementsByTagName("devices"):
        i_nodes = node.getElementsByTagName("interface")
        for i_node in i_nodes:
            nic = {}
            nic["type"] = i_node.getAttribute("type")
            for v_node in i_node.getElementsByTagName("*"):
                if v_node.tagName == "mac":
                    nic["mac"] = v_node.getAttribute("address")
                if v_node.tagName == "model":
                    nic["model"] = v_node.getAttribute("type")
                if v_node.tagName == "target":
                    nic["target"] = v_node.getAttribute("dev")
                # driver, source, and match can all have optional attributes
                if re.match("(driver|source|address)", v_node.tagName):
                    temp = {}
                    for key in v_node.attributes:
                        temp[key] = v_node.getAttribute(key)
                    nic[str(v_node.tagName)] = temp
                # virtualport needs to be handled separately, to pick up the
                # type attribute of the virtualport itself
                if v_node.tagName == "virtualport":
                    temp = {}
                    temp["type"] = v_node.getAttribute("type")
                    for key in v_node.attributes:
                        temp[key] = v_node.getAttribute(key)
                    nic["virtualport"] = temp
            if "mac" not in nic:
                continue
            nics[nic["mac"]] = nic
    return nics


# Traceback (KeyError)
# salt x01 virt.full_info
x01:
The minion function caused an exception: Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/minion.py", line 1022, in _thread_return
return_data = func(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/modules/virt.py", line 1076, in full_info
'vm_info': vm_info()}
File "/usr/lib/python2.7/dist-packages/salt/modules/virt.py", line 720, in vm_info
info[vm_] = _info(vm_)
File "/usr/lib/python2.7/dist-packages/salt/modules/virt.py", line 710, in _info
'graphics': get_graphics(vm_),
File "/usr/lib/python2.7/dist-packages/salt/modules/virt.py", line 860, in get_graphics
for key in g_node.attributes:
File "/usr/lib/python2.7/xml/dom/minidom.py", line 522, in __getitem__
return self._attrs[attname_or_tuple]
KeyError: 0

# Fixed code
def get_nics(vm_):
    """
    Return info about the network interfaces of a named vm

    CLI Example:

    .. code-block:: bash

        salt '*' virt.get_nics <vm name>
    """
    nics = {}
    doc = minidom.parse(_StringIO(get_xml(vm_)))
    for node in doc.getElementsByTagName("devices"):
        i_nodes = node.getElementsByTagName("interface")
        for i_node in i_nodes:
            nic = {}
            nic["type"] = i_node.getAttribute("type")
            for v_node in i_node.getElementsByTagName("*"):
                if v_node.tagName == "mac":
                    nic["mac"] = v_node.getAttribute("address")
                if v_node.tagName == "model":
                    nic["model"] = v_node.getAttribute("type")
                if v_node.tagName == "target":
                    nic["target"] = v_node.getAttribute("dev")
                # driver, source, and match can all have optional attributes
                if re.match("(driver|source|address)", v_node.tagName):
                    temp = {}
                    for key, value in v_node.attributes.items():
                        temp[key] = value
                    nic[str(v_node.tagName)] = temp
                # virtualport needs to be handled separately, to pick up the
                # type attribute of the virtualport itself
                if v_node.tagName == "virtualport":
                    temp = {}
                    temp["type"] = v_node.getAttribute("type")
                    for key in v_node.attributes:
                        temp[key] = v_node.getAttribute(key)
                    nic["virtualport"] = temp
            if "mac" not in nic:
                continue
            nics[nic["mac"]] = nic
    return nics