如何使用 REST API 检索我的 IBM Cloud 帐户的使用情况和成本数据?我发现有billing related commands and I can export some data as JSON 。是否有我可以使用的 API 或 SDK,最好是 Python?
以下是一些 IBM Cloud billing commands我使用:
ibmcloud billing resource-instances-usage --json
ibmcloud billing account-usage --json
是否有等效的 API?
最佳答案
更新: API 现在记录在此处:https://cloud.ibm.com/apidocs/metering-reporting
旧版:
我找不到记录的 API,但使用跟踪来查看上述命令是如何执行的。使用有效的access_token,程序可以调用计量主机并获取帐户、资源组或所有资源实例的使用数据:
对以下 URL 执行 GET 操作,其中包含帐户 ID 和 YYYY-MM 月份,会返回一个 JSON 对象,其中包含所有资源使用情况和相关成本:
https://metering-reporting.ng.bluemix.net/v4/accounts/account_id/resource_instances/usage/?_limit=100&_names=true
我编码了一个小Python script that dumps that data or prints it as CSV .
def processResourceInstanceUsage(account_id, billMonth):
METERING_HOST="https://metering-reporting.ng.bluemix.net"
USAGE_URL="/v4/accounts/"+account_id+"/resource_instances/usage/"+billMonth+"?_limit=100&_names=true"
url=METERING_HOST+USAGE_URL
headers = {
"Authorization": "{}".format(iam_token),
"Accept": "application/json",
"Content-Type": "application/json"
}
response=requests.get(url, headers=headers)
print ("\n\nResource instance usage for first 100 items")
return response.json()
关于python - IBM云: How to access the API for billing and usage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52333121/