查看您的素材资源

本文档介绍如何在 Cloud Shell 中使用 Cloud Asset Inventory 和 Google Cloud CLI 查看资产。

准备工作

如需完成本文档中的说明,您需要启用 Cloud Asset Inventory API 并设置访问 Cloud Asset Inventory 和 Compute Engine 的权限。

  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. 如果您要使用现有项目来完成本指南,请验证您是否拥有完成本指南所需的权限。如果您创建了新项目,则您已拥有所需的权限。

  4. Enable the Cloud Asset Inventory API.

    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 API

  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. 如果您要使用现有项目来完成本指南,请验证您是否拥有完成本指南所需的权限。如果您创建了新项目,则您已拥有所需的权限。

  7. Enable the Cloud Asset Inventory API.

    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 API

  8. 所需的角色

    如需获得完成这些说明所需的权限,请让管理员向您授予组织、文件夹或项目的以下 IAM 角色:

    如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

    这些预定义角色包含完成这些说明所需的权限。如需查看所需的确切权限,请展开所需权限部分:

    所需权限

    您需要拥有以下权限才能完成这些说明:

    • 列出资产元数据: cloudasset.assets.listResource
    • 如需创建 Compute Engine 虚拟机实例,请执行以下操作:
      • compute.disks.create
      • compute.instances.create
      • compute.instances.delete
      • compute.instances.setServiceAccount
      • compute.projects.get
      • compute.subnetworks.use
      • compute.subnetworks.useExternalIp
      • compute.zoneOperations.get

    您也可以使用自定义角色或其他预定义角色来获取这些权限。

    查看您的素材资源

    在您选择的项目中,创建一个 Compute Engine 虚拟机实例,以便您可以在 Cloud Asset Inventory 中查看该实例:

    1. 在 Google Cloud 控制台中,转到虚拟机实例页面。

      进入“虚拟机实例”

    2. 点击创建实例

    3. 输入实例的名称。

    4. 点击创建

    5. 等待实例创建完毕。

    列出项目中的所有虚拟机实例:

    1. 在 Google Cloud 控制台中,点击 激活 Cloud Shell

    2. 在 Cloud Shell 中,运行以下命令:

      gcloud asset list \
          --project=PROJECT_ID \
          --asset-types=compute.googleapis.com/Instance \
          --content-type=resource
      

      PROJECT_ID 替换为您要列出其资产的项目的 ID。

      在上面的代码示例中,资产类型 compute.googleapis.com/Instance 仅用于列出 Compute Engine 虚拟机实例。如需查看所有资源类型,请参阅资源类型

      还设置了 resource 的内容类型。这表示响应中还应返回 resource 元数据。如果未设置内容类型,则仅返回每项资产的基本信息,例如资产名称、上次更新时间以及所属项目。

    清理

    为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。

    1. 在 Google Cloud 控制台中,转到虚拟机实例页面。

      进入“虚拟机实例”

    2. 在您创建的虚拟机实例的名称所在的行中,点击 更多操作

    3. 点击删除,然后确认删除。

    后续步骤

    • 如需了解与查看资产相关的更多选项,请参阅列出资产