# Buggy code
def present(
    name, passwd, database="admin", user=None, password=None, host=None, port=None
):
    """
    Ensure that the user is present with the specified properties

    name
        The name of the user to manage

    passwd
        The password of the user

    user
        The user to connect as (must be able to create the user)

    password
        The password of the user

    host
        The host to connect to

    port
        The port to connect to

    database
        The database to create the user in (if the db doesn't exist, it will be created)

    """
    ret = {
        "name": name,
        "changes": {},
        "result": True,
        "comment": "User {0} is already present".format(name),
    }
    # check if user exists
    if __salt__["mongodb.user_exists"](name, user, password, host, port, database):
        return ret

    if __opts__["test"]:
        ret["result"] = None
        ret["comment"] = ("User {0} is not present and needs to be created").format(
            name
        )
        return ret
    # The user is not present, make it!
    if __salt__["mongodb.user_create"](
        name, passwd, user, password, host, port, database=database
    ):
        ret["comment"] = "User {0} has been created".format(name)
        ret["changes"][name] = "Present"
    else:
        ret["comment"] = "Failed to create database {0}".format(name)
        ret["result"] = False

    return ret


# Traceback (KeyError)
graylog:
----------
State: - mongodb_user
Name:      graylog2
Function:  present
Result:    False
Comment:   An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/pymodules/python2.7/salt/state.py", line 1317, in call
ret = self.states[cdata['full']](*cdata['args'])
File "/usr/lib/pymodules/python2.7/salt/states/mongodb_user.py", line 45, in present
if __salt__['mongodb.user_exists'](name, user, password, host, port, database):
KeyError: 'mongodb.user_exists'

# Fixed code
def present(
    name, passwd, database="admin", user=None, password=None, host=None, port=None
):
    """
    Ensure that the user is present with the specified properties

    name
        The name of the user to manage

    passwd
        The password of the user

    user
        The user to connect as (must be able to create the user)

    password
        The password of the user

    host
        The host to connect to

    port
        The port to connect to

    database
        The database to create the user in (if the db doesn't exist, it will be created)

    """
    ret = {
        "name": name,
        "changes": {},
        "result": True,
        "comment": "User {0} is already present".format(name),
    }

    # Check for valid port
    try:
        port = int(port)
    except TypeError:
        ret["result"] = False
        ret["comment"] = "Port ({0}) is not an integer.".format(port)
        return ret

    # check if user exists
    if __salt__["mongodb.user_exists"](name, user, password, host, port, database):
        return ret

    if __opts__["test"]:
        ret["result"] = None
        ret["comment"] = ("User {0} is not present and needs to be created").format(
            name
        )
        return ret
    # The user is not present, make it!
    if __salt__["mongodb.user_create"](
        name, passwd, user, password, host, port, database=database
    ):
        ret["comment"] = "User {0} has been created".format(name)
        ret["changes"][name] = "Present"
    else:
        ret["comment"] = "Failed to create database {0}".format(name)
        ret["result"] = False

    return ret