gcloudとjqでGoogle Cloudのリソース情報を取得(組織/フォルダ/プロジェクト編)

記事タイトルとURLをコピーする

G-gen の武井です。

当記事では Google Cloud (旧称 GCP) のリソース情報を gcloud コマンドと jq コマンドで 取得する方法を紹介します。

概要

gcloud コマンド

gcloud コマンド ( gcloud CLI ) は、Google Cloud リソースの管理を行うコマンドツールです。

Cloud コンソール ( GUI ) と同様、Google Cloud のリソース作成からその後の管理を含め、一連の操作をコマンドラインを介して行えます。

jq コマンド

jq コマンドは JSON データの加工や整形 ( 抽出 / 変換 / 集計 ) をするツールです。Linux でいう grep / sed / awk コマンドに相当します。

実行方法

それぞれのコマンドを組み合わせて実行するには、 gcloud コマンドの取得したデータを | (パイプ) コマンドで jq コマンドに渡します。

gcloud コマンドで取得したデータは JSON 形式して渡す必要があるため、--format を使ってデータを変換します。

コマンドツールの準備

Cloud Shell では gcloud / jq コマンドのどちらもインストール済みですが、それ以外の環境ではインストールが必要です。

※ 当記事では実行例を中心に紹介するため、インストール方法等の説明は割愛します。

使い方

gcloud organizations list コマンド ( 組織を一覧表示するコマンド ) を例に使い方を紹介します。

失敗例

前述の通り、jq コマンドに渡すデータは JSON 形式にする必要があります。

gcloud コマンドの出力 (デフォルト) はテキスト形式なので、データ形式を明示的に指定しない場合はエラーになります。

$ gcloud organizations list | jq
  
parse error: Invalid numeric literal at line 1, column 13

成功例

--format=json でデータ形式を JSON に変換して jq コマンドに渡すことでコマンドラインが成功します。

まずは jq コマンドには渡さず、JSON 形式で出力した場合の例です。

$ gcloud organizations list --format=json
  
[
  {
    "creationTime": "2023-01-19T23:02:33.677Z",
    "displayName": "example001.co.jp",
    "lifecycleState": "ACTIVE",
    "name": "organizations/11111111",
    "owner": {
      "directoryCustomerId": "AAAAAAAA"
    }
  },
  {
    "creationTime": "2022-11-02T00:35:22.696Z",
    "displayName": "example002.co.jp",
    "lifecycleState": "ACTIVE",
    "name": "organizations/22222222",
    "owner": {
      "directoryCustomerId": "BBBBBBBB"
    }
  },
  {
    "creationTime": "2021-11-11T03:22:50.021Z",
    "displayName": "example003.co.jp",
    "lifecycleState": "ACTIVE",
    "name": "organizations/33333333",
    "owner": {
      "directoryCustomerId": "CCCCCCCC"
    }
  },
  {
    "creationTime": "2021-10-27T03:09:00.929Z",
    "displayName": "example004.co.jp",
    "lifecycleState": "ACTIVE",
    "name": "organizations/44444444",
    "owner": {
      "directoryCustomerId": "DDDDDDDD"
    }
  },
  {
    "creationTime": "2021-08-12T08:08:27.729Z",
    "displayName": "example005.co.jp",
    "lifecycleState": "ACTIVE",
    "name": "organizations/55555555",
    "owner": {
      "directoryCustomerId": "EEEEEEEE"
    }
  }
]

次に jq コマンドに渡した場合です。オプション等を使用せず、単に jq に渡しただけでは前述の出力と変わりありません。

jq コマンドによる加工や整形については次の章でご紹介します。

$ gcloud organizations list --format=json | jq 
  
[
  {
    "creationTime": "2023-01-19T23:02:33.677Z",
    "displayName": "example001.co.jp",
    "lifecycleState": "ACTIVE",
    "name": "organizations/11111111",
    "owner": {
      "directoryCustomerId": "AAAAAAAA"
    }
  },
  {
    "creationTime": "2022-11-02T00:35:22.696Z",
    "displayName": "example002.co.jp",
    "lifecycleState": "ACTIVE",
    "name": "organizations/22222222",
    "owner": {
      "directoryCustomerId": "BBBBBBBB"
    }
  },
  {
    "creationTime": "2021-11-11T03:22:50.021Z",
    "displayName": "example003.co.jp",
    "lifecycleState": "ACTIVE",
    "name": "organizations/33333333",
    "owner": {
      "directoryCustomerId": "CCCCCCCC"
    }
  },
  {
    "creationTime": "2021-10-27T03:09:00.929Z",
    "displayName": "example004.co.jp",
    "lifecycleState": "ACTIVE",
    "name": "organizations/44444444",
    "owner": {
      "directoryCustomerId": "DDDDDDDD"
    }
  },
  {
    "creationTime": "2021-08-12T08:08:27.729Z",
    "displayName": "example005.co.jp",
    "lifecycleState": "ACTIVE",
    "name": "organizations/55555555",
    "owner": {
      "directoryCustomerId": "EEEEEEEE"
    }
  }
]

実例

組織

組織に関する情報の取得には、gcloud organizations list コマンドを使用しますが、その中から特定の値を抽出する方法をいくつかご紹介します。

組織名 (ドメイン名) を抽出

組織名の key は displayName ですので、以下のようにコマンドを実行すると組織名を抽出できます。

$ gcloud organizations list --format json | jq -r '.[].displayName'
  
example001.co.jp
example002.co.jp
example003.co.jp
example004.co.jp
example005.co.jp

組織 ID を抽出

組織 ID の key は name ですので、以下のようにコマンドを実行すると組織 ID を抽出できます。

$ gcloud organizations list --format json | jq -r '.[].name'
  
organizations/11111111
organizations/22222222
organizations/33333333
organizations/44444444
organizations/55555555

ID 部分だけを抽出したい場合、"organizations/" は不要となるので除外します。
( どちらも結果は同じ )

# gsub に渡して置換するパターン
$ gcloud organizations list --format json | jq -r '.[].name | gsub("organizations/";"")'
  
11111111
22222222
33333333
44444444
55555555
# sed に渡して置換するパターン
$ gcloud organizations list --format json | jq -r '.[].name' | sed -e "s|organizations/||"
  
11111111
22222222
33333333
44444444
55555555

条件に合致する値を抽出

select を使用して条件 (組織名) に合致する値 (組織 ID) を抽出します。

# gsub に渡して置換するパターン
$ gcloud organizations list --format json | jq -r '.[] |  select(.displayName == "example001.co.jp") | .name | gsub("organizations/";"")'
  
11111111
# sed に渡して置換するパターン
$ gcloud organizations list --format json | jq -r '.[] |  select(.displayName == "example001.co.jp") | .name'| sed -e "s|organizations/||"
  
11111111

key を , (カンマ) で区切れば複数の値 (組織 ID と 顧客 ID) も抽出可能です。

# gsub に渡して置換するパターン
$ gcloud organizations list --format json | jq -r '.[] |  select(.displayName == "example001.co.jp") | .displayName,.name,.owner.directoryCustomerId | gsub("organizations/";"")'
  
example001.co.jp
11111111
AAAAAAAA
# sed に渡して置換するパターン
gcloud organizations list --format json | jq -r '.[] |  select(.displayName == "example001.co.jp") | .displayName,.name,.owner.directoryCustomerId' | sed -e "s|organizations/||"
  
example001.co.jp
11111111
AAAAAAAA

フォルダ

フォルダに関する情報の取得には、gcloud resource-manager folders list コマンドを使用しますが、その中から特定の値を抽出する方法をいくつかご紹介します。

出力例は以下のとおりです。

$ gcloud resource-manager folders list --organization=${ORG_ID} --format json
  
[
  {
    "createTime": "2021-12-26T15:07:59.662Z",
    "displayName": "dev",
    "lifecycleState": "ACTIVE",
    "name": "folders/11111111",
    "parent": "organizations/11111111"
  },
  {
    "createTime": "2021-10-27T03:32:42.669Z",
    "displayName": "stg",
    "lifecycleState": "ACTIVE",
    "name": "folders/22222222",
    "parent": "organizations/11111111"
  },
  {
    "createTime": "2022-11-11T11:26:24.972Z",
    "displayName": "prod",
    "lifecycleState": "ACTIVE",
    "name": "folders/33333333",
    "parent": "organizations/11111111"
  }
]

フォルダ名を抽出

フォルダ名の key は displayName ですので、以下のようにコマンドを実行するとフォルダ名だけを抽出できます。

$ gcloud resource-manager folders list --organization=${ORG_ID} --format json | jq -r '.[].displayName'
  
dev
stg
prod

フォルダ ID を抽出

フォルダ ID の key は name ですので、以下のようにコマンドを実行するとフォルダ ID だけを抽出できます。

$ gcloud resource-manager folders list --organization=${ORG_ID} --format json | jq -r '.[].name'
  
folders/11111111
folders/22222222
folders/33333333

ID 部分だけを抽出したい場合、"folders/" は不要となるので除外します。
( どちらも結果は同じ )

# gsub に渡して置換するパターン
$ gcloud resource-manager folders list --organization=${ORG_ID} --format json | jq -r '.[].name | gsub("folders/";"")'
  
11111111
22222222
33333333
# sed に渡して置換するパターン
$ gcloud resource-manager folders list --organization=${ORG_ID} --format json | jq -r '.[].name' | sed -e "s|folders/||"
  
11111111
22222222
33333333

条件に合致する値を抽出

select を使用して条件 (フォルダ名) に合致する値 (フォルダ ID) だけを抽出します。

# gsub に渡して置換するパターン
$ gcloud resource-manager folders list --organization=${ORG_ID} --format json | jq -r '.[] |  select(.displayName == "dev") | .name | gsub("folders/";"")'
  
11111111
# sed に渡して置換するパターン
$ gcloud resource-manager folders list --organization=${ORG_ID} --format json | jq -r '.[] |  select(.displayName == "dev") | .name' | sed -e "s|folders/||"
  
11111111

プロジェクト

プロジェクトに関する情報の取得には、gcloud projects list コマンドを使用しますが、その中から特定の値を抽出する方法をいくつかご紹介します。

今回の例ではフォルダ配下のプロジェクトをリストするため、${FLDR_ID} で特定のフォルダ ID を指定します。

また、組織直下に作成されたプロジェクトリソースをリストする場合は組織 ID を指定します。

$ gcloud projects list --filter="parent.id: ${FLDR_ID}" --format json
  
[
  {
    "createTime": "2023-02-07T04:52:43.403Z",
    "lifecycleState": "ACTIVE",
    "name": "alpha",
    "parent": {
      "id": "11111111",
      "type": "folder"
    },
    "projectId": "alpha",
    "projectNumber": "11111111"
  },
  {
    "createTime": "2023-01-06T03:57:43.872Z",
    "lifecycleState": "ACTIVE",
    "name": "bravo",
    "parent": {
      "id": "11111111",
      "type": "folder"
    },
    "projectId": "bravo",
    "projectNumber": "22222222"
  },
  {
    "createTime": "2022-12-20T05:53:14.193Z",
    "lifecycleState": "ACTIVE",
    "name": "charlie",
    "parent": {
      "id": "11111111",
      "type": "folder"
    },
    "projectId": "charlie",
    "projectNumber": "33333333"
  }
]

プロジェクト名を抽出

プロジェクト名の key は name ですので、以下のようにコマンドを実行するとプロジェクト名だけを抽出できます。

$ gcloud projects list --filter="parent.id: ${FLDR_ID}" --format json | jq -r '.[].name'
  
alpha
bravo
charlie

プロジェクト ID を抽出

プロジェクト ID の key は projectId' ですので、以下のようにコマンドを実行するとフォルダ ID だけを抽出できます。

$ gcloud projects list --filter="parent.id: ${FLDR_ID}" --format json | jq -r '.[].projectId'
  
alpha
bravo
charlie

プロジェクト番号 を抽出

プロジェクト ID の key は projectNumber' ですので、以下のようにコマンドを実行するとフォルダ ID だけを抽出できます。

$ gcloud projects list --filter="parent.id: ${FLDR_ID}" --format json | jq -r '.[].projectNumber'
  
11111111
22222222
33333333

条件に合致する値を抽出

select を使用して条件 (プロジェクト名) に合致する値 (プロジェクト ID と番号) だけを抽出します。

$ gcloud projects list --filter="parent.id: ${FLDR_ID}" --format json | jq -r '.[] | select(.name == "alpha") | .projectId, .projectNumber'
  
alpha
11111111

武井 祐介 (記事一覧)

2022年4月にジョイン。クラウドソリューション部所属。G-gen唯一の山梨県在住エンジニア

Google Cloud Partner Top Engineer 2024 に選出。IaC や CI/CD 周りのサービスやプロダクトが興味分野です。

趣味はロードバイク、ロードレースやサッカー観戦です。