2

I need to get a consolidated bill based on organizations or resource groups. Something like:

    org1   $100
    org2   $200
    rg1    $150

Is there a way I can get this in one api call? I can get a detailed bill using following cli command:

ibmcloud billing account-usage
NoviceMe
  • 3,126
  • 11
  • 57
  • 117
  • Possible duplicate of https://stackoverflow.com/questions/52333121/ibm-cloud-how-to-access-the-api-for-billing-and-usage – Vidyasagar Machupalli Nov 20 '18 at 15:23
  • @VidyasagarMachupalli - That is a very old question, things have changed a lot since then. – NoviceMe Nov 20 '18 at 15:39
  • I recommend this tutorial to get an overview: https://console.bluemix.net/docs/tutorials/cloud-usage.html It does not provide a single command. Are you looking for an API or a command? – data_henrik Nov 20 '18 at 15:53
  • @data_henrik - preferably an api call so I can send a consolidated bill as an email. – NoviceMe Nov 20 '18 at 15:56

3 Answers3

1

There is no single API call which gives usage per org. But there is API call ibmcloud billing org-usage $org which retrieves usage for a single ORG. You can write a small shell script to call the API in a loop over a list of org.

#!/bin/bash
ibmcloud login --apikey @ibmcloud_key
ibmcloud target  -o ORGNAME1 -s SPACENAME 
org_list=(ORGNAME1 ORGNAME2)
echo "Get usage for each org...." 
for org in "$org_list[@]"
do
    ibmcloud billing org-usage $org >> billing.txt
done
Manoj Singh
  • 1,627
  • 12
  • 21
1

The answers by @VidyasagarMachupalli and @data_henrik are appropriate. While there is no single API call, the ibmcloud billing resource-instances-usage provides the closest solution as it segments data based on org, space, and resource group.

You can use a spreadsheet (i.e. pivot table) to take the last step and sum the data for an entire org or resource group. The tutorial mentioned - this section - does exactly this approach using the IBM Cloud CLI, jq, and json2csv.

ibmcloud billing resource-instances-usage --output json | jq '.[] | {month,resource_name,resource_group_name,organization_name,space_name,metric: .usage[].metric,cost : .usage[].cost}' | json2csv -f month,resource_name,resource_group_name,organization_name,space_name,metric,cost -p

┌───────────────┬──────────────────────────────┬──────────────────────────────────────────┬──────────────────────────────────────┬────────────────────────┬────────────────┬───────────────┐
│ "month"       │ "resource_name"              │ "resource_group_name"                    │ "organization_name"                  │ "space_name"           │ "metric"       │ "cost"        │
├───────────────┼──────────────────────────────┼──────────────────────────────────────────┼──────────────────────────────────────┼────────────────────────┼────────────────┼───────────────┤
│               │                              │                                          │                                      │                        │ "AUTHORIZED_US │               │
│ "2018-11"     │ "Continuous Delivery"        │ "default"                                │ ""                                   │ ""                     │ ERS_PER_MONTH" │ 0             │
├───────────────┼──────────────────────────────┼──────────────────────────────────────────┼──────────────────────────────────────┼────────────────────────┼────────────────┼───────────────┤
│               │                              │                                          │                                      │                        │ "AUTHORIZED_US │               │
│ "2018-11"     │ "Continuous Delivery"        │ "default"                                │ ""                                   │ ""                     │ ERS_PER_MONTH" │ 0             │
├───────────────┼──────────────────────────────┼──────────────────────────────────────────┼──────────────────────────────────────┼────────────────────────┼────────────────┼───────────────┤
│               │                              │                                          │                                      │                        │ "JOB_EXECUTION │               │
│ "2018-11"     │ "Continuous Delivery"        │ "default"                                │ ""                                   │ ""                     │ S_PER_MONTH"   │ 0             │
├───────────────┼──────────────────────────────┼──────────────────────────────────────────┼──────────────────────────────────────┼────────────────────────┼────────────────┼───────────────┤
│               │                              │                                          │                                      │                        │ "JOB_EXECUTION │               │
│ "2018-11"     │ "Continuous Delivery"        │ "default"                                │ ""                                   │ ""                     │ S_PER_MONTH"   │ 0             │
├───────────────┼──────────────────────────────┼──────────────────────────────────────────┼──────────────────────────────────────┼────────────────────────┼────────────────┼───────────────┤
│               │                              │                                          │                                      │                        │ "DEVOPS_INSIGH │               │
│ "2018-11"     │ "DevOps Insights"            │ "default"                                │ ""                                   │ ""                     │ TS_ITEMS"      │ 0             │
├───────────────┼──────────────────────────────┼──────────────────────────────────────────┼──────────────────────────────────────┼────────────────────────┼────────────────┼───────────────┤
│               │                              │                                          │                                      │                        │ "instance_hour │               │
│ "2018-11"     │ "Kubernetes Service"         │ "default"                                │ ""                                   │ ""                     │ s"             │ 0             │
├───────────────┼──────────────────────────────┼──────────────────────────────────────────┼──────────────────────────────────────┼────────────────────────┼────────────────┼───────────────┤
│               │                              │                                          │                                      │                        │ "DATA_POINTS_P │               │
│ "2018-11"     │ "Monitoring"                 │ ""                                       │ "van_org"                            │ "dev"                  │ ER_MONTH"      │ 0             │
├───────────────┼──────────────────────────────┼──────────────────────────────────────────┼──────────────────────────────────────┼────────────────────────┼────────────────┼───────────────┤
│               │ "IBM Bluemix Platform Runtim │                                          │                                      │                        │ "GB_HOURS_PER_ │               │
│ "2018-11"     │ e"                           │ ""                                       │ "van_org"                            │ "dev"                  │ MONTH"         │ 0.00035       │
├───────────────┼──────────────────────────────┼──────────────────────────────────────────┼──────────────────────────────────────┼────────────────────────┼────────────────┼───────────────┤
│               │                              │                                          │                                      │                        │ "GB_HOURS_PER_ │ 3.51056366597 │
│ "2018-11"     │ "IBM Bluemix Node.js"        │ ""                                       │ "van_org"                            │ "dev"                  │ MONTH"         │ 2222          │
Van Staub
  • 81
  • 3
  • I tried this but this is not the solution I can use inside a function or lambda to run. Is there a better way of doing this? – NoviceMe Nov 20 '18 at 18:29
  • The CLI communicates with an API; you'd mimic the same approach in a Function. Set the IBMCLOUD_TRACE=true env var and run the command, you'll then see the RESTful call that the CLI is doing. The [Cloud Usage sample application](https://github.com/IBM-Cloud/openwhisk-cloud-usage-sample) referenced from the tutorial uses Cloud Functions to retrieve billing data. – Van Staub Nov 21 '18 at 13:32
0

IBM Cloud offers APIs for usage metering and usage reports. The integrated API overview page in the IBM Cloud docs lists them under Platform Services.

I also recommend this IBM Cloud solution tutorial on reviewing cloud usage for an introduction. I just checked and it does not yet have the new APIs, but it's worth reading nonetheless.

data_henrik
  • 16,724
  • 2
  • 28
  • 49