# Buggy code
def update_distribution(
    name,
    config,
    tags=None,
    region=None,
    key=None,
    keyid=None,
    profile=None,
):
    """
    Update the config (and optionally tags) for the CloudFront distribution with the given name.

    name
        Name of the CloudFront distribution

    config
        Configuration for the distribution

    tags
        Tags to associate with the distribution

    region
        Region to connect to

    key
        Secret key to use

    keyid
        Access key to use

    profile
        A dict with region, key, and keyid,
        or a pillar key (string) that contains such a dict.

    CLI Example:

    .. code-block:: bash

        salt myminion boto_cloudfront.update_distribution name=mydistribution profile=awsprofile \
            config='{"Comment":"partial configuration","Enabled":true}'
    """
    distribution_ret = get_distribution(
        name, region=region, key=key, keyid=keyid, profile=profile
    )
    if "error" in distribution_result:
        return distribution_result
    dist_with_tags = distribution_result["result"]

    current_distribution = dist_with_tags["distribution"]
    current_config = current_distribution["DistributionConfig"]
    current_tags = dist_with_tags["tags"]
    etag = dist_with_tags["etag"]

    config_diff = __utils__["dictdiffer.deep_diff"](current_config, config)
    if tags:
        tags_diff = __utils__["dictdiffer.deep_diff"](current_tags, tags)

    conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
    try:
        if "old" in config_diff or "new" in config_diff:
            conn.update_distribution(
                DistributionConfig=config,
                Id=current_distribution["Id"],
                IfMatch=etag,
            )
        if tags:
            arn = current_distribution["ARN"]
            if "new" in tags_diff:
                tags_to_add = {
                    "Items": [
                        {"Key": k, "Value": v}
                        for k, v in six.iteritems(tags_diff["new"])
                    ],
                }
                conn.tag_resource(
                    Resource=arn,
                    Tags=tags_to_add,
                )
            if "old" in tags_diff:
                tags_to_remove = {
                    "Items": list(tags_diff["old"].keys()),
                }
                conn.untag_resource(
                    Resource=arn,
                    TagKeys=tags_to_remove,
                )
    except botocore.exceptions.ClientError as err:
        return {"error": __utils__["boto3.get_error"](err)}
    finally:
        _cache_id(
            "cloudfront",
            sub_resource=name,
            invalidate=True,
            region=region,
            key=key,
            keyid=keyid,
            profile=profile,
        )

    return {"result": True}


# Traceback (NameError)
root@master-10-155:/var/cache/salt/minion/files/qa# salt '*master*' state.sls_id cf_testgeorgi.blabla.com wordpress.cloudfront
master-10-155.eu-west-1a.qa-public:
----------
ID: cf_testgeorgi.blabla.com
Function: boto_cloudfront.present
Name: testgeorgi.blabla.com_distribution
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 1905, in call
**cdata['kwargs'])
File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1830, in wrapper
return f(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/states/boto_cloudfront.py", line 216, in present
profile=profile,
File "/usr/lib/python2.7/dist-packages/salt/modules/boto_cloudfront.py", line 403, in update_distribution
if 'error' in distribution_result:
NameError: global name 'distribution_result' is not defined
Started: 11:04:14.782944
Duration: 46474.37 ms
Changes:

# Fixed code
def update_distribution(
    name,
    config,
    tags=None,
    region=None,
    key=None,
    keyid=None,
    profile=None,
):
    """
    Update the config (and optionally tags) for the CloudFront distribution with the given name.

    name
        Name of the CloudFront distribution

    config
        Configuration for the distribution

    tags
        Tags to associate with the distribution

    region
        Region to connect to

    key
        Secret key to use

    keyid
        Access key to use

    profile
        A dict with region, key, and keyid,
        or a pillar key (string) that contains such a dict.

    CLI Example:

    .. code-block:: bash

        salt myminion boto_cloudfront.update_distribution name=mydistribution profile=awsprofile \
            config='{"Comment":"partial configuration","Enabled":true}'
    """
    distribution_ret = get_distribution(
        name, region=region, key=key, keyid=keyid, profile=profile
    )
    if "error" in distribution_ret:
        return distribution_ret
    dist_with_tags = distribution_ret["result"]

    current_distribution = dist_with_tags["distribution"]
    current_config = current_distribution["DistributionConfig"]
    current_tags = dist_with_tags["tags"]
    etag = dist_with_tags["etag"]

    config_diff = __utils__["dictdiffer.deep_diff"](current_config, config)
    if tags:
        tags_diff = __utils__["dictdiffer.deep_diff"](current_tags, tags)

    conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
    try:
        if "old" in config_diff or "new" in config_diff:
            conn.update_distribution(
                DistributionConfig=config,
                Id=current_distribution["Id"],
                IfMatch=etag,
            )
        if tags:
            arn = current_distribution["ARN"]
            if "new" in tags_diff:
                tags_to_add = {
                    "Items": [
                        {"Key": k, "Value": v}
                        for k, v in six.iteritems(tags_diff["new"])
                    ],
                }
                conn.tag_resource(
                    Resource=arn,
                    Tags=tags_to_add,
                )
            if "old" in tags_diff:
                tags_to_remove = {
                    "Items": list(tags_diff["old"].keys()),
                }
                conn.untag_resource(
                    Resource=arn,
                    TagKeys=tags_to_remove,
                )
    except botocore.exceptions.ClientError as err:
        return {"error": __utils__["boto3.get_error"](err)}
    finally:
        _cache_id(
            "cloudfront",
            sub_resource=name,
            invalidate=True,
            region=region,
            key=key,
            keyid=keyid,
            profile=profile,
        )

    return {"result": True}