def _filter_ignored_elbs()

in lemur/plugins/lemur_aws/elb.py [0:0]


def _filter_ignored_elbs(elbs, key_field, arg_name, response_key_field, **kwargs):
    """
    Look up tags and remove any ELBs that should be ignored.
    :param elbs: List of dictionaries keyed by the field key_field
    :param key_field: Field value pass in call to describe_tags
    :param arg_name: Name of the argument to describe_tags
    :param response_key_field: Name of the field in response list with the object key.
    :param kwargs: must contain a 'client' from @sts_client
    :return:
    """
    if not elbs:
        return elbs
    ignore_tag = current_app.config.get("AWS_ELB_IGNORE_TAG")
    if not ignore_tag:
        return elbs
    try:
        keys = [elb[key_field] for elb in elbs]
        client = kwargs.pop("client")
        # {'TagDescriptions': [{'ResourceArn': 'string','Tags': [{'Key': 'string','Value': 'string'},]}]}
        tags_list = []
        # Restrict to 20 tags per call per elbv1 limits.
        while len(keys):
            next_keys = keys[:20]
            keys = keys[20:]
            tags_list += client.describe_tags(**{arg_name: next_keys})["TagDescriptions"]
        ignored_keys = {}
        for tags in tags_list:
            key = tags[response_key_field]
            tags = tags["Tags"]
            for tag in tags:
                if tag["Key"] == ignore_tag:
                    current_app.logger.info(f"Ignoring ELB due to ignore tag: {key}")
                    ignored_keys[key] = True

        return [elb for elb in elbs if not elb[key_field] in ignored_keys]

    except Exception as e:  # noqa
        metrics.send("describe_tags_error", "counter", 1)
        capture_exception()
        raise