Search This Blog

Wednesday, 10 February 2016

Invoking the Billing API for Bluemix Public Organizations

The ability to view usage data from a billing perspective on IBM Bluemix Public is available as a REST based API. To use it follow the steps below.

In order to use the API you have to have the Billing Manager Role or be the Account Owner of the organization as shown below.



Steps

1. Log into the PUBLIC Bluemix region as shown below

pasapicella@Pas-MacBook-Pro:~$ cf login -u pasapi@au1.ibm.com -p ***** -o pasapi@au1.ibm.com -s dev
API endpoint: https://api.ng.bluemix.net
Authenticating...
OK

Targeted org pasapi@au1.ibm.com

Targeted space dev

API endpoint:   https://api.ng.bluemix.net (API version: 2.40.0)
User:           pasapi@au1.ibm.com
Org:            pasapi@au1.ibm.com
Space:          dev


2. List all your Organizations as shown below

pasapicella@Pas-MacBook-Pro:~$ cf orgs
Getting orgs as pasapi@au1.ibm.com...

name
iwinoto@au1.ibm.com
vralh@au1.ibm.com
MobileQualityAssurance
arthur.proestakis@au1.ibm.com
abentley@au1.ibm.com
shawmale@au1.ibm.com
ANZ-Innovation-Lab
pasapi@au1.ibm.com
NAB Experimentation
Telstra-CustomerA


3. Determine the GUID of the Org you wnat to get metering usage from

pasapicella@Pas-MacBook-Pro:~$ cf org pasapi@au1.ibm.com --guid
e270a605-978e-45fc-9507-00a50dec2469


4. Determine the region name for the PUBLIC instance your connected to as follows

pasapicella@Pas-MacBook-Pro:~$ curl http://mccp.ng.bluemix.net/info
{
  "name": "Bluemix",
  "build": "221004",
  "support": "http://ibm.com",
  "version": 2,
  "description": "IBM Bluemix",
  "authorization_endpoint": "https://mccp.ng.bluemix.net/login",
  "token_endpoint": "https://mccp.ng.bluemix.net/uaa",
  "min_cli_version": null,
  "min_recommended_cli_version": null,
  "api_version": "2.40.0",
  "app_ssh_endpoint": "ssh.ng.bluemix.net:2222",
  "app_ssh_host_key_fingerprint": null,
  "app_ssh_oauth_client": "ssh-proxy",
  "routing_endpoint": "https://api.ng.bluemix.net/routing",
  "logging_endpoint": "wss://loggregator.ng.bluemix.net:443",
  "doppler_logging_endpoint": "wss://doppler.ng.bluemix.net:4443",
  "console_endpoint": "https://mccp.ng.bluemix.net/console",
  "region": "us-south"
}


5. Determine the OAUTH token for your connected session as follows

pasapicella@Pas-MacBook-Pro:~$ cf oauth-token
Getting OAuth token...
OK

bearer eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI0ODAzOWM1My0yODZhLTQ5Y2YtYWIzYi0yNGVhZTY

4ZmFmYzIiLCJzdWIiOiJiNmMwMjBiNC1lMTFhLTQ2MzAtYTZhMi0zZjIwZmNlYzdmOTAiL
CJzY29wZSI6WyJjbG91ZF9jb250cm9sbGVyLnJlYWQiLCJwYXNzd29yZC53cml0ZSIsImNsb3
VkX2NvbnRyb2xsZXIud3JpdGUiLCJvcGVuaWQiXSwiY2xpZW50X2lkIjoiY2YiLCJjaWQiOiJj
ZiIsImF6cCI6ImNmIiwiZ3JhbnRfdHlwZSI6InBhc3N3b3JkIiwidXNlcl9pZCI6ImI2YzAyMGI0L
WUxMWEtNDYzMC1hNmEyLTNmMjBmY2VjN2Y5MCIsIm9yaWdpbiI6InVhYSIsInVzZXJf
bmFtZSI6InBhc2FwaUBhdTEuaWJtLmNvbSIsImVtYWlsIjoicGFzYXBpQGF1MS5pYm0uY29t
IiwicmV2X3NpZyI6IjVjOGMyODQ4IiwiaWF0IjoxNDU1MDU3NzQxLCJleHAiOjE0NTUxMD
A5NDEsImlzcyI6Imh0dHBzOi8vdWFhLm5nLmJsdWVtaXgubmV0L29hdXRoL3Rva2VuIiwiem
lkIjoidWFhIiwiYXVkIjpbImNsb3VkX2NvbnRyb2xsZXIiLCJwYXNzd29yZCIsImNmIiwib3Blbm
lkIl19.EUEIXZ-XgxQbvTQnSgrToODHbNmKvhx0PtAp9CaiPTk

At this point we are ready to invoke the Billing/Metering API. The format is as follows

Bluemix Endpoint:

https://rated-usage.ng.bluemix.net/v2/metering/organizations/us-south:ORG_ID/YYYY-MM

  1. ORG_ID : Account GUID
  2. YYYY-MM: Year and month for which usage is required
Format as follows:

curl -v -X GET -H "Authorization: bearer {oauth-token}" "https://rated-usage.ng.bluemix.net/v2/metering/organizations/us-south:e270a605-978e-45fc-9507-00a50dec2469/usage/2016-02" | python -m json.tool
 
6. To invoke using curl we should do it as follows

Output:

pasapicella@Pas-MacBook-Pro:~$ curl -v -X GET -H "Authorization: bearer {oauth-token}" "https://rated-usage.ng.bluemix.net/v2/metering/organizations/us-south:e270a605-978e-45fc-9507-00a50dec2469/usage/2016-02" | python -m json.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 75.126.70.44...
* Connected to rated-usage.ng.bluemix.net (75.126.70.44) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
* Server certificate: *.ng.bluemix.net
* Server certificate: DigiCert SHA2 Secure Server CA
* Server certificate: DigiCert Global Root CA
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0> GET /v2/metering/organizations/us-south:e270a605-978e-45fc-9507-00a50dec2469/usage/2016-02 HTTP/1.1
> Host: rated-usage.ng.bluemix.net
> User-Agent: curl/7.43.0
> Accept: */*
> Authorization: bearer eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI0ODAzOWM1My0yODZhLTQ5Y2YtYWIzYi0yNGVhZTY4ZmFmYzIiLCJzdWIiOiJiNmMwMjBiNC1lMTFhLTQ2MzAtYTZhMi0zZjIwZmNlYzdmOTAiLCJzY29wZSI6WyJjbG91ZF9jb250cm9sbGVyLnJlYWQiLCJwYXNzd29yZC53cml0ZSIsImNsb3VkX2NvbnRyb2xsZXIud3JpdGUiLCJvcGVuaWQiXSwiY2xpZW50X2lkIjoiY2YiLCJjaWQiOiJjZiIsImF6cCI6ImNmIiwiZ3JhbnRfdHlwZSI6InBhc3N3b3JkIiwidXNlcl9pZCI6ImI2YzAyMGI0LWUxMWEtNDYzMC1hNmEyLTNmMjBmY2VjN2Y5MCIsIm9yaWdpbiI6InVhYSIsInVzZXJfbmFtZSI6InBhc2FwaUBhdTEuaWJtLmNvbSIsImVtYWlsIjoicGFzYXBpQGF1MS5pYm0uY29tIiwicmV2X3NpZyI6IjVjOGMyODQ4IiwiaWF0IjoxNDU1MDU3NzQxLCJleHAiOjE0NTUxMDA5NDEsImlzcyI6Imh0dHBzOi8vdWFhLm5nLmJsdWVtaXgubmV0L29hdXRoL3Rva2VuIiwiemlkIjoidWFhIiwiYXVkIjpbImNsb3VkX2NvbnRyb2xsZXIiLCJwYXNzd29yZCIsImNmIiwib3BlbmlkIl19.EUEIXZ-XgxQbvTQnSgrToODHbNmKvhx0PtAp9CaiPTk
>
  0     0    0     0    0     0      0      0 --:--:--  0:00:04 --:--:--     0< HTTP/1.1 200 OK
< X-Backside-Transport: OK OK
< Connection: Keep-Alive
< Transfer-Encoding: chunked
< Content-Type: application/json; charset=utf-8
< Date: Tue, 09 Feb 2016 22:54:44 GMT
< Etag: W/"3bcc-JgmFioUYI4v46tUXnGY1SQ"
< Vary: Accept-Encoding
< X-Cf-Requestid: 7b5cea8c-1a24-4114-44b2-a45e5d6e6f40
< X-Heap-Used: 136304240
< X-Instance-Id: 657c5e04638a49788a1053e7bb4e22ff
< X-Instance-Index: 5
< X-Node-Version: v0.10.41
< X-Powered-By: Express
< X-Process-Id: 93
< X-Response-Time: 3374.537ms
< X-Uptime: 16055
< X-Client-IP: 124.180.37.173
< X-Global-Transaction-ID: 750960253
<
{ [4055 bytes data]
100 15308    0 15308    0     0   3019      0 --:--:--  0:00:05 --:--:--  3967
* Connection #0 to host rated-usage.ng.bluemix.net left intact
{
    "organizations": [
        {
            "billable_usage": {
                "spaces": []
            },
            "currency_code": "AUD",
            "id": "e270a605-978e-45fc-9507-00a50dec2469",
            "name": "pasapi@au1.ibm.com",
            "non_billable_usage": {
                "spaces": [
                    {
                        "applications": [
                            {
                                "id": "121ccef0-2417-49c4-9f8f-47958b6d819d",
                                "name": "pas-bmspringboot-demo",
                                "usage": [
                                    {
                                        "buildpack": "0154f971-ae72-4882-9695-bda6e31310b7",
                                        "cost": 8.531996000805556,
                                        "quantity": 107.45586902777778,
                                        "runtime": {
                                            "id": "0154f971-ae72-4882-9695-bda6e31310b7",
                                            "name": "liberty-for-java_v2_1-20151006-0912"
                                        },
                                        "unit": "GB-HOURS",
                                        "unitId": "GB_HOURS_PER_MONTH"
                                    }
                                ]
                            },
                            {
                                "id": "2d7dfb5f-0900-4c4a-a936-edaf3b7febb8",
                                "name": "pas-tonynode",
                                "usage": [
                                    {
                                        "buildpack": "f0bff590-8b49-4c7d-bc4a-3ff24adcd411",
                                        "cost": 8.531996000805556,
                                        "quantity": 107.45586902777778,
                                        "runtime": {
                                            "id": "f0bff590-8b49-4c7d-bc4a-3ff24adcd411",
                                            "name": "sdk-for-nodejs_v2_8-20151209-1403"
                                        },
                                        "unit": "GB-HOURS",
                                        "unitId": "GB_HOURS_PER_MONTH"
                                    }
                                ]
                            },
                            {
                                "id": "3a962319-e7c4-456f-a2a4-b1f356a5d142",
                                "name": "pas-dotnet-helloworld",
                                "usage": [
                                    {
                                        "buildpack": "0a566654-d250-463e-b413-67782482e903",
                                        "cost": 4.265998000402778,
                                        "quantity": 53.72793451388889,
                                        "runtime": {
                                            "id": "0a566654-d250-463e-b413-67782482e903",
                                            "name": "aspnet5-experimental"
                                        },
                                        "unit": "GB-HOURS",
                                        "unitId": "GB_HOURS_PER_MONTH"
                                    }
                                ]
                            },
                            {
                                "id": "54629864-0e43-488f-bfca-3f9c9d806de6",
                                "name": "pas-mysql-local",
                                "usage": [
                                    {
                                        "buildpack": "dac36860-94be-495a-96f5-d81d79c2ef3f",
                                        "cost": 7.498824610083008,
                                        "quantity": 94.44363488769531,
                                        "runtime": {
                                            "id": "dac36860-94be-495a-96f5-d81d79c2ef3f",
                                            "name": "java_buildpack"
                                        },
                                        "unit": "GB-HOURS",
                                        "unitId": "GB_HOURS_PER_MONTH"
                                    }
                                ]
                            },
                            {
                                "id": "59f15702-1c42-444b-a1fb-94fbaf6cb27a",
                                "name": "pas-mobile-web",
                                "usage": [
                                    {
                                        "buildpack": "0154f971-ae72-4882-9695-bda6e31310b7",
                                        "cost": 8.531996000805556,
                                        "quantity": 107.45586902777778,
                                        "runtime": {
                                            "id": "0154f971-ae72-4882-9695-bda6e31310b7",
                                            "name": "liberty-for-java_v2_1-20151006-0912"
                                        },
                                        "unit": "GB-HOURS",
                                        "unitId": "GB_HOURS_PER_MONTH"
                                    }
                                ]
                            },
                            {
                                "id": "926900dd-ccd7-4442-8f58-413df2bc0237",
                                "name": "pas-mongodb-local",
                                "usage": [
                                    {
                                        "buildpack": "dac36860-94be-495a-96f5-d81d79c2ef3f",
                                        "cost": 7.498824610083008,
                                        "quantity": 94.44363488769531,
                                        "runtime": {
                                            "id": "dac36860-94be-495a-96f5-d81d79c2ef3f",
                                            "name": "java_buildpack"
                                        },
                                        "unit": "GB-HOURS",
                                        "unitId": "GB_HOURS_PER_MONTH"
                                    }
                                ]
                            },
                            {
                                "id": "ab5a3278-a1c1-44f6-9113-713a4d800131",
                                "name": "bluemix-apples-springboot",
                                "usage": [
                                    {
                                        "buildpack": "dac36860-94be-495a-96f5-d81d79c2ef3f",
                                        "cost": 8.531996000805556,
                                        "quantity": 107.45586902777778,
                                        "runtime": {
                                            "id": "dac36860-94be-495a-96f5-d81d79c2ef3f",
                                            "name": "java_buildpack"
                                        },
                                        "unit": "GB-HOURS",
                                        "unitId": "GB_HOURS_PER_MONTH"
                                    }
                                ]
                            },
                            {
                                "id": "b448fd3a-5350-47d2-820d-7f739a057f22",
                                "name": "pas-SpringBootJARDemo",
                                "usage": [
                                    {
                                        "buildpack": "eb0b11e9-8982-4b93-adcb-7350d0bf2ae4",
                                        "cost": 8.531996000805556,
                                        "quantity": 107.45586902777778,
                                        "runtime": {
                                            "id": "eb0b11e9-8982-4b93-adcb-7350d0bf2ae4",
                                            "name": "liberty-for-java_v2_3-20151208-1311"
                                        },
                                        "unit": "GB-HOURS",
                                        "unitId": "GB_HOURS_PER_MONTH"
                                    }
                                ]
                            },
                            {
                                "id": "b7d3d442-5546-41b4-b5c0-4ef737734e7b",
                                "name": "pas-sb-elastic",
                                "usage": [
                                    {
                                        "buildpack": "dac36860-94be-495a-96f5-d81d79c2ef3f",
                                        "cost": 7.498824610083008,
                                        "quantity": 94.44363488769531,
                                        "runtime": {
                                            "id": "dac36860-94be-495a-96f5-d81d79c2ef3f",
                                            "name": "java_buildpack"
                                        },
                                        "unit": "GB-HOURS",
                                        "unitId": "GB_HOURS_PER_MONTH"
                                    }
                                ]
                            },



No comments: