def find_stack_by_bot_id()

in packages/blueprints/gen-ai-chatbot/static-assets/chatbot-genai-components/backend/python/app/repositories/api_publication.py [0:0]


def find_stack_by_bot_id(bot_id: str) -> PublishedApiStackModel:
    client = boto3.client("cloudformation")
    # DO NOT change the stack naming rule
    stack_name = f"ApiPublishmentStack{bot_id}"

    try:
        response = client.describe_stacks(StackName=stack_name)
    except client.exceptions.ClientError as e:
        raise RecordNotFoundError()

    stack = response["Stacks"][0]

    if stack["StackStatus"] != "CREATE_COMPLETE":
        return PublishedApiStackModel(
            stack_id=stack["StackId"],
            stack_name=stack["StackName"],
            stack_status=stack["StackStatus"],
            api_id=None,
            api_name=None,
            api_usage_plan_id=None,
            api_allowed_origins=None,
            api_stage=None,
            create_time=int(stack["CreationTime"].timestamp() * 1000),
        )

    return PublishedApiStackModel(
        stack_id=stack["StackId"],
        stack_name=stack["StackName"],
        stack_status=stack["StackStatus"],
        api_id=[
            output["OutputValue"]
            for output in stack["Outputs"]
            if output["OutputKey"] == "ApiId"
        ][0],
        api_name=[
            output["OutputValue"]
            for output in stack["Outputs"]
            if output["OutputKey"] == "ApiName"
        ][0],
        api_usage_plan_id=[
            output["OutputValue"]
            for output in stack["Outputs"]
            if output["OutputKey"] == "ApiUsagePlanId"
        ][0],
        api_allowed_origins=[
            output["OutputValue"]
            for output in stack["Outputs"]
            if output["OutputKey"] == "AllowedOrigins"
        ][0].split(","),
        api_stage=[
            output["OutputValue"]
            for output in stack["Outputs"]
            if output["OutputKey"] == "DeploymentStage"
        ][0],
        create_time=int(stack["CreationTime"].timestamp() * 1000),
    )