How can I get the budget id for a Google Cloud project

67 views Asked by At

Could someone tell me how I get the budget id for a project inside a Cloud Function

I have the project id and want to delete the budget like so

    from google.cloud.billing import budgets_v1

    const BILLING_ACCOUNT_ID = XXXXXXX

    client = budgets_v1.BudgetServiceClient()
    budget_id = <How do I get my budget id  here >
    name = f"BillingAccounts/{BILLING_ACCOUNT_ID}/budgets/{budget_id}"
    request = budgets_v1.DeleteBudgetRequest(name)
    client.delete_budget(request=request)

Thanks for any help. I've read the Docs but I cannot understand how to do it.

I read the Docs but I cannot understand how to achieve this

1

There are 1 answers

0
zorroemoji On

Here is my solution for anyone interested . Thanks again to John Hanley for the useful commant

def create_billing_name(project_id: str):
    return <your standard billing name e.g. f"{project_id)-billing_name" >


def get_budget_name_by_billing_name(display_name: str):
    budget_name = ""
    try:
        parent_value = f"billingAccounts/{BILLING_ACCOUNT_ID}"
        request = budgets_v1.ListBudgetsRequest(parent=parent_value)
        page_result = budget_client.list_budgets(request=request)

        for budget in page_result:
            if display_name == budget.display_name:
                budget_name = budget.name
                break

        return budget_name

    except Exception as e:
        print(e)