您可以搜尋專案、資料夾或組織中的資源,並使用查詢篩選傳回的結果。
事前準備
在執行 Cloud Asset Inventory 指令的專案中,啟用 Cloud Asset Inventory API。
確認帳戶具備呼叫 Cloud Asset Inventory API 的正確角色。如要瞭解各個呼叫類型的個別權限,請參閱「權限」。
建構查詢
建構查詢前,建議先從「不」指定查詢的搜尋要求開始。使用完整回應中的欄位和值,透過搜尋查詢語法建立查詢,並加以調整,直到傳回所需結果。
如要進一步瞭解查詢中可使用的欄位,請參閱 searchAllResources 參考文件。
並非所有資源都能搜尋。請參閱資源類型,瞭解資源名稱,並確認是否可供搜尋 API 使用。
搜尋資源
控制台
如要搜尋資源中繼資料,請完成下列步驟。
-
前往 Google Cloud 控制台的「Asset Inventory」(資產目錄) 頁面。
- 變更為要搜尋的專案、資料夾或組織。
- 按一下「資源」分頁標籤。
-
如要搜尋資源,請在「Filter」(篩選器) 欄位中輸入查詢。如要瞭解如何編寫搜尋查詢,請參閱「搜尋查詢語法」。
如要輕鬆建構查詢,請按一下「Filter」(篩選器) 搜尋欄位,即可顯示可搜尋的欄位,並將其新增至查詢。
執行搜尋後,符合查詢條件的資源會列在「Results」(結果) 表格中。按兩下查詢即可編輯,或使用「Filter results」(篩選結果) 窗格,依特定條件限制搜尋結果。
如要以 Google Cloud CLI 指令的形式查看查詢,請按一下「View query」(查看查詢)。
如要匯出結果,請按一下「下載 CSV」。
gcloud
gcloud asset search-all-resources \ --scope=SCOPE_PATH \ --query="QUERY" \ --asset-types=ASSET_TYPE_1,ASSET_TYPE_2,... \ --order-by="ORDER_BY" \ --read-mask="READ_MASK"
提供以下這些值:
-
SCOPE_PATH:請使用下列其中一個值:-
projects/PROJECT_ID,其中PROJECT_ID是要搜尋資產的專案 ID。 -
projects/PROJECT_NUMBER,其中PROJECT_NUMBER是要搜尋資產的專案編號。如何找出 Google Cloud 專案編號
Google Cloud 控制台
如要找出 Google Cloud 專案編號,請完成下列步驟:
gcloud CLI
您可以使用下列指令擷取 Google Cloud 專案編號:
gcloud projects describe PROJECT_ID --format="value(projectNumber)"
-
folders/FOLDER_ID,其中FOLDER_ID是要搜尋資產的資料夾 ID。如何找出 Google Cloud 資料夾的 ID
Google Cloud 控制台
如要找出 Google Cloud 資料夾的 ID,請完成下列步驟:
-
前往 Google Cloud 控制台。
- 按一下選單列中的「切換器」清單方塊。
- 從清單方塊中選取貴機構。
- 搜尋資料夾名稱。資料夾 ID 會顯示在資料夾名稱旁邊。
gcloud CLI
您可以使用下列指令,擷取位於組織層級的 Google Cloud 資料夾 ID:
gcloud resource-manager folders list \ --organization=$(gcloud organizations describe ORGANIZATION_NAME \ --format="value(name.segment(1))") \ --filter='"DISPLAY_NAME":"TOP_LEVEL_FOLDER_NAME"' \ --format="value(ID)"
其中 TOP_LEVEL_FOLDER_NAME 是資料夾名稱的部分或完整字串比對。移除
--format旗標,即可查看找到的資料夾詳細資訊。上述指令不會傳回資料夾內子資料夾的 ID。如要這麼做,請使用頂層資料夾的 ID 執行下列指令:
gcloud resource-manager folders list --folder=FOLDER_ID
-
-
organizations/ORGANIZATION_ID,其中ORGANIZATION_ID是含有您要搜尋資產的組織 ID。如何找出 Google Cloud 組織的 ID
Google Cloud 控制台
如要找出 Google Cloud 組織的 ID,請完成下列步驟:
-
前往 Google Cloud 控制台。
- 按一下選單列中的「切換器」清單方塊。
- 從清單方塊中選取貴機構。
- 按一下「全部」分頁標籤。組織 ID 會顯示在組織名稱旁邊。
gcloud CLI
您可以使用下列指令,擷取 Google Cloud 組織的 ID:
gcloud organizations describe ORGANIZATION_NAME --format="value(name.segment(1))"
-
-
-
QUERY:選用。查詢運算式。如果未指定或留空,系統會在指定範圍內搜尋所有資源。如要瞭解如何撰寫搜尋查詢,請參閱「搜尋查詢語法」。 -
ASSET_TYPE_#:選用。以半形逗號分隔的可搜尋資產類型清單。支援與 RE2 相容的規則運算式。如果規則運算式與任何支援的資產類型都不相符,系統會傳回INVALID_ARGUMENT錯誤。如未指定--asset-types,系統會傳回所有資產類型。 -
ORDER_BY:選用。以半形逗號分隔的欄位清單,用於指定結果的排序順序。預設為遞增排序。在欄位名稱後方加上DESC,表示遞減順序。如要瞭解可排序的欄位,請參閱參考文件。 -
READ_MASK:選用。以半形逗號分隔的欄位清單,指定要在結果中傳回哪些欄位。如要瞭解預設傳回的欄位,以及指定讀取遮罩時應注意的其他條件,請參閱參考文件。
您可以使用 --format 和 --flatten 旗標,設定 gcloud CLI 輸出的格式。
如要瞭解所有選項,請參閱 gcloud CLI 參考資料。
範例
執行下列指令,列出 my-project 專案中所有 Compute Engine 執行個體,其完整資源名稱包含 instance 字詞,後接 1。結果會依名稱遞減排序,且由於讀取遮罩,系統只會傳回 name 和 location 欄位。
gcloud asset search-all-resources \ --scope=projects/my-project \ --query="name:instance 1" \ --asset-types=compute.googleapis.com/Instance \ --order-by="name DESC" \ --read-mask="name,location"
回應範例
--- location: us-central1-a name: //compute.googleapis.com/projects/my-project/zones/us-central1-a/instances/instance-2-1 --- location: us-central1-a name: //compute.googleapis.com/projects/my-project/zones/us-central1-a/instances/instance-1
REST
HTTP 方法和網址:
POST https://cloudasset.googleapis.com/v1/SCOPE_PATH:searchAllResources
JSON 要求主體:
{ "query": "QUERY", "assetTypes": [ "ASSET_TYPE_1", "ASSET_TYPE_2", "..." ], "orderBy": "ORDER_BY", "pageSize": "PAGE_SIZE", "pageToken": "PAGE_TOKEN", "readMask": "READ_MASK" }
提供以下這些值:
-
SCOPE_PATH:請使用下列其中一個值:-
projects/PROJECT_ID,其中PROJECT_ID是要搜尋資產的專案 ID。 -
projects/PROJECT_NUMBER,其中PROJECT_NUMBER是要搜尋資產的專案編號。如何找出 Google Cloud 專案編號
Google Cloud 控制台
如要找出 Google Cloud 專案編號,請完成下列步驟:
gcloud CLI
您可以使用下列指令擷取 Google Cloud 專案編號:
gcloud projects describe PROJECT_ID --format="value(projectNumber)"
-
folders/FOLDER_ID,其中FOLDER_ID是要搜尋資產的資料夾 ID。如何找出 Google Cloud 資料夾的 ID
Google Cloud 控制台
如要找出 Google Cloud 資料夾的 ID,請完成下列步驟:
-
前往 Google Cloud 控制台。
- 按一下選單列中的「切換器」清單方塊。
- 從清單方塊中選取貴機構。
- 搜尋資料夾名稱。資料夾 ID 會顯示在資料夾名稱旁邊。
gcloud CLI
您可以使用下列指令,擷取位於組織層級的 Google Cloud 資料夾 ID:
gcloud resource-manager folders list \ --organization=$(gcloud organizations describe ORGANIZATION_NAME \ --format="value(name.segment(1))") \ --filter='"DISPLAY_NAME":"TOP_LEVEL_FOLDER_NAME"' \ --format="value(ID)"
其中 TOP_LEVEL_FOLDER_NAME 是資料夾名稱的部分或完整字串比對。移除
--format旗標,即可查看找到的資料夾詳細資訊。上述指令不會傳回資料夾內子資料夾的 ID。如要這麼做,請使用頂層資料夾的 ID 執行下列指令:
gcloud resource-manager folders list --folder=FOLDER_ID
-
-
organizations/ORGANIZATION_ID,其中ORGANIZATION_ID是含有您要搜尋資產的組織 ID。如何找出 Google Cloud 組織的 ID
Google Cloud 控制台
如要找出 Google Cloud 組織的 ID,請完成下列步驟:
-
前往 Google Cloud 控制台。
- 按一下選單列中的「切換器」清單方塊。
- 從清單方塊中選取貴機構。
- 按一下「全部」分頁標籤。組織 ID 會顯示在組織名稱旁邊。
gcloud CLI
您可以使用下列指令,擷取 Google Cloud 組織的 ID:
gcloud organizations describe ORGANIZATION_NAME --format="value(name.segment(1))"
-
-
-
QUERY:選用。查詢運算式。如果未指定或留空,系統會在指定範圍內搜尋所有資源。如要瞭解如何撰寫搜尋查詢,請參閱「搜尋查詢語法」。 -
ASSET_TYPE_#:選用。可搜尋的素材資源類型陣列。支援與 RE2 相容的規則運算式。如果規則運算式與任何支援的資產類型都不相符,系統會傳回INVALID_ARGUMENT錯誤。如未指定assetTypes,系統會傳回所有資產類型。 -
ORDER_BY:選用。以半形逗號分隔的欄位清單,用於指定結果的排序順序。預設為遞增排序。在欄位名稱後方加上DESC,表示遞減順序。如要瞭解可排序的欄位,請參閱參考文件。 -
PAGE_SIZE:選用。每頁傳回的結果數量。上限為 500。如果值設為0或負值,系統會選取適當的預設值。系統會傳回nextPageToken,用於擷取後續結果。 -
PAGE_TOKEN:選用。較長的要求回應會分成多個頁面。如未指定pageToken,系統會傳回第一頁。如要呼叫後續頁面,請將前一個回應的nextPageToken做為pageToken值。 -
READ_MASK:選用。以半形逗號分隔的欄位清單,指定要在結果中傳回哪些欄位。如要瞭解預設傳回的欄位,以及指定讀取遮罩時應留意的其他條件,請參閱參考文件。
如要瞭解所有選項,請參閱 REST 參考資料。
指令範例
執行下列任一指令,列出 my-project 專案中所有 Compute Engine 執行個體,其完整資源名稱包含 instance 字詞,後接 1。結果會依名稱遞減排序,且只會傳回 name 和 location 欄位。
curl (Linux、macOS 或 Cloud Shell)
curl -X POST \ -H "X-HTTP-Method-Override: GET" \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d '{ "query": "name:instance 1", "assetTypes": ["compute.googleapis.com/Instance"], "orderBy": "name DESC", "readMask": "name,location" }' \ https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources
PowerShell (Windows)
$cred = gcloud auth print-access-token $headers = @{ "X-HTTP-Method-Override" = "GET"; "Authorization" = "Bearer $cred" } $body = @" { "query": "name:instance 1", "assetTypes": ["compute.googleapis.com/Instance"], "orderBy": "name DESC", "readMask": "name,location" } "@ Invoke-WebRequest ` -Method POST ` -Headers $headers ` -ContentType: "application/json; charset=utf-8" ` -Body $body ` -Uri "https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources" | Select-Object -Expand Content
回應範例
{ "results": [ { "name": "//compute.googleapis.com/projects/my-project/zones/us-central1-a/instances/instance-2-1", "location": "us-central1-a" }, { "name": "//compute.googleapis.com/projects/my-project/zones/us-central1-a/instances/instance-1", "location": "us-central1-a" } ] }
C#
如要瞭解如何安裝及使用 Cloud Asset Inventory 的用戶端程式庫,請參閱「Cloud Asset Inventory 用戶端程式庫」。
如要向 Cloud Asset Inventory 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Go
如要瞭解如何安裝及使用 Cloud Asset Inventory 的用戶端程式庫,請參閱「Cloud Asset Inventory 用戶端程式庫」。
如要向 Cloud Asset Inventory 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Java
如要瞭解如何安裝及使用 Cloud Asset Inventory 的用戶端程式庫,請參閱「Cloud Asset Inventory 用戶端程式庫」。
如要向 Cloud Asset Inventory 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Node.js
如要瞭解如何安裝及使用 Cloud Asset Inventory 的用戶端程式庫,請參閱「Cloud Asset Inventory 用戶端程式庫」。
如要向 Cloud Asset Inventory 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
PHP
如要瞭解如何安裝及使用 Cloud Asset Inventory 的用戶端程式庫,請參閱「Cloud Asset Inventory 用戶端程式庫」。
如要向 Cloud Asset Inventory 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Python
如要瞭解如何安裝及使用 Cloud Asset Inventory 的用戶端程式庫,請參閱「Cloud Asset Inventory 用戶端程式庫」。
如要向 Cloud Asset Inventory 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
小茹
如要瞭解如何安裝及使用 Cloud Asset Inventory 的用戶端程式庫,請參閱「Cloud Asset Inventory 用戶端程式庫」。
如要向 Cloud Asset Inventory 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
其他搜尋範例
下列程式碼範例顯示 gcloud 和 REST 的特定搜尋查詢,協助您建構自己的搜尋。
專案中的所有資源
以下範例說明如何搜尋 my-project 專案中的所有資源。
gcloud
gcloud asset search-all-resources \ --scope=projects/my-project \ --format="table(assetType.basename(), name.basename(), name.scope(projects).segment(0):label=PROJECT_ID, labels)"
REST
HTTP 方法和網址:
GET https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources
指令範例
curl (Linux、macOS 或 Cloud Shell)
curl -X GET \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources
PowerShell (Windows)
$cred = gcloud auth print-access-token $headers = @{ "Authorization" = "Bearer $cred" } Invoke-WebRequest ` -Method GET ` -Headers $headers ` -Uri "https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources" | Select-Object -Expand Content
依說明列出 Compute Engine 執行個體
以下範例說明如何搜尋 my-project 專案中名稱完全符合 instance-prod 的 Compute Engine 執行個體。
gcloud
gcloud asset search-all-resources \ --scope=projects/my-project \ --query="description=instance-prod" \ --asset-types=compute.googleapis.com/Instance \ --format="table(name, assetType, location)"
REST
HTTP 方法和網址:
POST https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources
JSON 要求主體:
{ "assetTypes": "compute.googleapis.com/Instance", "pageSize": 1, "query": "description=instance-prod", "readMask": "name,assetType,location" }
指令範例
curl (Linux、macOS 或 Cloud Shell)
curl -X POST \ -H "X-HTTP-Method-Override: GET" \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d '{ "assetTypes": "compute.googleapis.com/Instance", "pageSize": 1, "query": "description=instance-prod", "readMask": "name,assetType,location" }' \ https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources
PowerShell (Windows)
$cred = gcloud auth print-access-token $headers = @{ "X-HTTP-Method-Override" = "GET"; "Authorization" = "Bearer $cred" } $body = @" { "assetTypes": "compute.googleapis.com/Instance", "pageSize": 1, "query": "description=instance-prod", "readMask": "name,assetType,location" } "@ Invoke-WebRequest ` -Method POST ` -Headers $headers ` -ContentType: "application/json; charset=utf-8" ` -Body $body ` -Uri "https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources" | Select-Object -Expand Content
如要搜尋名稱中含有 instance-prod 的執行個體 (例如 instance-prod-1 和 instance-prod-2),而非完全比對的執行個體,請將 =instance-prod 替換為 :instance-prod。
Compute Engine 執行個體 (依標籤區分)
下列範例說明如何使用名為 env 的標籤,在 my-project 專案中搜尋 Compute Engine 執行個體。
gcloud
gcloud asset search-all-resources \ --scope=projects/my-project \ --query="labels.env:*" \ --asset-types=compute.googleapis.com/Instance \ --format="table(name, assetType, labels)"
REST
HTTP 方法和網址:
POST https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources
JSON 要求主體:
{ "assetTypes": "compute.googleapis.com/Instance", "pageSize": 1, "query": "labels.env:*", "readMask": "name,assetType,labels" }
指令範例
curl (Linux、macOS 或 Cloud Shell)
curl -X POST \ -H "X-HTTP-Method-Override: GET" \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d '{ "assetTypes": "compute.googleapis.com/Instance", "pageSize": 1, "query": "labels.env:*", "readMask": "name,assetType,labels" }' \ https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources
PowerShell (Windows)
$cred = gcloud auth print-access-token $headers = @{ "X-HTTP-Method-Override" = "GET"; "Authorization" = "Bearer $cred" } $body = @" { "assetTypes": "compute.googleapis.com/Instance", "pageSize": 1, "query": "labels.env:*", "readMask": "name,assetType,labels" } "@ Invoke-WebRequest ` -Method POST ` -Headers $headers ` -ContentType: "application/json; charset=utf-8" ` -Body $body ` -Uri "https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources" | Select-Object -Expand Content
依 TagKey 區分的 Compute Engine 執行個體
下列範例說明如何搜尋 my-project 專案中直接附加 TagKey 標記的 Compute Engine 執行個體。namespacedName
包含 env。
如要改為依生效 TagKey 搜尋,請將 tagKeys 替換為 effectiveTagKeys。
gcloud
gcloud asset search-all-resources \ --scope=projects/my-project \ --query="tagKeys:env" \ --asset-types=compute.googleapis.com/Instance \ --format="table(name, assetType, tags)"
REST
HTTP 方法和網址:
POST https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources
JSON 要求主體:
{ "assetTypes": "compute.googleapis.com/Instance", "pageSize": 1, "query": "tagKeys:env", "readMask": "name,assetType,tags" }
指令範例
curl (Linux、macOS 或 Cloud Shell)
curl -X POST \ -H "X-HTTP-Method-Override: GET" \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d '{ "assetTypes": "compute.googleapis.com/Instance", "pageSize": 1, "query": "tagKeys:env", "readMask": "name,assetType,tags" }' \ https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources
PowerShell (Windows)
$cred = gcloud auth print-access-token $headers = @{ "X-HTTP-Method-Override" = "GET"; "Authorization" = "Bearer $cred" } $body = @" { "assetTypes": "compute.googleapis.com/Instance", "pageSize": 1, "query": "tagKeys:env", "readMask": "name,assetType,tags" } "@ Invoke-WebRequest ` -Method POST ` -Headers $headers ` -ContentType: "application/json; charset=utf-8" ` -Body $body ` -Uri "https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources" | Select-Object -Expand Content
依 TagValue 列出 Compute Engine 執行個體
下列範例說明如何搜尋 my-project 專案中直接附加標記的 Compute Engine 執行個體,其中 TagValue。namespacedName 包含 prod 或 sea,且執行個體名稱不含 instance1。
如要改為依生效 TagValues 搜尋,請將 tagValues 替換為 effectiveTagValues
gcloud
gcloud asset search-all-resources \ --scope=projects/my-project \ --query="tagValues:(prod OR sea) (NOT name:instance1)" \ --asset-types=compute.googleapis.com/Instance \ --format="table(name, assetType, tags)"
REST
HTTP 方法和網址:
POST https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources
JSON 要求主體:
{ "assetTypes": "compute.googleapis.com/Instance", "pageSize": 1, "query": "tagValues:(prod OR sea) (NOT name:instance1)", "readMask": "name,assetType,tags" }
指令範例
curl (Linux、macOS 或 Cloud Shell)
curl -X POST \ -H "X-HTTP-Method-Override: GET" \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d '{ "assetTypes": "compute.googleapis.com/Instance", "pageSize": 1, "query": "tagValues:(prod OR sea) (NOT name:instance1)", "readMask": "name,assetType,tags" }' \ https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources
PowerShell (Windows)
$cred = gcloud auth print-access-token $headers = @{ "X-HTTP-Method-Override" = "GET"; "Authorization" = "Bearer $cred" } $body = @" { "assetTypes": "compute.googleapis.com/Instance", "pageSize": 1, "query": "tagValues:(prod OR sea) (NOT name:instance1)", "readMask": "name,assetType,tags" } "@ Invoke-WebRequest ` -Method POST ` -Headers $headers ` -ContentType: "application/json; charset=utf-8" ` -Body $body ` -Uri "https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources" | Select-Object -Expand Content
在特定時間後建立的 Compute Engine 執行個體
下列範例說明如何搜尋 my-project 專案中,於 2023 年 12 月 31 日後建立的 Compute Engine 執行個體。
如要進一步瞭解日期時間比較,請參閱「數值和時間戳記比較」。
gcloud
gcloud asset search-all-resources \ --scope=projects/my-project \ --query="createTime>2023-31-12" \ --asset-types=compute.googleapis.com/Instance \ --format="table(name, assetType, location)"
REST
HTTP 方法和網址:
POST https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources
JSON 要求主體:
{ "assetTypes": "compute.googleapis.com/Instance", "pageSize": 1, "query": "createTime>2023-31-12", "readMask": "name,assetType,location" }
指令範例
curl (Linux、macOS 或 Cloud Shell)
curl -X POST \ -H "X-HTTP-Method-Override: GET" \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d '{ "assetTypes": "compute.googleapis.com/Instance", "pageSize": 1, "query": "createTime>2023-31-12", "readMask": "name,assetType,location" }' \ https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources
PowerShell (Windows)
$cred = gcloud auth print-access-token $headers = @{ "X-HTTP-Method-Override" = "GET"; "Authorization" = "Bearer $cred" } $body = @" { "assetTypes": "compute.googleapis.com/Instance", "pageSize": 1, "query": "createTime>2023-31-12", "readMask": "name,assetType,location" } "@ Invoke-WebRequest ` -Method POST ` -Headers $headers ` -ContentType: "application/json; charset=utf-8" ` -Body $body ` -Uri "https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources" | Select-Object -Expand Content
位於美國的 Compute Engine 執行個體
以下範例說明如何搜尋 my-project 專案中位於美國的 Compute Engine 執行個體。
gcloud
gcloud asset search-all-resources \ --scope=projects/my-project \ --query="location:us-*" \ --asset-types=compute.googleapis.com/Instance \ --format="table(name, assetType, location)"
REST
HTTP 方法和網址:
POST https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources
JSON 要求主體:
{ "assetTypes": "compute.googleapis.com/Instance", "pageSize": 1, "query": "location:us-*", "readMask": "name,assetType,location" }
指令範例
curl (Linux、macOS 或 Cloud Shell)
curl -X POST \ -H "X-HTTP-Method-Override: GET" \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d '{ "assetTypes": "compute.googleapis.com/Instance", "pageSize": 1, "query": "location:us-*", "readMask": "name,assetType,location" }' \ https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources
PowerShell (Windows)
$cred = gcloud auth print-access-token $headers = @{ "X-HTTP-Method-Override" = "GET"; "Authorization" = "Bearer $cred" } $body = @" { "assetTypes": "compute.googleapis.com/Instance", "pageSize": 1, "query": "location:us-*", "readMask": "name,assetType,location" } "@ Invoke-WebRequest ` -Method POST ` -Headers $headers ` -ContentType: "application/json; charset=utf-8" ` -Body $body ` -Uri "https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources" | Select-Object -Expand Content
執行中的 Compute Engine 執行個體
下列範例說明如何搜尋在 my-project 專案中執行的 Compute Engine 執行個體。
gcloud
gcloud asset search-all-resources \ --scope=projects/my-project \ --query="state=RUNNING" \ --asset-types=compute.googleapis.com/Instance \ --read-mask="name,assetType,location,versionedResources" \ --format="table(name, assetType, location, versionedResources)"
REST
HTTP 方法和網址:
POST https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources
JSON 要求主體:
{ "assetTypes": "compute.googleapis.com/Instance", "pageSize": 1, "query": "state=RUNNING", "readMask": "name,assetType,location,versionedResources" }
指令範例
curl (Linux、macOS 或 Cloud Shell)
curl -X POST \ -H "X-HTTP-Method-Override: GET" \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d '{ "assetTypes": "compute.googleapis.com/Instance", "pageSize": 1, "query": "state=RUNNING", "readMask": "name,assetType,location,versionedResources" }' \ https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources
PowerShell (Windows)
$cred = gcloud auth print-access-token $headers = @{ "X-HTTP-Method-Override" = "GET"; "Authorization" = "Bearer $cred" } $body = @" { "assetTypes": "compute.googleapis.com/Instance", "pageSize": 1, "query": "state=RUNNING", "readMask": "name,assetType,location,versionedResources" } "@ Invoke-WebRequest ` -Method POST ` -Headers $headers ` -ContentType: "application/json; charset=utf-8" ` -Body $body ` -Uri "https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources" | Select-Object -Expand Content
IAM 拒絕政策
以下範例說明如何搜尋 my-project 專案中的 IAM 拒絕政策。
gcloud
gcloud asset search-all-resources \ --scope=projects/my-project \ --asset-types=iam.googleapis.com/PolicyV2 \ --query="name:denypolicies"
REST
HTTP 方法和網址:
POST https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources
JSON 要求主體:
{ "assetTypes": "iam.googleapis.com/PolicyV2", "pageSize": 1, "query": "name:denypolicies" }
指令範例
curl (Linux、macOS 或 Cloud Shell)
curl -X POST \ -H "X-HTTP-Method-Override: GET" \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d '{ "assetTypes": "iam.googleapis.com/PolicyV2", "pageSize": 1, "query": "name:denypolicies" }' \ https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources
PowerShell (Windows)
$cred = gcloud auth print-access-token $headers = @{ "X-HTTP-Method-Override" = "GET"; "Authorization" = "Bearer $cred" } $body = @" { "assetTypes": "iam.googleapis.com/PolicyV2", "pageSize": 1, "query": "name:denypolicies" } "@ Invoke-WebRequest ` -Method POST ` -Headers $headers ` -ContentType: "application/json; charset=utf-8" ` -Body $body ` -Uri "https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources" | Select-Object -Expand Content
如要搜尋未執行的 Compute Engine 執行個體,請將 state=RUNNING 替換為 NOT state:running。
相關 Google Cloud 資源
如要執行關係查詢,必須具備 Security Command Center Premium 或 Enterprise 級別的存取權。
下列範例說明如何在 my-project 專案中,搜尋支援關係類型的相關資源。例如,搜尋完整資源名稱中含有 instance-group 的執行個體群組,找出其中所有 Compute Engine 執行個體。
gcloud
gcloud asset search-all-resources \ --scope=projects/my-project \ --query="relationships:instance-group" \ --format="table(name, assetType, relationships)"
REST
HTTP 方法和網址:
POST https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources
JSON 要求主體:
{ "pageSize": 1, "query": "relationships:instance-group", "readMask": "name,assetType,relationships" }
指令範例
curl (Linux、macOS 或 Cloud Shell)
curl -X POST \ -H "X-HTTP-Method-Override: GET" \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d '{ "pageSize": 1, "query": "relationships:instance-group", "readMask": "name,assetType,relationships" }' \ https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources
PowerShell (Windows)
$cred = gcloud auth print-access-token $headers = @{ "X-HTTP-Method-Override" = "GET"; "Authorization" = "Bearer $cred" } $body = @" { "pageSize": 1, "query": "relationships:instance-group", "readMask": "name,assetType,relationships" } "@ Invoke-WebRequest ` -Method POST ` -Headers $headers ` -ContentType: "application/json; charset=utf-8" ` -Body $body ` -Uri "https://cloudasset.googleapis.com/v1/projects/my-project:searchAllResources" | Select-Object -Expand Content
您也可以指定要搜尋的關係類型。舉例來說,如要搜尋 INSTANCE_TO_INSTANCEGROUP 關係類型,請使用查詢 relationships.INSTANCE_TO_INSTANCEGROUP。如要搜尋完整資源名稱包含 instance-group 的關係類型,請使用查詢 relationships.INSTANCE_TO_INSTANCEGROUP:instance-group。