LoginSignup
2
0

More than 3 years have passed since last update.

SORACOMのSIM一覧CSVダウンロード機能をcliで試してみた

Posted at

SIM一覧をダウンロードする

先日、SORACOMコンソールからSIMの一覧をCSV形式でダウンロードできるようになりました。

SIM管理の省力化に、SIM一覧のCSVダウンロード機能を提供開始 - ソラコム公式blog

ブログにもあるように、cliで取得する場合はJSONで出力されます。該当のコマンドはこんな感じですね。

%soracom subscribers list

cli使ってる分にはJSONで落としてきてcsvに成形すればいいんですが、コンソールからできることはAPIでできること、そしてAPIでできるならきっとcliでも出来るだろうと言うことで確認してみました。

なお、本記事は2021/4/30現在での状況ですのでご注意ください。

helpを確認する

最新版(2021/4/30時点では最新版はv0.7.0)でコマンドラインのヘルプを見てみます。まずはsubscribersに専用のコマンドがあるかなということでsubscribersのヘルプを見ます。

%soracom subscribers --help
Manage subscribers.

Usage:
  soracom subscribers [command]
(省略)

出力は長くなるので省略しますが、なさそうです。ではlistコマンドのオプションにazure cliっぽく--output csvなんてオプションないかなと調べます。

%soracom subscribers list --help
Returns a list of subscribers that match certain criteria. If the total number of subscribers does not fit in one page, a URL for accessing the next page is returned in the 'Link' header of the response.

Usage:
  soracom subscribers list [flags]

Flags:
      --fetch-all                     Do pagination automatically.
  -h, --help                          help for list
      --last-evaluated-key string     The IMSI of the last subscriber retrieved on the current page. By specifying this parameter, you can continue to retrieve the list from the next subscriber onward.
      --limit int                     Maximum number of subscribers to retrieve.
      --serial-number-filter |        Serial number for filtering the search. Can specify multiple values delimited by |. Returns subscribers with serial number starting with the specified value(s).
      --speed-class-filter |          Speed class for filtering the search. Can specify multiple values delimited by |. Valid values include: `s1.minimum`, `s1.slow`, `s1.standard`, `s1.fast`
      --status-filter |               Status for filtering the search. Can specify multiple values delimited by |. Valid values include: `active`, `inactive`, `ready`, `instock`, `shipped`, `suspended`, and `terminated`.
      --tag-name string               Tag name for filtering the search (exact match).
      --tag-value tag_name            Tag search string for filtering the search. Required when tag_name has been specified.
      --tag-value-match-mode string   Tag match mode. (default "exact")

こちらもなさそうです。

通信を確認する

こういうときはコンソールにアクセスして実際にCSVをダウンロードし、その時の通信内容をブラウザの開発ツールなどで見て、どういうAPIが呼ばれてるかを確認してみます。

Chromeでやってみたところ、こんな感じになりました。通信先が「api.soracom.io」のものに絞っています。

image.png

前半のcell_locationsまでのは、SIM一覧ページを表示するまでのものです。CSVダウンロードはそのあとのexport?export_mode=async以降になります。この通信内容を見ると、

  • https://api.soracom.io/v1/subscribers/export?export_mode=async にPOSTする
  • 出力されたファイル名が戻される
  • https://api.soracom.io/v1/files/exported/ファイル名をGETして取得する

という動作のようです。

次はこのAPIがどういうものかをAPIリファレンスで調べるのとになりますが、どうも、subscribers/exportというのはまだないようです。

同じように「CSVで取得する(ためにファイルをエクスポートする)」というAPIで最新の支払い情報をCSVでs3にエクスポートするためのbills/latest/exportというのがあるので(他にもいくつか同種のものあり)、これに合わせた形で実装はされてるもののまだドキュメントの公開やcliの対応はされてないという状況のようです。

(参考)cliで最新の支払い情報をCSVで取得する

ちなみに、最新の支払い情報をcliでCSV形式で取得するには以下のようにします。

%soracom bills export-latest --export-mode async
{
        "exportedFileId": "fe_********************************",
        "exportedFilePath": "//files/exported/fe_********************************"
}
%soracom files get --scope exported --path fe_********************************"
{"status":"exported","url":"https://soracom-billing-fileoutput-prod.s3.ap-northeast-1.amazonaws.com/******"}

最後のコマンドで戻ってきたurlにアクセスすると、csvファイルが取得できます。

まとめ

SIM一覧のCSV出力機能はcliではまだ実装されてませんでしたが、今回はSORACOMコンソールの機能が裏側ではどういうAPIを呼んでいて、同じことをcliでやるときにどうするのかを調べる時の一つの手順を書いてみました。

2
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
2
0