管理租户单元

本页面介绍如何管理服务的租户单元租户单元是一种轻量级资源,用于表示服务使用方托管式服务之间的关系。对于一个托管式服务而言,每个服务使用方只能有一个活动的租户单元。此功能由 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, then initialize it by running the following command:

gcloud init

客户端库

如需在本地开发环境中使用客户端库,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。

  1. Install the Google Cloud CLI.
  2. To initialize the gcloud CLI, run the following command:

    gcloud init
  3. 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.

Google Cloud

REST

如需在本地开发环境中使用 REST API,请使用您提供给 gcloud CLI 的凭据。

    Install the Google Cloud CLI, then initialize it by running the following command:

    gcloud init

如需了解详情,请参阅 Google Cloud 身份验证文档中的使用 REST 时进行身份验证

如需了解如何为生产环境设置身份验证,请参阅 Google Cloud 身份验证文档中的 Set up Application Default Credentials for code running on Google Cloud

创建租户单元

在创建您自己的服务中的资源时,根据为使用方预配的其他 Google Cloud 资源,系统通常会创建租户单元及其中的租户项目。

您按如下所示创建租户单元:

POST https://serviceconsumermanagement.googleapis.com/v1/services/service.example.com/projects/12345678901/tenancyUnits

其中,“projects/12345678901”表示服务使用方,service.example.com 是您的服务的名称。

返回的数据结构包含租户单元的名称以及生成的唯一 ID(可用于访问它)。在此示例中,生成的名称为 services/your-service.example.com/projects/12345678901/tenancyUnits/absdef

添加租户项目

您现在可以为用户添加项目了。如要将新租户项目添加到上一步创建的租户单元中,请调用以下方法:

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 值是您在租户单元内为项目提供的标识符:它可以是您喜欢的任何内容(这里是 tag1),可以是区域、用户网络或只是字符串 ID。

此调用返回一个长时间运行的操作,您可以查询该操作以查看项目创建是否成功。

如果需要应用其他配置(例如,添加新的托管服务),则可以调用 services.tenancyUnits.applyProjectConfig 方法。

搜索您的租户单元

为服务使用方查找某一租户单元

如要为特定服务使用方查找某一租户单元,请使用 services.tenancyUnits.ListTenancyUnits 方法指定服务使用方项目编号:

GET https://serviceconsumermanagement.googleapis.com/v1/services/your-service.example.com/projects/12345678901/tenancyUnits

搜索租户单元

您可以使用 services.tenancyUnits.SearchTenancyUnits 方法搜索为您的服务定义的租户单位。举例来说,以下查询将返回所有满足下列条件的单元:其中包含带有“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 状态时,您可以删除租户单元:

DELETE https://serviceconsumermanagement.googleapis.com/v1/services/your-service.example.com/projects/12345678901/tenancyUnits/absdef