# Buggy code
def present(name, password, admin=False, **client_args):
    """
    Ensure that given user is present.

    name
        Name of the user to manage

    password
        Password of the user

    admin : False
        Whether the user should have cluster administration
        privileges or not.
    """
    ret = {
        "name": name,
        "changes": {},
        "result": True,
        "comment": "User {0} is present and up to date".format(name),
    }

    if not __salt__["influxdb.user_exists"](name, **client_args):
        if __opts__["test"]:
            ret["result"] = None
            ret["comment"] = "User {0} will be created".format(name)
            return ret
        else:
            if __salt__["influxdb.create_user"](
                name, password, admin=admin, **client_args
            ):
                ret["comment"] = "Created user {0}".format(name)
                ret["changes"][name] = "created"
                return ret
            else:
                ret["comment"] = "Failed to create user {0}".format(name)
                ret["result"] = False
                return ret
    else:
        changes = _changes(name, admin)
        if changes:
            if __opts__["test"]:
                ret["result"] = None
                ret["comment"] = (
                    "The following user attributes are set to be changed:\n"
                )
                for k, v in changes.items():
                    ret["comment"] += "{0}: {1}\n".format(k, v)
                return ret
            else:
                pre = __salt__["influxdb.user_info"](name)
                for k, v in changes.items():
                    if k == "admin":
                        if v:
                            __salt__["influxdb.grant_admin_privileges"](name)
                            continue
                        else:
                            __salt__["influxdb.revoke_admin_privileges"](name)
                            continue

                post = __salt__["influxdb.user_info"](name)
                for k in post:
                    if post[k] != pre[k]:
                        ret["changes"][k] = post[k]
                if ret["changes"]:
                    ret["comment"] = "Updated user {0}".format(name)
    return ret


# Traceback (ConnectionError)
----------
ID: sensu_influxdb
Function: influxdb_user.present
Name: sensu
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/state.py", line 1744, in call
**cdata['kwargs'])
File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1702, in wrapper
return f(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/states/influxdb_user.py", line 72, in present
changes = _changes(name, admin)
File "/usr/lib/python2.7/dist-packages/salt/states/influxdb_user.py", line 24, in _changes
existing_user = __salt__['influxdb.user_info'](name)
File "/usr/lib/python2.7/dist-packages/salt/modules/influx.py", line 198, in user_info
matching_users = (user for user in list_users(**client_args)
File "/usr/lib/python2.7/dist-packages/salt/modules/influx.py", line 163, in list_users
return client.get_list_users()
File "/usr/lib/python2.7/dist-packages/influxdb/client.py", line 633, in get_list_users
return list(self.query("SHOW USERS").get_points())
File "/usr/lib/python2.7/dist-packages/influxdb/client.py", line 325, in query
expected_response_code=expected_response_code
File "/usr/lib/python2.7/dist-packages/influxdb/client.py", line 241, in request
raise e
ConnectionError: HTTPConnectionPool(host='localhost', port=8086): Max retries exceeded with url: /query?q=SHOW+USERS (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7f45d18b8350>: Failed to establish a new connection: [Errno 111] Connection refused',))
Started: 14:10:02.779676
Duration: 9098882.04 ms
Changes:
----------

# Fixed code
def present(name, password, admin=False, **client_args):
    """
    Ensure that given user is present.

    name
        Name of the user to manage

    password
        Password of the user

    admin : False
        Whether the user should have cluster administration
        privileges or not.
    """
    ret = {
        "name": name,
        "changes": {},
        "result": True,
        "comment": "User {0} is present and up to date".format(name),
    }

    if not __salt__["influxdb.user_exists"](name, **client_args):
        if __opts__["test"]:
            ret["result"] = None
            ret["comment"] = "User {0} will be created".format(name)
            return ret
        else:
            if __salt__["influxdb.create_user"](
                name, password, admin=admin, **client_args
            ):
                ret["comment"] = "Created user {0}".format(name)
                ret["changes"][name] = "created"
                return ret
            else:
                ret["comment"] = "Failed to create user {0}".format(name)
                ret["result"] = False
                return ret
    else:
        changes = _changes(name, admin, **client_args)
        if changes:
            if __opts__["test"]:
                ret["result"] = None
                ret["comment"] = (
                    "The following user attributes are set to be changed:\n"
                )
                for k, v in changes.items():
                    ret["comment"] += "{0}: {1}\n".format(k, v)
                return ret
            else:
                pre = __salt__["influxdb.user_info"](name, **client_args)
                for k, v in changes.items():
                    if k == "admin":
                        if v:
                            __salt__["influxdb.grant_admin_privileges"](
                                name, **client_args
                            )
                            continue
                        else:
                            __salt__["influxdb.revoke_admin_privileges"](
                                name, **client_args
                            )
                            continue

                post = __salt__["influxdb.user_info"](name, **client_args)
                for k in post:
                    if post[k] != pre[k]:
                        ret["changes"][k] = post[k]
                if ret["changes"]:
                    ret["comment"] = "Updated user {0}".format(name)
    return ret