發布 Google 來源的事件

您可以建立 GoogleApiSource 資源,為特定 Eventarc Advanced 匯流排啟用 Google 來源事件的收集和發布功能。

Google 來源的事件是由未經中介的事件直接觸發,例如建立 Cloud Run 函式,或是 Dataflow 工作狀態變更。詳情請參閱「Eventarc 支援的 Google 事件類型」。

注意事項:

  • 根據預設,系統會從 Google Cloud 專案收集事件,其中包含GoogleApiSource資源。您也可以使用 gcloud CLI,從與 GoogleApiSource 資源位於同一機構的多個專案收集事件。詳情請參閱本文「從多個專案發布事件」一節。
  • 您可以將事件發布至與 GoogleApiSource 資源所在專案不同的 Google Cloud 專案 中的匯流排。您可以使用 gcloud CLI 或傳送要求至 Eventarc API (但無法透過 Google Cloud 控制台) 設定這項功能。
  • 每個專案在每個區域最多只能有 1 個資源GoogleApiSource Google Cloud

事前準備

開始之前,請確認您已完成下列步驟:

控制台

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Eventarc and Eventarc Publishing APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Eventarc and Eventarc Publishing APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

gcloud

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install the Google Cloud CLI.

  3. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  4. To initialize the gcloud CLI, run the following command:

    gcloud init
  5. Create or select a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Eventarc and Eventarc Publishing APIs:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable eventarc.googleapis.com eventarcpublishing.googleapis.com
  8. Install the Google Cloud CLI.

  9. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  10. To initialize the gcloud CLI, run the following command:

    gcloud init
  11. Create or select a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  12. Verify that billing is enabled for your Google Cloud project.

  13. Enable the Eventarc and Eventarc Publishing APIs:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable eventarc.googleapis.com eventarcpublishing.googleapis.com

啟用 Google 來源事件

如要發布 Google 來源的事件,您必須建立 GoogleApiSource 資源。這項資源代表特定 Eventarc Advanced 匯流排的特定 Google Cloud 專案中,Google API 事件的訂閱項目。

您可以透過下列方式啟用發布 Google 來源事件:

  • 在 Google Cloud 控制台
  • 在終端機或 Cloud Shell 中使用 Google Cloud CLI
  • 傳送要求至 Eventarc API

控制台

  1. 在 Google Cloud 控制台中,依序前往「Eventarc」>「Bus」(匯流排) 頁面。

    前往 Bus

  2. 您可以建立巴士,或在更新巴士時按一下 「編輯」

  3. 在「編輯匯流排」頁面中,如要新增訊息來源,請按一下 「新增來源」

    1. 在「新增訊息來源」窗格中,針對 Google API 訊息供應商,接受預設的 google-api-source 或輸入其他訊息來源名稱。
    2. 點選「建立」
  4. 按一下 [儲存]

gcloud

  1. 開啟終端機。

  2. 您可以使用 gcloud eventarc google-api-sources create 指令啟用 Google 來源事件:

    gcloud eventarc google-api-sources create GOOGLE_API_SOURCE_NAME \
        --destination-message-bus=BUS_NAME \
        --destination-message-bus-project=BUS_PROJECT_ID

    更改下列內容:

    • GOOGLE_API_SOURCE_NAMEGoogleApiSource 資源的 ID 或完整識別碼,代表特定匯流排的 Google API 事件訂閱項目
    • BUS_NAME:您要訂閱 Google API 事件的匯流排 ID 或完整識別碼
    • BUS_PROJECT_ID:匯流排的 Google Cloud 專案 ID

    選用:您也可以使用下列標記:

    • --async,讓指令立即傳回,不必等待執行中的作業完成。
    • --crypto-key,指定客戶管理的加密金鑰的完整名稱;如未指定,則使用 Google-owned and managed keys 。
    • --logging-config,設定記錄層級,必須是下列其中一個值:NONEDEBUGINFONOTICEWARNINGERRORCRITICALALERTEMERGENCY。詳情請參閱「LogSeverity」。

REST API

如要啟用 Google 來源事件,請使用 projects.locations.googleApiSources.create 方法。

使用任何要求資料之前,請先替換以下項目:

  • GOOGLE_API_SOURCE_NAMEGoogleApiSource 資源的顯示名稱,代表特定匯流排的 Google API 事件訂閱項目,例如 my_google_source
  • LABEL_KEYLABEL_VALUE:選用。標籤鍵/值組合對應,可協助您整理 Google Cloud 資源。詳情請參閱「什麼是標籤?
  • ANNOTATION_KEYANNOTATION_VALUE:選用。任意格式文字的註解鍵/值組合對應。您可以使用這些標籤,附加與資源相關的任意資訊。詳情請參閱「註解」一節。
  • DESTINATION:Eventarc Advanced 匯流排的完整 ID,Google 來源事件應以 projects/PROJECT_NAME/locations/LOCATION/messageBuses/BUS_NAME 格式發布至該匯流排。詳情請參閱「建立訊息路由匯流排」。
  • ENCRYPT_KEY:選用。GoogleApiSource資源應使用的 Cloud KMS 金鑰,用於加密資料,格式為 projects/PROJECT_NAME/locations/LOCATION/keyRings/RING_NAME/cryptoKeys/KEY_NAME。詳情請參閱「使用客戶管理的加密金鑰」。
  • PROJECT_ID:要在其中建立 GoogleApiSource 資源的專案 ID。 Google Cloud
  • LOCATION:建立 GoogleApiSource 資源的支援區域,例如 us-central1
  • LOG_SEVERITY:選用。記錄項目所述事件的最低嚴重程度。只能設定為NONEDEBUGINFONOTICEWARNINGERRORCRITICALALERTEMERGENCY。預設值為 NONE。詳情請參閱 LogSeverity

JSON 要求主體:

{
  "displayName": "GOOGLE_API_SOURCE_NAME",
  "labels": {"LABEL_KEY":"LABEL_VALUE"},
  "annotations": {"ANNOTATION_KEY":"ANNOTATION_VALUE"},
  "destination": "DESTINATION",
  "cryptoKeyName": "ENCRYPT_KEY",
  "loggingConfig": {"logSeverity":"LOG_SEVERITY"}
}

請展開以下其中一個選項,以傳送要求:

如果成功,回應主體會包含新建立的 Operation 例項:

{
  "name": "projects/PROJECT_ID/locations/LOCATION/operations/OPERATION_ID",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.eventarc.v1.OperationMetadata",
    "createTime": "2024-01-25T17:17:45.782370139Z",
    "target": "projects/PROJECT_ID/locations/LOCATION/googleApiSources/GOOGLE_API_SOURCE_NAME",
    "verb": "create",
    "requestedCancellation": false,
    "apiVersion": "v1"
  },
  "done": false
}

系統現在會收集與 GoogleApiSource 資源位於相同 Google Cloud 專案的 Google 來源事件,並發布至匯流排。

停用 Google 來源相關事件

如要禁止特定Google Cloud 專案中的 Google 來源將事件發布至 Eventarc Advanced 匯流排,請刪除 GoogleApiSource 資源。

您可以透過下列方式停用發布 Google 來源的事件:

  • 在 Google Cloud 控制台
  • 在終端機或 Cloud Shell 中使用 gcloud CLI
  • 傳送要求至 Eventarc API

控制台

  1. 在 Google Cloud 控制台中,依序前往「Eventarc」>「Bus details」(匯流排詳細資料) 頁面。

    前往「巴士詳細資料」

  2. 按一下「編輯」

  3. 在「編輯匯流排」頁面中,如要刪除 Google API 訊息提供者,請按一下「google-api-source」訊息來源 (或您為其指定的名稱) 的「刪除資源」

  4. 在「Delete resource」(刪除資源) 提示中,按一下「Confirm」(確認)

  5. 按一下 [儲存]

gcloud

  1. 開啟終端機。

  2. 您可以使用 gcloud eventarc google-api-sources delete 指令停用 Google 來源事件:

    gcloud eventarc google-api-sources delete GOOGLE_API_SOURCE_NAME \
        --location=REGION

    更改下列內容:

    • GOOGLE_API_SOURCE_NAMEGoogleApiSource資源的 ID 或完整 ID
    • REGIONGoogleApiSource 資源的位置

REST API

如要停用 Google 來源事件,請使用 projects.locations.googleApiSources.delete 方法。

使用任何要求資料之前,請先替換以下項目:

  • PROJECT_ID:建立 GoogleApiSource 資源的 Google Cloud 專案 ID。
  • LOCATION:建立 GoogleApiSource 資源的區域,例如 us-central1
  • GOOGLE_API_SOURCE_NAME:要刪除的 GoogleApiSource 資源名稱。

請展開以下其中一個選項,以傳送要求:

如果成功,回應主體會包含新建立的 Operation 例項:

{
  "name": "projects/PROJECT_ID/locations/LOCATION/operations/OPERATION_ID",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.eventarc.v1.OperationMetadata",
    "createTime": "2024-01-25T17:17:45.782370139Z",
    "target": "projects/PROJECT_ID/locations/LOCATION/googleApiSources/GOOGLE_API_SOURCE_NAME",
    "verb": "delete",
    "requestedCancellation": false,
    "apiVersion": "v1"
  },
  "done": false
}

系統不會再收集及發布與已刪除 Google Cloud 資源位於同一GoogleApiSource專案的 Google 來源事件。

從多個專案發布事件

如要從多個 Google Cloud 專案收集及發布 Google 來源事件,您必須建立GoogleApiSource Google Cloud 資源,且專案必須與 GoogleApiSource 資源位於同一個機構。如要進一步瞭解機構,請參閱資源階層

注意事項:

  • 如要設定從多個專案發布事件,必須使用 gcloud CLI,且不會顯示在 Google Cloud 控制台中。
  • 如果含有 GoogleApiSource 資源的專案遷移至其他機構,從多個專案發布事件將無法正常運作。您必須更新或重新建立 GoogleApiSource 資源。

1. 授予 Eventarc 服務代理適當的 IAM 角色

如要在機構層級收集事件,請使用 gcloud organizations add-iam-policy-binding 指令,授予 Eventarc 服務代理程式適當的 Identity and Access Management (IAM) 角色:

gcloud organizations add-iam-policy-binding ORGANIZATION_ID \
    --member="SERVICE_AGENT_EMAIL" \
    --role="roles/eventarc.multiProjectEventCollector"

更改下列內容:

  • ORGANIZATION_ID:您的機構資源 ID
  • SERVICE_AGENT_EMAIL:您要在當中建立 GoogleApiSource 資源的專案中,Eventarc 主要服務代理程式的電子郵件地址。電子郵件地址的格式如下:

    service-PROJECT_NUMBER@gcp-sa-eventarc.iam.gserviceaccount.com
    

    PROJECT_NUMBER 替換為您的Google Cloud 專案編號。您可以在 Google Cloud 控制台的「歡迎」頁面中找到專案編號,也可以執行下列指令:

    gcloud projects describe PROJECT_ID --format='value(projectNumber)'
    

2. 從所有專案或專案清單發布

如要從多個專案中的 Google 來源發布事件,請使用下列任一方式執行 gcloud eventarc google-api-sources create 指令:

  • 從與 GoogleApiSource 資源位於同一機構的所有 Google Cloud 專案發布:

    gcloud eventarc google-api-sources create GOOGLE_API_SOURCE_NAME \
        --location=REGION \
        --destination-message-bus=BUS_NAME \
        --destination-message-bus-project=BUS_PROJECT_ID \
        --organization-subscription
  • 從與 GoogleApiSource 資源位於同一個機構的專案清單 Google Cloud 發布:

    gcloud eventarc google-api-sources create GOOGLE_API_SOURCE_NAME \
        --location=REGION \
        --destination-message-bus=BUS_NAME \
        --destination-message-bus-project=BUS_PROJECT_ID \
        --project-subscriptions=LIST_OF_PROJECTS

    更改下列內容:

    • GOOGLE_API_SOURCE_NAMEGoogleApiSource資源的 ID 或完整 ID
    • REGIONGoogleApiSource 資源的位置
    • BUS_NAME:您要訂閱 Google API 事件的匯流排 ID 或完整識別碼
    • BUS_PROJECT_ID:匯流排的 Google Cloud 專案 ID
    • LIST_OF_PROJECTS:以半形逗號分隔的Google Cloud 專案 ID 或專案編號清單。您可以在同一份清單中同時加入 ID 和號碼。如要從包含 GoogleApiSource 資源的Google Cloud 專案發布事件,必須在清單中明確加入專案 ID 或編號。

後續步驟