def vpc_exists()

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


def vpc_exists(module, vpc, vpc_id, name, cidr_block, multi, recent):
    """Returns None or a vpc object depending on the existence of a VPC. When supplied
    with a CIDR and Name, it will check them to determine if it is a match
    otherwise it will assume the VPC does not exist and thus return None.
    """
    if multi:
        return None
    matching_vpcs = []
    try:
        for v in vpc.describe_vpcs():
            if cidr_block and v.cidr_block != cidr_block:
                continue
            if name and v.vpc_name != name:
                continue
            if vpc_id and v.vpc_id != vpc_id:
                continue
            matching_vpcs.append(v)

    except Exception as e:
        module.fail_json(msg="Failed to describe VPCs: {0}".format(e))

    if len(matching_vpcs) == 1:
        return matching_vpcs[0]
    elif len(matching_vpcs) > 1:
        if recent:
            return matching_vpcs[-1]
        module.fail_json(msg='Currently there are {0} VPCs that have the same name and '
                             'CIDR block you specified. If you would like to create '
                             'the VPC 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_vpcs)))
    return None