查看您的素材资源
本文档介绍如何在 Cloud Shell 中使用 Cloud Asset Inventory 和 Google Cloud CLI 查看资产。
准备工作
如需完成本文档中的说明,您需要启用 Cloud Asset Inventory API 并设置访问 Cloud Asset Inventory 和 Compute Engine 的权限。
- 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.
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
如果您要使用现有项目来完成本指南,请验证您是否拥有完成本指南所需的权限。如果您创建了新项目,则您已拥有所需的权限。
-
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 theserviceusage.services.enablepermission. Learn how to grant roles. -
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
如果您要使用现有项目来完成本指南,请验证您是否拥有完成本指南所需的权限。如果您创建了新项目,则您已拥有所需的权限。
-
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 theserviceusage.services.enablepermission. Learn how to grant roles. -
如需查看资产元数据,请授予以下角色:
Cloud Asset Viewer (
roles/cloudasset.viewer) -
如需创建和删除 Compute Engine 虚拟机实例,请执行以下操作:
-
Compute Instance Admin (v1) (
roles/compute.instanceAdmin.v1) -
Service Account User (
roles/iam.serviceAccountUser)
-
Compute Instance Admin (v1) (
-
列出资产元数据:
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
-
在 Google Cloud 控制台中,转到虚拟机实例页面。
点击创建实例。
输入实例的名称。
点击创建。
等待实例创建完毕。
在 Google Cloud 控制台中,点击 激活 Cloud Shell。
在 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 控制台中,转到虚拟机实例页面。
在您创建的虚拟机实例的名称所在的行中,点击 更多操作。
点击删除,然后确认删除。
- 如需了解与查看资产相关的更多选项,请参阅列出资产。
所需的角色
如需获得完成这些说明所需的权限,请让管理员向您授予组织、文件夹或项目的以下 IAM 角色:
如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
这些预定义角色包含完成这些说明所需的权限。如需查看所需的确切权限,请展开所需权限部分:
所需权限
您需要拥有以下权限才能完成这些说明:
查看您的素材资源
在您选择的项目中,创建一个 Compute Engine 虚拟机实例,以便您可以在 Cloud Asset Inventory 中查看该实例:
列出项目中的所有虚拟机实例:
清理
为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。