繁体   English   中英

IBM Cloud:如何访问API进行计费和使用?

[英]IBM Cloud: How to access the API for billing and usage?

如何使用REST API检索我的IBM Cloud帐户的使用情况和成本数据? 我发现有与计费相关的命令,可以将某些数据导出为JSON 有没有我可以使用的API或SDK,最好是Python?

这是我使用的一些IBM Cloud计费命令

ibmcloud billing resource-instances-usage --json

ibmcloud billing  account-usage --json

是否有等效的API?

我找不到文档化的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脚本,该脚本转储该数据或将其打印为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存储库openwhisk-cloud-usage-samples使用无服务器方法通过API获取数据。 回购中包含示例。 它是用Javascript编写的,但是设计了一个使用openwhisk-jsonetl的包,以便您可以在YAML中声明URL和参数(而不是编写代码)以请求和转换JSON。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM