本頁說明如何管理服務的租戶單元。獨立租用環境是一種輕量級資源,代表服務用戶和代管服務之間的關係。每個服務用戶只能有一個代管服務的有效獨立租用環境。這是 Service Infrastructure 提供的功能。
租戶單位的資源名稱格式如下:
services/{your service name}/projects/{consumer project number}/tenancyUnits/{id}
建立租戶單元時,系統會自動產生 ID。您也可以在呼叫 services.tenancyUnits.create
方法時提供 ID,如果您提供 ID,則在所有服務用戶的代管服務範圍內,該 ID 必須是全域專屬。
本頁面的範例會直接呼叫 Service Consumer Management REST API。如要用於實際工作環境,建議您使用 Google 提供的用戶端程式庫,提升可用性和穩定性。
事前準備
- Service Consumer Management API 必須與代管服務和服務供應商專案搭配使用。您必須先在該專案中擁有 Google Cloud 專案和受管理服務 (例如使用 Cloud Endpoints 建立的服務)。
- 如要使用獨立租用環境,Service Consumer Management API 必須在您的服務生產端機構中建立用戶群專案。請確認您有足夠的配額,可為服務的消費者提供必要數量的租戶專案。
- 如要建立及刪除獨立租用環境,請按照「開始使用 Service Consumer Management API」中的初始設定操作說明進行。
- 在獨立租用環境中建立的每個用戶群專案,也必須位於您指定的資料夾中,這是用戶群專案設定的一部分。因此,您必須擁有機構,才能使用租戶單元。
驗證
Select the tabs for how you plan to access the API:
gcloud
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
用戶端程式庫
如要在本機開發環境中使用用戶端程式庫,請安裝並初始化 gcloud CLI,然後使用您的使用者憑證設定應用程式預設憑證。
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
REST
如要在本機開發環境中使用 REST API,請使用您提供給 gcloud CLI 的憑證。
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using REST」。
如要瞭解如何為正式版環境設定驗證,請參閱 Google Cloud 驗證說明文件中的 Set up Application Default Credentials for code running on Google Cloud。
建立獨立租用環境
通常,當您在自己的服務中建立資源時,如果這些資源需要為消費者佈建額外的 Google Cloud 資源,系統就會建立租戶單元和其中的租戶專案。
使用 services.tenancyUnits.create
方法建立獨立租用環境:
POST https://serviceconsumermanagement.googleapis.com/v1/services/service.example.com/projects/12345678901/tenancyUnits
其中「projects/12345678901」代表服務消費者,service.example.com
則是服務名稱。
系統傳回的資料結構含有獨立租用環境的名稱,以及系統產生的唯一識別碼,可用於存取獨立租用環境。在本範例中,產生的名稱為 services/your-service.example.com/projects/12345678901/tenancyUnits/absdef
。
新增用戶群專案
您現在可以為使用者新增專案。如要將新的用戶群專案新增至上一步建立的獨立租用環境,請使用 services.tenancyUnits.addProject
方法:
POST https://serviceconsumermanagement.googleapis.com/v1/services/your-service.example.com/projects/12345678901/tenancyUnits/absdef:addProject
包含下列資料:
{"tag":"tag1", "project_config":{"folder":"folders/9876543210", "tenant_project_policy":{"policy_bindings":{"role":"roles/owner", "members":"user:bob@example.com"}}, "billing_config":{"billing_account":"billingAccounts/123456-472F22-28F9AA"}}}
tag
值是您在租戶單元中為專案提供的 ID,可以是任何您喜歡的內容 (這裡為 tag1
),例如區域、消費者網路,或只是字串 ID。
這個呼叫會傳回長時間執行的作業,您可以查詢專案是否成功建立。
如果您需要套用不同的設定 (例如新增代管服務),可以呼叫 services.tenancyUnits.applyProjectConfig
方法。
搜尋您的獨立租用環境
找出服務用戶的獨立租用環境
如要找出特定服務消費者的獨立租用環境,請使用 services.tenancyUnits.list
方法,並指定服務消費者專案編號:
GET https://serviceconsumermanagement.googleapis.com/v1/services/your-service.example.com/projects/12345678901/tenancyUnits
搜尋獨立租用環境
您可以使用 services.search
方法,搜尋為服務定義的獨立租用環境。舉例來說,以下查詢會傳回所有內含「tag1」標記的專案所屬的獨立租用環境:
GET https://serviceconsumermanagement.googleapis.com/v1/services/your-service.example.com:search?query=tenant_resources.tag=tag1
清除獨立租用環境
當服務消費者停止使用您的服務時,您需要移除其租戶單元,以釋出資源並確保使用者資料已刪除。
移除用戶群專案
您必須先刪除所有用戶群專案,才能刪除對應的獨立租用環境。您應使用 services.tenancyUnits.removeProject
方法刪除租戶專案和其中的所有資源:
POST https://serviceconsumermanagement.googleapis.com/v1/services/your-service.example.com/projects/12345678901/tenancyUnits/absdef:removeProject
刪除獨立租用環境
刪除獨立租用環境中的所有用戶群專案,或所有專案都處於 DELETED
狀態後,即可使用 services.tenancyUnits.delete
方法刪除獨立租用環境:
DELETE https://serviceconsumermanagement.googleapis.com/v1/services/your-service.example.com/projects/12345678901/tenancyUnits/absdef
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-14 (世界標準時間)。