# Buggy code
def bindings_exist(name, jboss_config, bindings, profile=None):
    """
    Ensures that given JNDI binding are present on the server.
    If a binding doesn't exist on the server it will be created.
    If it already exists its value will be changed.

    jboss_config:
        Dict with connection properties (see state description)
    bindings:
        Dict with bindings to set.
    profile:
        The profile name (domain mode only)

    Example:

    .. code-block:: yaml

            jndi_entries_created:
              jboss7.bindings_exist:
               - bindings:
                  'java:global/sampleapp/environment': 'DEV'
                  'java:global/sampleapp/configurationFile': '/var/opt/sampleapp/config.properties'
               - jboss_config: {{ pillar['jboss'] }}

    """
    log.debug(
        " ======================== STATE: jboss7.bindings_exist (name: %s) (profile: %s) ",
        name,
        profile,
    )
    log.debug("bindings=%s", bindings)
    ret = {
        "name": name,
        "result": True,
        "changes": {},
        "comment": "Bindings not changed.",
    }

    has_changed = False
    for key in bindings:
        value = six.text_type(bindings[key])
        query_result = __salt__["jboss7.read_simple_binding"](
            binding_name=key, jboss_config=jboss_config, profile=profile
        )
        if query_result["success"]:
            current_value = query_result["result"]["value"]
            if current_value != value:
                update_result = __salt__["jboss7.update_simple_binding"](
                    binding_name=key,
                    value=value,
                    jboss_config=jboss_config,
                    profile=profile,
                )
                if update_result["success"]:
                    has_changed = True
                    __log_binding_change(
                        ret["changes"], "changed", key, value, current_value
                    )
                else:
                    raise CommandExecutionError(update_result["failure-description"])
        else:
            if query_result["err_code"] == "JBAS014807":  # ok, resource not exists:
                create_result = __salt__["jboss7.create_simple_binding"](
                    binding_name=key,
                    value=value,
                    jboss_config=jboss_config,
                    profile=profile,
                )
                if create_result["success"]:
                    has_changed = True
                    __log_binding_change(ret["changes"], "added", key, value)
                else:
                    raise CommandExecutionError(create_result["failure-description"])
            else:
                raise CommandExecutionError(query_result["failure-description"])

    if has_changed:
        ret["comment"] = "Bindings changed."
    return ret


# Traceback (AttributeError)
ID: CMDataSource
Function: jboss7.datasource_exists
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/salt/state.py", line 1561, in call
**cdata['kwargs'])
File "/usr/lib/python2.7/site-packages/salt/states/jboss7.py", line 129, in datasource_exists
ds_result = __salt__['jboss7.read_datasource'](jboss_config=jboss_config, name=name)
File "/usr/lib/python2.7/site-packages/salt/modules/jboss7.py", line 243, in read_datasource
return __read_datasource(jboss_config, name)
File "/usr/lib/python2.7/site-packages/salt/modules/jboss7.py", line 332, in __read_datasource
operation_result = __salt__['jboss7_cli.run_operation'](jboss_config, operation)
File "/usr/lib/python2.7/site-packages/salt/modules/jboss7_cli.py", line 114, in run_operation
cli_result['err_code'] = match.group(1)
AttributeError: 'NoneType' object has no attribute 'group'
Started: 12:27:24.778509
Duration: 1724.338 ms
Changes:

# Fixed code
def bindings_exist(name, jboss_config, bindings, profile=None):
    """
    Ensures that given JNDI binding are present on the server.
    If a binding doesn't exist on the server it will be created.
    If it already exists its value will be changed.

    jboss_config:
        Dict with connection properties (see state description)
    bindings:
        Dict with bindings to set.
    profile:
        The profile name (domain mode only)

    Example:

    .. code-block:: yaml

            jndi_entries_created:
              jboss7.bindings_exist:
               - bindings:
                  'java:global/sampleapp/environment': 'DEV'
                  'java:global/sampleapp/configurationFile': '/var/opt/sampleapp/config.properties'
               - jboss_config: {{ pillar['jboss'] }}

    """
    log.debug(
        " ======================== STATE: jboss7.bindings_exist (name: %s) (profile: %s) ",
        name,
        profile,
    )
    log.debug("bindings=%s", bindings)
    ret = {
        "name": name,
        "result": True,
        "changes": {},
        "comment": "Bindings not changed.",
    }

    has_changed = False
    for key in bindings:
        value = six.text_type(bindings[key])
        query_result = __salt__["jboss7.read_simple_binding"](
            binding_name=key, jboss_config=jboss_config, profile=profile
        )
        if query_result["success"]:
            current_value = query_result["result"]["value"]
            if current_value != value:
                update_result = __salt__["jboss7.update_simple_binding"](
                    binding_name=key,
                    value=value,
                    jboss_config=jboss_config,
                    profile=profile,
                )
                if update_result["success"]:
                    has_changed = True
                    __log_binding_change(
                        ret["changes"], "changed", key, value, current_value
                    )
                else:
                    raise CommandExecutionError(update_result["failure-description"])
        else:
            if query_result["err_code"] in (
                "JBAS014807",
                "WFLYCTL0216",
            ):  # ok, resource not exists:
                create_result = __salt__["jboss7.create_simple_binding"](
                    binding_name=key,
                    value=value,
                    jboss_config=jboss_config,
                    profile=profile,
                )
                if create_result["success"]:
                    has_changed = True
                    __log_binding_change(ret["changes"], "added", key, value)
                else:
                    raise CommandExecutionError(create_result["failure-description"])
            else:
                raise CommandExecutionError(query_result["failure-description"])

    if has_changed:
        ret["comment"] = "Bindings changed."
    return ret