# Buggy code
def workgroup(name):
    """
    .. versionadded:: 3001

    Manage the workgroup of the computer

    Args:
        name (str): The workgroup to set

    Example:

    .. code-block:: yaml

        set workgroup:
          system.workgroup:
            - name: local
    """
    ret = {"name": name.upper(), "result": False, "changes": {}, "comment": ""}

    # Grab the current domain/workgroup
    out = __salt__["system.get_domain_workgroup"]()
    current_workgroup = (
        out["Domain"]
        if "Domain" in out
        else out["Workgroup"]
        if "Workgroup" in out
        else ""
    )

    # Notify the user if the requested workgroup is the same
    if current_workgroup.upper() == name.upper():
        ret["result"] = True
        ret["comment"] = "Workgroup is already set to '{0}'".format(name.upper())
        return ret

    # If being run in test-mode, inform the user what is supposed to happen
    if __opts__["test"]:
        ret["result"] = None
        ret["changes"] = {}
        ret["comment"] = "Computer will be joined to workgroup '{0}'".format(name)
        return ret

    # Set our new workgroup, and then immediately ask the machine what it
    # is again to validate the change
    res = __salt__["system.set_domain_workgroup"](name.upper())
    out = __salt__["system.get_domain_workgroup"]()
    new_workgroup = (
        out["Domain"]
        if "Domain" in out
        else out["Workgroup"]
        if "Workgroup" in out
        else ""
    )

    # Return our results based on the changes
    ret = {}
    if res and current_workgroup.upper() == new_workgroup.upper():
        ret["result"] = True
        ret["comment"] = "The new workgroup '{0}' is the same as '{1}'".format(
            current_workgroup.upper(), new_workgroup.upper()
        )
    elif res:
        ret["result"] = True
        ret["comment"] = "The workgroup has been changed from '{0}' to '{1}'".format(
            current_workgroup.upper(), new_workgroup.upper()
        )
        ret["changes"] = {
            "old": current_workgroup.upper(),
            "new": new_workgroup.upper(),
        }
    else:
        ret["result"] = False
        ret["comment"] = "Unable to join the requested workgroup '{0}'".format(
            new_workgroup.upper()
        )

    return ret


# Traceback (KeyError)
2020-06-25 00:53:54,081 [salt.utils.decorators.state:39  ][DEBUG   ][3532] An exception occurred in this state: The following keys were not present in the state return: name.
Traceback (most recent call last):
File "c:\python37\lib\site-packages\salt\utils\decorators\state.py", line 34, in _func
result = pls(result)
File "c:\python37\lib\site-packages\salt\utils\decorators\state.py", line 78, in content_check
raise SaltException(err_msg)
salt.exceptions.SaltException: The following keys were not present in the state return: name.
Traceback (most recent call last):
File "c:\python37\lib\site-packages\salt\utils\decorators\state.py", line 34, in _func
result = pls(result)
File "c:\python37\lib\site-packages\salt\utils\decorators\state.py", line 78, in content_check
raise SaltException(err_msg)
salt.exceptions.SaltException: The following keys were not present in the state return: name.
2020-06-25 00:53:54,081 [salt.minion      :1949][WARNING ][3532] The minion function caused an exception
Traceback (most recent call last):
File "c:\python37\lib\site-packages\salt\minion.py", line 1870, in _thread_return
opts, data, func, args, kwargs
File "c:\python37\lib\site-packages\salt\executors\direct_call.py", line 12, in execute
return func(*args, **kwargs)
File "c:\python37\lib\site-packages\salt\modules\state.py", line 1129, in highstate
orchestration_jid=orchestration_jid,
File "c:\python37\lib\site-packages\salt\state.py", line 4468, in call_highstate
return self.state.call_high(high, orchestration_jid)
File "c:\python37\lib\site-packages\salt\state.py", line 3209, in call_high
ret = self.call_chunks(chunks)
File "c:\python37\lib\site-packages\salt\state.py", line 2481, in call_chunks
running = self.call_chunk(low, running, chunks)
File "c:\python37\lib\site-packages\salt\state.py", line 3084, in call_chunk
self.event(running[tag], len(chunks), fire_event=low.get("fire_event"))
File "c:\python37\lib\site-packages\salt\state.py", line 2828, in event
six.text_type(chunk_ret["__run_num__"]),
KeyError: '__run_num__'
2020-06-25 00:53:54,081 [salt.utils.event :319 ][DEBUG   ][3532] SaltEvent PUB socket URI: 4510
2020-06-25 00:53:54,081 [salt.utils.event :320 ][DEBUG   ][3532] SaltEvent PULL socket URI: 4511
2020-06-25 00:53:54,081 [salt.transport.ipc:40  ][TRACE   ][3532] IPCClient: Connecting to socket: 4511

# Fixed code
def workgroup(name):
    """
    .. versionadded:: 3001

    Manage the workgroup of the computer

    Args:
        name (str): The workgroup to set

    Example:

    .. code-block:: yaml

        set workgroup:
          system.workgroup:
            - name: local
    """
    ret = {"name": name.upper(), "result": False, "changes": {}, "comment": ""}

    # Grab the current domain/workgroup
    out = __salt__["system.get_domain_workgroup"]()
    current_workgroup = (
        out["Domain"]
        if "Domain" in out
        else out["Workgroup"]
        if "Workgroup" in out
        else ""
    )

    # Notify the user if the requested workgroup is the same
    if current_workgroup.upper() == name.upper():
        ret["result"] = True
        ret["comment"] = "Workgroup is already set to '{0}'".format(name.upper())
        return ret

    # If being run in test-mode, inform the user what is supposed to happen
    if __opts__["test"]:
        ret["result"] = None
        ret["changes"] = {}
        ret["comment"] = "Computer will be joined to workgroup '{0}'".format(name)
        return ret

    # Set our new workgroup, and then immediately ask the machine what it
    # is again to validate the change
    res = __salt__["system.set_domain_workgroup"](name.upper())
    out = __salt__["system.get_domain_workgroup"]()
    new_workgroup = (
        out["Domain"]
        if "Domain" in out
        else out["Workgroup"]
        if "Workgroup" in out
        else ""
    )

    # Return our results based on the changes
    if res and current_workgroup.upper() == new_workgroup.upper():
        ret["result"] = True
        ret["comment"] = "The new workgroup '{0}' is the same as '{1}'".format(
            current_workgroup.upper(), new_workgroup.upper()
        )
    elif res:
        ret["result"] = True
        ret["comment"] = "The workgroup has been changed from '{0}' to '{1}'".format(
            current_workgroup.upper(), new_workgroup.upper()
        )
        ret["changes"] = {
            "old": current_workgroup.upper(),
            "new": new_workgroup.upper(),
        }
    else:
        ret["result"] = False
        ret["comment"] = "Unable to join the requested workgroup '{0}'".format(
            new_workgroup.upper()
        )

    return ret