大量匯入及匯出現有 Google Cloud 資源
本頁面說明 config-connector bulk-export
指令,以及如何使用該指令將 Google Cloud 資源匯出至 Config Connector YAML 檔案,然後匯入 Config Connector。
config-connector bulk-export
會使用 Cloud Asset Inventory 的「匯出」功能,探索現有 Google Cloud 資源。您可以提供 Cloud Asset Inventory 匯出內容,或config-connector
由我們代為匯出。
Cloud Asset Inventory 會匯出 JSON 結構。每個結構都有資源名稱、資產清單類型,以及祖系資源:專案、資料夾和機構。如要瞭解資產清查支援的類型,請參閱「支援的資產類型」。
限制
並非所有資源都支援 bulk-export
指令。如要取得支援的資源清單,請執行 config-connector print-resources
。
事前準備
如要使用
config-connector
工具直接從 Cloud Asset Inventory 匯出,請使用gcloud
在 Identity 的專案中啟用 Cloud Asset Inventory API。 Google Cloudgcloud services enable cloudasset.googleapis.com
大量匯出範例
在本範例中,您將使用 Google Cloud CLI 建立 PubSubTopic,然後匯入 Config Connector。
使用 Google Cloud CLI 建立名為
sample-topic
的主題:gcloud pubsub topics create sample-topic
您會收到主題建立完成的確認訊息。
Created topic [projects/PROJECT_ID/topics/sample-topic].
在輸出內容中,
PROJECT_ID
會替換為您的Google Cloud 專案。使用下列指令取得主題的 Google Cloud 資源名稱,並儲存至環境變數:
TOPIC_RESOURCE_NAME=$(gcloud pubsub topics describe sample-topic --format "value(name)")
為識別物件,
config-connector
工具會使用 Cloud Asset Inventory JSON 結構。將主題資產的 JSON 結構儲存至環境變數:TOPIC_ASSET='{"name":"//pubsub.googleapis.com/'"${TOPIC_RESOURCE_NAME}"'","asset_type":"pubsub.googleapis.com/Topic"}'
執行下列指令,將資產傳遞至
config-connector bulk-export
:echo ${TOPIC_ASSET} | config-connector bulk-export
輸出內容為 YAML 格式的 Config Connector 資源。
--- apiVersion: pubsub.cnrm.cloud.google.com/v1beta1 kind: PubSubTopic metadata: annotations: cnrm.cloud.google.com/project-id: PROJECT_ID name: sample-topic ...
在輸出內容中,
PROJECT_ID
會替換為您的Google Cloud 專案。您可以透過
kubectl apply -f -
將這項資源傳遞至 Config Connector。如要直接傳遞資源,請執行下列指令:echo ${TOPIC_ASSET} | config-connector bulk-export | kubectl apply -f - --namespace CC_NAMESPACE
將 CC_NAMESPACE 替換為 Config Connector 管理資源的命名空間。
Config Connector 會取得資源。
確認 Config Connector 是否使用
kubectl describe
管理資源:kubectl describe pubsubtopic sample-topic --namespace CC_NAMESPACE
將
CC_NAMESPACE
替換為 Config Connector 管理資源的命名空間。
正在清除所用資源
您可以使用 config-connector bulk-export
和 kubectl delete
刪除 PubSubTopic。
echo ${TOPIC_ASSET} | config-connector bulk-export | kubectl delete -f - --namespace CC_NAMESPACE
將 CC_NAMESPACE
替換為 Config Connector 管理資源的命名空間。
尋找要匯入的資源
匯入資源時,您可以執行 Cloud Asset Inventory 匯出作業,並將結果提供給 config-connector bulk-export
,或由 config-connector bulk-export
代表您執行這項作業。
從 Cloud Asset Inventory 匯出內容匯入
您可以提供資產清單匯出作業,方法是提供包含匯出作業的本機檔案路徑,或將匯出作業的結果透過管道傳輸至 STDIN 上的 config-connector
。
從本機檔案匯入
您可以使用含有 --input
參數的本機檔案,向 config-connector bulk-export
提供資產目錄匯出資料。
config-connector bulk-export --input ASSET_INVENTORY_EXPORT
請將 ASSET_INVENTORY_EXPORT
改成 Cloud Asset Inventory 匯出檔案的名稱。
從 STDIN 匯入
如要在 STDIN 上提供資產清單匯出內容,請將匯出結果管道傳送至 config-connector bulk-export
。舉例來說,如果匯出內容位於名為 export.json
的本機檔案中,請將檔案內容管道傳輸至 config-connector bulk-export
,不必提供任何匯出參數。
cat export.json | config-connector bulk-export
透過 STDIN 篩選資產目錄匯出內容
如要篩選匯出的資產目錄,可以使用 jq
工具,並透過管道將結果輸入 config-connector bulk-export
。舉例來說,如要只從 EXPORT_FILE 檔案匯入 PubSubTopic 資產,請執行下列指令:
cat EXPORT_FILE | jq '. | select( .asset_type == "pubsub.googleapis.com/Topic" )' | config-connector bulk-export
使用 config-connector 匯出資產目錄
config-connector bulk-export
工具可以從 Google Cloud
資源階層匯出資源。
匯出專案
如要匯出專案中的所有資源,請使用 --project
參數。
config-connector bulk-export --project PROJECT_ID
將 PROJECT_ID
替換為您的 Google Cloud 專案。
匯出資料夾
如要匯出資料夾中的所有資源,請使用 --folder
參數。
config-connector bulk-export --folder FOLDER_NUMBER
將 FOLDER_NUMBER
替換為您的 Google Cloud 資料夾編號。
匯出機構
如要匯出機構的所有資源,請使用 --organization
參數。
config-connector bulk-export --organization ORGANIZATION_ID
將 ORGANIZATION_ID
替換為您的 Google Cloud 機構 ID。
Cloud Storage 位置
資產清單匯出的輸出位置是 Cloud Storage URI。config-connector bulk-export
執行匯出作業時,會使用 Cloud Storage 值區。根據預設,config-connector bulk-export
會建立臨時 bucket。您也可以指定 bucket 名稱。
臨時 Cloud Storage bucket
如果您未提供 --storage-key
參數,config-connector
bulk-export
會代您建立臨時 Cloud Storage bucket。值區會在儲存空間值區的預設位置建立,也就是 US
多地區。匯出作業完成後,系統會刪除值區。
指定臨時 bucket
如要指定 bucket,請使用 storage-key
參數的 Cloud Storage URI。如果 URI 只有值區名稱,系統會為匯出儲存空間物件產生名稱。如果 URI 是儲存空間物件的完整路徑,則會使用完整路徑。
config-connector bulk-export --storage-key gs://BUCKET_NAME
輸出
config-connector bulk-export
指令的輸出內容是 YAML 格式的 Config Connector 資源。根據預設,YAML 檔案會寫入 STDOUT。您可以使用 output
選項,將資源輸出內容導向至檔案。
輸出至單一檔案
設定 --output
參數時,如果符合下列任一條件,config-connector bulk-export
會將結果寫入單一檔案:
output
指定的檔案存在,且為regular
檔案。output
指定的檔案不存在,但output
代表的上層目錄存在。
輸出至目錄
如果 --output
參數是結尾為 /
的目錄,config-connector
會將結果寫入多個檔案。config-connector bulk-export
會為每個資源建立一個檔案,且檔案名稱與資源名稱相符。
config-connector bulk-export --project PROJECT_ID --on-error continue --output OUTPUT_DIRECTORY/
將 PROJECT_ID
替換為您的 Google Cloud 專案。
舉例來說,如要將專案 my-project
的資產輸出至 sample
目錄,請執行下列指令:
config-connector bulk-export --project my-project --on-error continue --output sample/
指令列選項
config-connector bulk-export
指令包含下列選項:
config-connector bulk-export
--input FILENAME \
--output FILENAME \
--storage-key gs://BUCKET_NAME \
--project PROJECT_ID \
--folder FOLDER_NUMBER \
--organization ORGANIZATION_ID \
--oauth2-token TOKEN \
--on-error [halt | continue | ignore] \
--iam-format [policy | policymember | none] \
--filter-deleted-iam-members [true | false] \
--verbose
--input
:Cloud Asset Inventory 輸入檔案。--output
:選用的輸出檔案路徑,可停用標準輸出。如果是檔案,結果會包含所有指令輸出內容;如果是目錄,目錄會包含輸出內容中每個資源的新檔案。--storage-key
:匯出的目標暫時 Cloud Storage 值區。--project
: Google Cloud 要匯出的專案 ID--folder
: Google Cloud 要匯出的資料夾 ID--organization
:要匯出的 Google Cloud 機構 ID。--oauth2-token
:做為 Google Cloud 身分的 OAuth 2.0 權杖。根據預設,config-connector
會使用 Google Cloud CLI 預設憑證。--on-error
:控制發生可復原錯誤時的行為。選項為「continue」、「halt」或「ignore」。halt
:在發生任何錯誤時停止執行 (預設)continue
:繼續處理資源,將錯誤列印至 STDERRignore
:繼續處理資源,不要列印錯誤
--iam-format
:指定要透過匯出作業輸出哪種 IAM 資源。選項包括:policy
(預設值)、policymember
或none
。--filter-deleted-iam-members
:指定是否要篩除已刪除的 IAM 主體。 選項為true
或false
。預設值為false
。--verbose
:啟用詳細記錄。
後續步驟
- 請參閱這篇文章 Google Cloud ,瞭解 Config Connector 如何取得現有資源
- 瞭解 Cloud Asset Inventory 和將資產匯出至 Cloud Storage。
- 瞭解 Config Connector 支援的資源。