このページでは、さまざまなリソース検索のユースケースのサンプルクエリを紹介します。
ユースケース: 組織内の Cloud リソースを一覧表示し、出力の形式をタプル(ASSET_TYPE、NAME、PROJECT_ID、LABELS)に設定する
gcloud asset search-all-resources \
--scope=organizations/123456 \
--page-size=50 \
--format='table(assetType.basename(), name.basename(), name.scope(projects).segment(0):label=PROJECT_ID, labels)'
--scope
を projects/12345678 または folders/1234567 に変更すると、組織ではなくプロジェクトまたはフォルダ内を検索できます。
--query
制限を追加して、より具体的なリソース検索結果を取得できます。
--asset-types
制限を追加して、より詳細なタイプのリソースを取得できます。
結果の形式を設定したくない場合は、--flatten
と --format
を削除できます。
table
ではなく csv
を使用すると、結果の形式を CSV 形式に設定できます。
--format
フレーズから .basename()
を削除すると、各フィールドの完全な名前を返すことができます。
--limit
を追加すると、検索結果のサブセットのみを取得できます。このフラグを指定しないと、すべての検索結果のページが自動的に表示されるようになります。
ユースケース: プロジェクト内のコンピューティング インスタンスを一覧表示する
gcloud asset search-all-resources \
--scope=projects/12345678 \
--asset-types='compute.googleapis.com/Instance'
--scope
を organizations/123456 または folders/1234567 に変更すると、プロジェクトではなく、組織またはフォルダ内のすべてのリソースを一覧表示できます。
--query
制限を追加して、より具体的なリソース検索結果を取得できます。
ユースケース: env
という名前のラベルを持つコンピューティング インスタンスを一覧表示する
gcloud asset search-all-resources \
--query='labels.env:*' \
--scope=organizations/123456 \
--asset-types='compute.googleapis.com/Instance' \
--page-size=50 \
--format='table(name, assetType, labels)'
ユースケース: 米国内のコンピューティング インスタンスを一覧表示する
gcloud asset search-all-resources \
--query='location:us-*' \
--scope=organizations/123456 \
--asset-types='compute.googleapis.com/Instance' \
--page-size=50 \
--format='table(name, assetType, location)'
ユースケース: 説明 instance-prod
があり、instance-prod-1
、INSTANCE-PROD
はないコンピューティング インスタンスを一覧表示します。
gcloud asset search-all-resources \
--query='description=instance-prod' \
--scope=organizations/123456 \
--asset-types='compute.googleapis.com/Instance' \
--page-size=50 \
--format='table(name, assetType, location)'
ユースケース: すべての Cloud リソースをアセットタイプとロケーションごとに計上する
gcloud asset search-all-resources \
--scope=organizations/123456 \
--page-size=500 \
--format='csv(assetType,location)' \
| sed 1d | sort | uniq -c
ユースケース: SUSPENDED
状態のコンピューティング インスタンスを一覧表示します。
gcloud asset search-all-resources \
--query='state=SUSPENDED' \
--scope=organizations/123456 \
--asset-types='compute.googleapis.com/Instance' \
--page-size=50 \
--format='table(name, assetType, location)'
ユースケース: 2021-01-01 00:00:00 UTC
よりも後に作成されたコンピューティング インスタンスを一覧表示します。
gcloud asset search-all-resources \
--query='createTime>1609459200' \
--scope=organizations/123456 \
--asset-types='compute.googleapis.com/Instance' \
--page-size=50 \
--format='table(name, assetType, location)'