查看您的素材资源

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

准备工作

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

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  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. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  4. Enable the Cloud Asset Inventory API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). 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. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  7. Enable the Cloud Asset Inventory API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

所需的角色

如需获得完成这些说明所需的权限,请让管理员向您授予组织、文件夹或项目的以下 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. 点击删除,然后确认删除。

后续步骤

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