I'm currently building an admin dashboard, using flask, for an online service that's running on GKE.
I want to show how large of a bill the service has racked up since the beginning of the month. I've looked into the Google Cloud billing API, but it's a bloody jungle and I can't seem to find any way to retrieve the data I want.
I assume it should be possible but I can't figure out how. Can anybody more experienced with Google Cloud APIs help me, preferably with python code-snippets?
I've looked at the answers in this post but couldn't really figure out how to use them.
The official documentation:
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.
Use the client library 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: "" ```
I checked the gcloud alpha billing
command and I can see only the options :
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.
There is no API for getting current spending to my understanding. You can export billing to BigQuery or to a file(this is deprecated) and do your analysis.