LoginSignup
6
0

More than 5 years have passed since last update.

【IBM Cloudメモ】IBM Cloudの課金情報をコマンドラインで取得する

Last updated at Posted at 2019-01-28

はじめに

IBM Cloudの課金情報をコマンドから取得できることをつい先ほど知りました。
その手順のメモをアップします。

前提

Python (動作確認は3.6)
bxコマンド (0.13.1)
gitコマンド (オプション)

導入手順

深く調べればもっといい方法があるかもしれませんが、取り急ぎ

pyBill - fetch IBM Cloud billing and usage information

のコードをダウンロードして使うのが簡単そうです。私のPCは上の条件を満たしているのですが、次のコマンドであっという間に使えるようになりました。

$ git clone https://github.com/data-henrik/pyBill.git
$ cd pyBill

利用方法

上記サイトのReadmeにあるとおりです。一応コマンド例を転記しておきます。

  • 2018年12月のインスタンス単位の課金を画面表示する
$ python ./pyBill.py -riu -print -m 2018-12
  • 2018年12月のインスタンス単位の課金をcsv出力する(リダイレクトすればファイル出力)
$ python ./pyBill.py -riu -csv -m 2018-12
  • 2018年12月のリソースグループ単位の課金を画面出力する
$ python ./pyBill.py -rgu -print  -m 2018-12

(備考)リソースグループ単位のCSV出力はできない仕様のようです。データ構造が複雑なため??

解説

一応簡単にコードの解説をしておきます。

このツールのミソは、API呼出しで必要な認証情報を~/.bluemix/config.jsonから読み取っている点にあります。
その部分のコードを抜き出すと以下の形になります。

with open(homedir+'/.bluemix/config.json') as data_file:
  credentials = json.load(data_file)
iam_token = credentials.get('IAMToken')
api_endpoint = credentials.get('APIEndpoint')
account_id=credentials.get('Account').get('GUID')
resourceGroupID=credentials.get('ResourceGroup').get('GUID')

iam_token, api_endpoint, account_id, resourceGroupIDの4つの値を読み取っていることがわかります。
逆にいうと、この4つの変数をマニュアルで設定する形にすれば、bxコマンドがない環境でもこのシェルは動作するものと思われます。

追記

ibmcloudコマンドが導入済みであれば、下記のコマンドを発行する方法が一番簡単なようです。

$ ibmcloud billing resource-instances-usage --json
$ ibmcloud billing  account-usage --json

参考リンク

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

6
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
0