# Buggy code
def import_key(text=None, filename=None, user=None, gnupghome=None):
    r"""
    Import a key from text or file

    text
        The text containing to import.

    filename
        The filename containing the key to import.

    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.import_key text='-----BEGIN PGP PUBLIC KEY BLOCK-----\n ... -----END PGP PUBLIC KEY BLOCK-----'
        salt '*' gpg.import_key filename='/path/to/public-key-file'

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

    gpg = _create_gpg(user, gnupghome)

    if not text and not filename:
        raise SaltInvocationError("filename or text must be passed.")

    if filename:
        try:
            with salt.utils.files.flopen(filename, "rb") as _fp:
                text = salt.utils.stringutils.to_unicode(_fp.read())
        except IOError:
            raise SaltInvocationError("filename does not exist.")

    imported_data = gpg.import_keys(text)

    if GPG_1_3_1:
        counts = imported_data.counts
        if counts.get("imported") or counts.get("imported_rsa"):
            ret["message"] = "Successfully imported key(s)."
        elif counts.get("unchanged"):
            ret["message"] = "Key(s) already exist in keychain."
        elif counts.get("not_imported"):
            ret["res"] = False
            ret["message"] = "Unable to import key."
        elif not counts.get("count"):
            ret["res"] = False
            ret["message"] = "Unable to import key."
    else:
        if imported_data.imported or imported_data.imported_rsa:
            ret["message"] = "Successfully imported key(s)."
        elif imported_data.unchanged:
            ret["message"] = "Key(s) already exist in keychain."
        elif imported_data.not_imported:
            ret["res"] = False
            ret["message"] = "Unable to import key."
        elif not imported_data.count:
            ret["res"] = False
            ret["message"] = "Unable to import key."
    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 import_key(text=None, filename=None, user=None, gnupghome=None):
    r"""
    Import a key from text or file

    text
        The text containing to import.

    filename
        The filename containing the key to import.

    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.import_key text='-----BEGIN PGP PUBLIC KEY BLOCK-----\n ... -----END PGP PUBLIC KEY BLOCK-----'
        salt '*' gpg.import_key filename='/path/to/public-key-file'

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

    gpg = _create_gpg(user, gnupghome)

    if not text and not filename:
        raise SaltInvocationError("filename or text must be passed.")

    if filename:
        try:
            with salt.utils.files.flopen(filename, "rb") as _fp:
                text = salt.utils.stringutils.to_unicode(_fp.read())
        except OSError:
            raise SaltInvocationError("filename does not exist.")

    imported_data = gpg.import_keys(text)

    if GPG_1_3_1:
        counts = imported_data.counts
        if counts.get("imported") or counts.get("imported_rsa"):
            ret["message"] = "Successfully imported key(s)."
        elif counts.get("unchanged"):
            ret["message"] = "Key(s) already exist in keychain."
        elif counts.get("not_imported"):
            ret["res"] = False
            ret["message"] = "Unable to import key."
        elif not counts.get("count"):
            ret["res"] = False
            ret["message"] = "Unable to import key."
    else:
        if imported_data.imported or imported_data.imported_rsa:
            ret["message"] = "Successfully imported key(s)."
        elif imported_data.unchanged:
            ret["message"] = "Key(s) already exist in keychain."
        elif imported_data.not_imported:
            ret["res"] = False
            ret["message"] = "Unable to import key."
        elif not imported_data.count:
            ret["res"] = False
            ret["message"] = "Unable to import key."
    return ret