Google云计费API,获取当月费用

问题描述 投票:0回答:1

我目前正在使用烧瓶为在GKE上运行的在线服务构建管理仪表板。

我想显示自本月初以来该服务已累积了多大的账单。我已经研究过Google Cloud计费API,但这是一个血腥的丛林,我似乎找不到任何方法来检索所需的数据。

我认为应该可以,但我不知道如何做到。任何对Google Cloud API经验丰富的人都可以帮助我,最好是使用python代码段吗?

我看过this post中的答案,但无法真正弄清楚如何使用它们。

google-cloud-platform google-api google-kubernetes-engine
1个回答
0
投票

官方文档:

Get started with the Cloud Billing Budget API

Cloud Billing Budget API Setup

Using the Cloud Billing Budget API

Cloud Billing Budget Client Libraries

使用Cloud Billing Budget API来:

为每个Google Cloud项目创建单独的预算,这样您就可以了解您的Google Cloud环境中哪些方面的支出更多超出预期。

[在季度财务计划后批量更新所有预算。

与您公司的部署经理集成以添加创建您的云配置工作流程中的预算。

  1. 创建项目
  2. 启用计费
  3. 启用API(Cloud Budget API,Cloud Billing Budget API)
  4. 创建预算,在预算和警报上,创建预算,范围,金额(上个月的支出),操作
  5. [设置身份验证(创建服务帐户,向服务帐户授予帐单帐户管理员角色,下载key.json,设置环境变量export GOOGLE_APPLICATION_CREDENTIALS =“ [PATH]”
  6. 使用客户端库Client for Cloud Billing Budget API

    from google.cloud import billing_budgets_v1beta1
    client = billing_budgets_v1beta1.BudgetServiceClient()
    
    #get all the budgets
    parent = client.billing_account_path([BILLING_ACCOUNT])
    for element in client.list_budgets(parent):
       print(element)
       pass
    
    #for a specific budget
    name = client.budget_path('[BILLING_ACCOUNT]', '[BUDGET]')
    response = client.get_budget(name)
    
    #The output should be in the form 
    
    display_name: "billing-budget"
    budget_filter {
    projects: "projects/"
    credit_types_treatment: 
    }
    amount {
    last_period_amount {
    }
    }
    threshold_rules {
    threshold_percent: 0.5
    spend_basis: CURRENT_SPEND
    }
    threshold_rules {
    threshold_percent: 0.9
    spend_basis: CURRENT_SPEND
    }
    threshold_rules {
    threshold_percent: 1.0
    spend_basis: CURRENT_SPEND
    }
    all_updates_rule {
    }
    etag: "" ```
    
© www.soinside.com 2019 - 2024. All rights reserved.