简体   繁体   English

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

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

How can I retrieve usage and cost data for my IBM Cloud account using a REST API? 如何使用REST API检索我的IBM Cloud帐户的使用情况和成本数据? I found that there are billing related commands and I can export some data as JSON . 我发现有与计费相关的命令,可以将某些数据导出为JSON Is there an API or SDK I can use, ideally Python? 有没有我可以使用的API或SDK,最好是Python?

Here are some of the IBM Cloud billing commands I use: 这是我使用的一些IBM Cloud计费命令

ibmcloud billing resource-instances-usage --json

ibmcloud billing  account-usage --json

Are there equivalent APIs for that? 是否有等效的API?

I couldn't find a documented API but used the tracing to see how the above commands are executed. 我找不到文档化的API,但是使用了跟踪来查看上述命令的执行方式。 Using a valid access_token a program can call the metering host and obtain usage data for an account, resource group or all resource instances: 使用有效的access_token,程序可以调用计量主机并获取帐户,资源组或所有资源实例的使用情况数据:

A GET on the following URL with an account ID and month as YYYY-MM returns a JSON object with all resource usage and related cost: 以下URL上的GET(帐户ID和月份为YYYY-MM)将返回一个JSON对象,其中包含所有资源使用情况和相关费用:

https://metering-reporting.ng.bluemix.net/v4/accounts/account_id/resource_instances/usage/?_limit=100&_names=true

I coded up a small Python script that dumps that data or prints it as CSV . 我编写了一个小的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()

The GitHub repo openwhisk-cloud-usage-samples uses a serverless approach to getting data via APIs. GitHub存储库openwhisk-cloud-usage-samples使用无服务器方法通过API获取数据。 Examples are included in the repo. 回购中包含示例。 It's written in Javascript, but a package it uses openwhisk-jsonetl was designed so that you could declare the URLs and parameters in YAML (rather than writing code) to request and transform JSON. 它是用Javascript编写的,但是设计了一个使用openwhisk-jsonetl的包,以便您可以在YAML中声明URL和参数(而不是编写代码)以请求和转换JSON。

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

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