def group_exists()

in src/ansible_collections/alibaba/apsarastack/plugins/modules/ali_security_group.py [0:0]


def group_exists(conn, module, vpc_id, name, security_group_id, multi, recent):
    """Returns None or a security group object depending on the existence of a security group.
    When supplied with a vpc_id and Name, it will check them to determine if it is a match
    otherwise it will assume the Security Group does not exist and thus return None.
    """
    if multi:
        return None
    matching_groups = []
    filters = {}
    if vpc_id:
        filters['vpc_id'] = vpc_id
    if security_group_id:
        filters['security_group_id'] = security_group_id
    try:
        for g in conn.describe_security_groups(**filters):
            if name and g.security_group_name != name:
                continue
            matching_groups.append(g.get())
    except Exception as e:
        module.fail_json(msg="Failed to describe Security Groups: {0}".format(e))

    if len(matching_groups) == 1:
        return matching_groups[0]
    elif len(matching_groups) > 1:
        if recent:
            return matching_groups[-1]
        module.fail_json(msg='Currently there are {0} Security Groups that have the same name and '
                             'vpc id you specified. If you would like to create anyway '
                             'please pass True to the multi_ok param. Or, please pass True to the recent '
                             'param to choose the recent one.'.format(len(matching_groups)))
    return None