# Buggy code
def present(name, database, duration="7d", replication=1, default=False, **client_args):
    """
    Ensure that given retention policy is present.

    name
        Name of the retention policy to create.

    database
        Database to create retention policy on.
    """
    ret = {
        "name": name,
        "changes": {},
        "result": True,
        "comment": "retention policy {0} is already present".format(name),
    }

    if not __salt__["influxdb.retention_policy_exists"](
        name=name, database=database, **client_args
    ):
        if __opts__["test"]:
            ret["result"] = None
            ret["comment"] = " {0} is absent and will be created".format(name)
            return ret
        if __salt__["influxdb.create_retention_policy"](
            database, name, duration, replication, default, **client_args
        ):
            ret["comment"] = "retention policy {0} has been created".format(name)
            ret["changes"][name] = "Present"
            return ret
        else:
            ret["comment"] = "Failed to create retention policy {0}".format(name)
            ret["result"] = False
            return ret

    else:
        current_policy = __salt__["influxdb.get_retention_policy"](
            database=database, name=name
        )
        update_policy = False
        if current_policy["duration"] != convert_duration(duration):
            update_policy = True
            ret["changes"]["duration"] = "Retention changed from {0} to {1}.".format(
                current_policy["duration"], duration
            )

        if current_policy["replicaN"] != replication:
            update_policy = True
            ret["changes"]["replication"] = (
                "Replication changed from {0} to {1}.".format(
                    current_policy["replicaN"], replication
                )
            )

        if current_policy["default"] != default:
            update_policy = True
            ret["changes"]["default"] = "Default changed from {0} to {1}.".format(
                current_policy["default"], default
            )

        if update_policy:
            if __opts__["test"]:
                ret["result"] = None
                ret["comment"] = " {0} is present and set to be changed".format(name)
                return ret
            else:
                if __salt__["influxdb.alter_retention_policy"](
                    database, name, duration, replication, default, **client_args
                ):
                    ret["comment"] = "retention policy {0} has been changed".format(
                        name
                    )
                    return ret
                else:
                    ret["comment"] = "Failed to update retention policy {0}".format(
                        name
                    )
                    ret["result"] = False
                    return ret

    return ret


# Traceback (urllib3.exceptions.ProtocolError)
Function: influxdb_retention_policy.present
Name: rp_telegraf_1year
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 601, in urlopen
chunked=chunked)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 387, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 383, in _make_request
httplib_response = conn.getresponse()
File "/usr/lib/python3.6/http/client.py", line 1331, in getresponse
response.begin()
File "/usr/lib/python3.6/http/client.py", line 297, in begin
version, status, reason = self._read_status()
File "/usr/lib/python3.6/http/client.py", line 279, in _read_status
raise BadStatusLine(line)
http.client.BadStatusLine: �����


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 440, in send
timeout=timeout
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 639, in urlopen
_stacktrace=sys.exc_info()[2])
File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 357, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/usr/lib/python3/dist-packages/six.py", line 692, in reraise
raise value.with_traceback(tb)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 601, in urlopen
chunked=chunked)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 387, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 383, in _make_request
httplib_response = conn.getresponse()
File "/usr/lib/python3.6/http/client.py", line 1331, in getresponse
response.begin()
File "/usr/lib/python3.6/http/client.py", line 297, in begin
version, status, reason = self._read_status()
File "/usr/lib/python3.6/http/client.py", line 279, in _read_status
raise BadStatusLine(line)
urllib3.exceptions.ProtocolError: ('Connection aborted.', BadStatusLine('\x15\x03\x01\x00\x02\x02\n',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/salt/state.py", line 1933, in call
**cdata['kwargs'])
File "/usr/lib/python3/dist-packages/salt/loader.py", line 1939, in wrapper
return f(*args, **kwargs)
File "/usr/lib/python3/dist-packages/salt/states/influxdb_retention_policy.py", line 92, in present
current_policy = __salt__['influxdb.get_retention_policy'](database=database, name=name)
File "/usr/lib/python3/dist-packages/salt/modules/influx.py", line 354, in get_retention_policy
return next((p for p in client.get_list_retention_policies(database)
File "/usr/local/lib/python3.6/dist-packages/influxdb/client.py", line 749, in get_list_retention_policies
quote_ident(database or self._database))
File "/usr/local/lib/python3.6/dist-packages/influxdb/client.py", line 416, in query
expected_response_code=expected_response_code
File "/usr/local/lib/python3.6/dist-packages/influxdb/client.py", line 267, in request
timeout=self._timeout
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 520, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 630, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 490, in send
raise ConnectionError(err, request=request)
pip._vendor.requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine('\x15\x03\x01\x00\x02\x02\n',))
Started: 20:36:05.863657
Duration: 44.999 ms
Changes:

# Fixed code
def present(name, database, duration="7d", replication=1, default=False, **client_args):
    """
    Ensure that given retention policy is present.

    name
        Name of the retention policy to create.

    database
        Database to create retention policy on.
    """
    ret = {
        "name": name,
        "changes": {},
        "result": True,
        "comment": "retention policy {0} is already present".format(name),
    }

    if not __salt__["influxdb.retention_policy_exists"](
        name=name, database=database, **client_args
    ):
        if __opts__["test"]:
            ret["result"] = None
            ret["comment"] = " {0} is absent and will be created".format(name)
            return ret
        if __salt__["influxdb.create_retention_policy"](
            database, name, duration, replication, default, **client_args
        ):
            ret["comment"] = "retention policy {0} has been created".format(name)
            ret["changes"][name] = "Present"
            return ret
        else:
            ret["comment"] = "Failed to create retention policy {0}".format(name)
            ret["result"] = False
            return ret

    else:
        current_policy = __salt__["influxdb.get_retention_policy"](
            database=database, name=name, **client_args
        )
        update_policy = False
        if current_policy["duration"] != convert_duration(duration):
            update_policy = True
            ret["changes"]["duration"] = "Retention changed from {0} to {1}.".format(
                current_policy["duration"], duration
            )

        if current_policy["replicaN"] != replication:
            update_policy = True
            ret["changes"]["replication"] = (
                "Replication changed from {0} to {1}.".format(
                    current_policy["replicaN"], replication
                )
            )

        if current_policy["default"] != default:
            update_policy = True
            ret["changes"]["default"] = "Default changed from {0} to {1}.".format(
                current_policy["default"], default
            )

        if update_policy:
            if __opts__["test"]:
                ret["result"] = None
                ret["comment"] = " {0} is present and set to be changed".format(name)
                return ret
            else:
                if __salt__["influxdb.alter_retention_policy"](
                    database, name, duration, replication, default, **client_args
                ):
                    ret["comment"] = "retention policy {0} has been changed".format(
                        name
                    )
                    return ret
                else:
                    ret["comment"] = "Failed to update retention policy {0}".format(
                        name
                    )
                    ret["result"] = False
                    return ret

    return ret