LoginSignup
5
0

More than 3 years have passed since last update.

[Oracle Cloud] テナント上のリソース一覧をテキストで出力してみる

Posted at

やったこと

テナント上のリソース一覧をテキスト形式で出力してみた。
作成されてるリソースの一覧が欲しい、エビデンス用途、OCIDの一覧が欲しいとか、そういった用途に。

参考にしたもの

Oracle Cloud Infrastructure Documentation - Querying Resources
Oracle Cloud Infrastructure Documentation - Sample Queries

準備

oci cliの導入

Oracle Cloud 用の oci cli を導入を参考にoci cliを導入

jqの導入

# yum install -y jq

内容

コマンドライン

以下のコマンドを実行すると、すべてのリソースをJSON形式で出力できる。(出力される範囲は、実行しているアカウントの権限による)

# oci search resource structured-search --query-text 'query all resources'
…
      },
      {
        "availability-domain": null,
        "compartment-id": "xxxxxxxxxxxxxx",
        "defined-tags": {},
        "display-name": "xxxxxxxxxxxxxx",
        "freeform-tags": {},
        "identifier": "xxxxxxxxxxxxxx",
        "lifecycle-state": "ACTIVE",
        "resource-type": "User",
        "search-context": null,
        "time-created": "2019-04-15T11:05:46.555000+00:00"
      }
    ]
  }
}

コンパートメント単位で出力したい場合は、where句で絞ることもできる

# COMPARTMENT_ID="コンパートメントのOCID"
# oci search resource structured-search --query-text 'query all resources where compartmentId = "'"${COMPARTMENT_ID}"'"'

JSON形式だと見づらいので、以下項目を抽出してCSV形式で出力してみる。( jqの使い方はいまいちわかっていない )

  • "compartment-id"
  • "resource-type"
  • "display-name"
  • "lifecycle-state"
  • "identifier"
  • "time-created"
#  oci search resource structured-search --query-text 'query all resources' |jq -r '."data"."items"' |jq -r '["compartment-id","resource-type","display-name","lifecycle-state","identifier","time-created"],(. [] |[."compartment-id",."resource-type",."display-name",."lifecycle-state",."identifier",."time-created"]) |@csv' |sort -t , -k 1,2
…
"ocid1.tenancy.oc1..xxxxxxxxxx","User","xxxxxxxxxx","ACTIVE","ocid1.user.oc1..xxxxxxxxxx","2019-09-25T02:38:23.891000+00:00"
"ocid1.tenancy.oc1..xxxxxxxxxx","Vcn","xxxxxxxxxx","AVAILABLE","ocid1.vcn.oc1.xxxxxxxxxx","2019-06-24T12:25:38.400000+00:00"

このクエリだけでは、リソースの詳細は出力できないので、詳細も出力したい場合は、リソースごとにoci xxx list コマンドも実行しないといけない

コンソール (おまけ)

このクエリ、コンソールからも実行できる。

In the Console, if you are not already there, append "/a/query" to the end of your base Console URL. For example, https://console.us-ashburn-1.oraclecloud.com/a/query.

マニュアルにあるとおり、コンソールURLの最後に/a/queryを入力すると以下の画面に遷移する。ドロップダウンリストからサンプルクエリを表示させて、修正して実行することができる。

query.png

コンパートメント単位でリソース一覧を、コンソールから見たい場合は、コンパートメント・エクスプローラのほうが一覧性があって使いやすいかもしれない

メニュー→[ガバナンス]→[コンパートメント・エクスプローラ]
query02.png

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