如何使用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,但使用跟踪来查看上述命令的执行方式。使用有效的access_token,程序可以调用计量主机并获取帐户,资源组或所有资源实例的使用情况数据:
在具有帐户ID和月份的以下URL上获取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()
GitHub repo openwhisk-cloud-usage-samples使用无服务器方法通过API获取数据。例子包括在回购中。它是用Javascript编写的,但是它使用openwhisk-jsonetl的包被设计为可以在YAML中声明URL和参数(而不是编写代码)来请求和转换JSON。