def wait_all_stacks()

in lambdas/custom_resources/CTE_CrossAccountCloudFormation/src/cfn_helper.py [0:0]


def wait_all_stacks(stack_list):
    """Loops through all stacks in list checking status, raises Exception if any failed

    Args:
        stack_list (list of dict): List of dicts with 'Name' and 'AccountNumber' per stack to wait on

    Returns:
        None
    """
    successful_cfn_list = [
        'CREATE_COMPLETE',
        'UPDATE_COMPLETE'
    ]
    failure_cfn_list = [
        'CREATE_FAILED',
        'DELETE_FAILED',
        'ROLLBACK_COMPLETE',
        'UPDATE_ROLLBACK_COMPLETE',
        'UPDATE_ROLLBACK_FAILED',
        'ROLLBACK_FAILED'
    ]
    duration = 30
    timeout = 1
    failed_list = list()

    # Stack_list [{"StackName": name, "AssumedCredentials": creds}]
    while stack_list and duration > timeout:
        for stack in stack_list:
            remove_stack_list = False
            stack_status = get_stack_status(stack_name=stack['Name'], session=stack['Session'])
            logger.info(f"Stack ({stack['Name']}) Status:{stack_status}")
            if stack_status in successful_cfn_list:
                logger.info(f"{stack['Name']} was successful, removing from Status list")
                remove_stack_list = True

            elif stack_status in failure_cfn_list:
                logger.info(f"{stack['Name']} was failed, removing from Status list and getting more information")
                failure = determine_stack_failure_event(stack_name=stack['Name'], session=stack['Session'])
                failed_list.append({"Name": stack['Name'], "Failure": failure})
                remove_stack_list = True

            if remove_stack_list:
                try:
                    stack_list.remove(stack)
                except ValueError:
                    logger.warning(f"Stack {stack} not in list:{stack_list}")

        timeout += 1
        time.sleep(10)

    if failed_list:
        raise Exception(failed_list)