# Buggy code
def delete_key(
    keyid=None, fingerprint=None, delete_secret=False, user=None, gnupghome=None
):
    """
    Get a key from the GPG keychain

    keyid
        The keyid of the key to be deleted.

    fingerprint
        The fingerprint of the key to be deleted.

    delete_secret
        Whether to delete a corresponding secret key prior to deleting the public key.
        Secret keys must be deleted before deleting any corresponding public keys.

    user
        Which user's keychain to access, defaults to user Salt is running as.
        Passing the user as ``salt`` will set the GnuPG home directory to the
        ``/etc/salt/gpgkeys``.

    gnupghome
        Specify the location where GPG keyring and related files are stored.

    CLI Example:

    .. code-block:: bash

        salt '*' gpg.delete_key keyid=3FAD9F1E

        salt '*' gpg.delete_key fingerprint=53C96788253E58416D20BCD352952C84C3252192

        salt '*' gpg.delete_key keyid=3FAD9F1E user=username

        salt '*' gpg.delete_key keyid=3FAD9F1E user=username delete_secret=True

    """
    ret = {"res": True, "message": ""}

    if fingerprint and keyid:
        ret["res"] = False
        ret["message"] = "Only specify one argument, fingerprint or keyid"
        return ret

    if not fingerprint and not keyid:
        ret["res"] = False
        ret["message"] = "Required argument, fingerprint or keyid"
        return ret

    gpg = _create_gpg(user, gnupghome)
    key = get_key(keyid, fingerprint, user)
    if key:
        fingerprint = key["fingerprint"]
        skey = get_secret_key(keyid, fingerprint, user)
        if skey and not delete_secret:
            ret["res"] = False
            ret["message"] = (
                "Secret key exists, delete first or pass delete_secret=True."
            )
            return ret
        elif (
            skey
            and delete_secret
            and six.text_type(gpg.delete_keys(fingerprint, True)) == "ok"
        ):
            # Delete the secret key
            ret["message"] = "Secret key for {0} deleted\n".format(fingerprint)

        # Delete the public key
        if six.text_type(gpg.delete_keys(fingerprint)) == "ok":
            ret["message"] += "Public key for {0} deleted".format(fingerprint)
        ret["res"] = True
        return ret
    else:
        ret["res"] = False
        ret["message"] = "Key not available in keychain."
        return ret


# Traceback (ValueError)
[root@master1 salt]# salt-call gpg.encrypt text='Hello there.  How are you?'
[ERROR   ] An un-handled exception was caught by salt's global exception handler:
ValueError: No recipients specified with asymmetric encryption
Traceback (most recent call last):
File "/usr/bin/salt-call", line 11, in <module>
load_entry_point('salt==3000.1', 'console_scripts', 'salt-call')()
File "/usr/lib/python2.7/site-packages/salt/scripts.py", line 445, in salt_call
client.run()
File "/usr/lib/python2.7/site-packages/salt/cli/call.py", line 57, in run
caller.run()
File "/usr/lib/python2.7/site-packages/salt/cli/caller.py", line 119, in run
ret = self.call()
File "/usr/lib/python2.7/site-packages/salt/cli/caller.py", line 218, in call
ret['return'] = self.minion.executors[fname](self.opts, data, func, args, kwargs)
File "/usr/lib/python2.7/site-packages/salt/executors/direct_call.py", line 12, in execute
return func(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/salt/modules/gpg.py", line 1191, in encrypt
result = gpg.encrypt(text, recipients, passphrase=gpg_passphrase)
File "/usr/lib/python2.7/site-packages/gnupg.py", line 1506, in encrypt
result = self.encrypt_file(data, recipients, **kwargs)
File "/usr/lib/python2.7/site-packages/gnupg.py", line 1440, in encrypt_file
raise ValueError('No recipients specified with asymmetric '
ValueError: No recipients specified with asymmetric encryption
Traceback (most recent call last):
File "/usr/bin/salt-call", line 11, in <module>
load_entry_point('salt==3000.1', 'console_scripts', 'salt-call')()
File "/usr/lib/python2.7/site-packages/salt/scripts.py", line 445, in salt_call
client.run()
File "/usr/lib/python2.7/site-packages/salt/cli/call.py", line 57, in run
caller.run()
File "/usr/lib/python2.7/site-packages/salt/cli/caller.py", line 119, in run
ret = self.call()
File "/usr/lib/python2.7/site-packages/salt/cli/caller.py", line 218, in call
ret['return'] = self.minion.executors[fname](self.opts, data, func, args, kwargs)
File "/usr/lib/python2.7/site-packages/salt/executors/direct_call.py", line 12, in execute
return func(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/salt/modules/gpg.py", line 1191, in encrypt
result = gpg.encrypt(text, recipients, passphrase=gpg_passphrase)
File "/usr/lib/python2.7/site-packages/gnupg.py", line 1506, in encrypt
result = self.encrypt_file(data, recipients, **kwargs)
File "/usr/lib/python2.7/site-packages/gnupg.py", line 1440, in encrypt_file
raise ValueError('No recipients specified with asymmetric '
ValueError: No recipients specified with asymmetric encryption

# Fixed code
def delete_key(
    keyid=None, fingerprint=None, delete_secret=False, user=None, gnupghome=None
):
    """
    Get a key from the GPG keychain

    keyid
        The keyid of the key to be deleted.

    fingerprint
        The fingerprint of the key to be deleted.

    delete_secret
        Whether to delete a corresponding secret key prior to deleting the public key.
        Secret keys must be deleted before deleting any corresponding public keys.

    user
        Which user's keychain to access, defaults to user Salt is running as.
        Passing the user as ``salt`` will set the GnuPG home directory to the
        ``/etc/salt/gpgkeys``.

    gnupghome
        Specify the location where GPG keyring and related files are stored.

    CLI Example:

    .. code-block:: bash

        salt '*' gpg.delete_key keyid=3FAD9F1E

        salt '*' gpg.delete_key fingerprint=53C96788253E58416D20BCD352952C84C3252192

        salt '*' gpg.delete_key keyid=3FAD9F1E user=username

        salt '*' gpg.delete_key keyid=3FAD9F1E user=username delete_secret=True

    """
    ret = {"res": True, "message": ""}

    if fingerprint and keyid:
        ret["res"] = False
        ret["message"] = "Only specify one argument, fingerprint or keyid"
        return ret

    if not fingerprint and not keyid:
        ret["res"] = False
        ret["message"] = "Required argument, fingerprint or keyid"
        return ret

    gpg = _create_gpg(user, gnupghome)
    key = get_key(keyid, fingerprint, user)
    if key:
        fingerprint = key["fingerprint"]
        skey = get_secret_key(keyid, fingerprint, user)
        if skey and not delete_secret:
            ret["res"] = False
            ret["message"] = (
                "Secret key exists, delete first or pass delete_secret=True."
            )
            return ret
        elif skey and delete_secret and str(gpg.delete_keys(fingerprint, True)) == "ok":
            # Delete the secret key
            ret["message"] = "Secret key for {} deleted\n".format(fingerprint)

        # Delete the public key
        if str(gpg.delete_keys(fingerprint)) == "ok":
            ret["message"] += "Public key for {} deleted".format(fingerprint)
        ret["res"] = True
        return ret
    else:
        ret["res"] = False
        ret["message"] = "Key not available in keychain."
        return ret