我目前正在使用 Flask 为在 GKE 上运行的在线服务构建管理仪表板。
我想展示自本月初以来该服务的账单有多大。我已经研究了 Google Cloud billing API,但它是一个血腥的丛林,我似乎找不到任何方法来检索我想要的数据。
我认为这应该是可能的,但我不知道如何。
任何对 Google Cloud API 更有经验的人都可以帮助我,最好是使用 python 代码片段吗?
我看过 this post 中的答案但无法真正弄清楚如何使用它们。
最佳答案
官方文档:
Get started with the Cloud Billing Budget API
Cloud Billing Budget API Setup
Using the Cloud Billing Budget API
Cloud Billing Budget Client Libraries
Use the Cloud Billing Budget API to:
Create a separate budget for each of your Google Cloud projects so you know which areas of your Google Cloud environment are spending more than expected.
Bulk update all of your budgets after quarterly financial planning.
Integrate with your company's deployment manager to add the creation of budgets to your cloud provisioning workflow.
上个月的支出),操作
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: "" ```
编辑:
我查了
gcloud alpha billing
命令,我只能看到选项: 1. accounts(describe, get-iam-policy, list, projects(describe, link, list, unlink))
2. budgets(create, delete, describe, list, update)
3. projects(describe, link, list, unlink). These are the API that you can call.
根据我的理解,没有用于获取当前支出的 API。您可以将帐单导出到 BigQuery 或文件(已弃用)并进行分析。
关于google-cloud-platform - 谷歌云计费API,抓取当月费用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60148004/